我必须从helper类的静态方法中找到有关当前运行的UnitTest的信息。这个想法是从每个测试中获得一个独特的密钥。
我考虑过使用TestContext,不确定它是否可行。
例
[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
}
}我们目前正在使用Thread.SetData将该密钥存储在线程上,但如果测试代码生成多个线程,则会出现问题。对于每个线程,我需要为给定的单元测试获得相同的密钥。
Foo.GetSomething()不是从单元本身调用的。调用它的代码是由Unity注入的模拟代码。
编辑
我会解释一下上下文,因为它似乎让人困惑。
通过统一创建的对象是实体框架的上下文。在运行单元测试时,上下文在Foo.GetSomething创建的结构中获取其数据。我们叫它DataPersistance吧。
DataPersistance不能是单一的,因为单元测试会相互影响。
我们目前每个线程都有一个DataPersistance实例,只要测试的代码是单线程的,就好了。
我希望每个单元测试都有一个DataPersistance实例。如果每个测试都可以得到一个唯一的guid,我就可以解析这个测试的实例。
发布于 2013-08-23 16:00:49
public static class Foo
{
public static Something GetSomething(Guid guid)
{
//Return something base on this key
return new Something();
}
}测试:
[TestClass]
public void MyTestClass
{
public TestContext TestContext { get; set; }
[TestMethod]
public void MyTestMethod()
{
Guid guid = ...;
Something something = Foo.GetSomething(guid);
}
}https://stackoverflow.com/questions/18407086
复制相似问题