Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting info from currently running unit test
    text
    copied!<p>I have to find info about the currently running UnitTest from a helper class' static method. The Idea is getting a unique key from each test.</p> <p>I thought about using <code>TestContext</code>, not sure if it is possible.</p> <p>Exemple</p> <pre><code>[TestClass] public void MyTestClass { public TestContext TestContext { get; set; } [TestMethod] public void MyTestMethod() { TestContext.Properties.Add("MyKey", Guid.NewGuid()); //Continue.... } } public static class Foo { public static Something GetSomething() { //Get the guid from test context. //Return something base on this key } } </code></pre> <p>We are currently storing this key on the thread with <code>Thread.SetData</code>, but it is problematic if the tested code spawn multiple thread. For each thread I need to get the same key for a given unit test.</p> <p><code>Foo.GetSomething()</code> is not called from the unittest itself. The code calling it is a mock injected by Unity.</p> <p><strong>Edit</strong></p> <p>I'll explain the context a little bit, because it seems to be confusing.</p> <p>The object created via unity is entity framework's context. When running unit tests, the context get its data in a structure that is created by <code>Foo.GetSomething</code>. Let's call it <code>DataPersistance</code>.</p> <p><code>DataPersistance</code> cannot be a singleton, because unit tests would impact each others.</p> <p>We currently have one instance of <code>DataPersistance</code> per thread, witch is nice as long as the tested code is single threaded.</p> <p>I want one instance of <code>DataPersistance</code> per unit test. If a could get an unique guid per test, I could resolve the instance for this test.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload