我有一个类 (codedUI),包含静态属性,用于保存每次运行的变量:
[CodedUITest]
public class SomeClass
{
public static string MyStaticProp { get; set; }
[TestMethod]
public void TestMethod1()
{
SomeClass.MyStaticProp = "AHA";
}
[TestMethod]
public void TestMethod2()
{
string x = SomeClass.MyStaticProp;//when TestMethod1 and TestMethod2 are called from an "ordered test", MyStaticProp is reset everytime. The strange thing: it used to work....
}
}
我认为 MyStaticProp 会在每次运行中保持不变(第一次运行,初始值 = null,第二次运行初始值“AHA”)。 但显然 MyStaticProp 从运行到运行总是重置为 null。 知道为什么会发生这种情况吗?
编辑: 感谢大家的帮助! 我想我会创建一个“DataClass”,它将保存到临时文件夹/从临时文件夹加载。 这样我就可以确定什么时候会发生什么。
我仍然不明白的是,为什么它在过去有效但现在不再有效了。
最佳答案
我想我现在更好地理解了这个问题,CodedUI
在运行之间没有使用相同的对象,也没有使用相同的 AppDomain,上次运行中使用的 AppDomain 可能被丢弃了。通过这种方式,CodedUI
生成可重现的测试,这些测试不依赖于先前运行的致命状态。
关于C# 静态属性,被多次初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6186677/