C#静态属性,不止一次初始化

Joh*_*see 2 c# static properties coded-ui-tests

我有一个类(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....
    }
}
Run Code Online (Sandbox Code Playgroud)

我认为MyStaticProp在运行中保持不变(首次运行,初始值= null,第二次运行初始值"AHA").但显然MyStaticProp从运行到运行总是重置为null.知道为什么会这样吗?

编辑:谢谢大家的帮助!我想我会创建一个"DataClass",它将保存到temp-folder中/从temp-folder中加载.像这样我可以肯定会发生什么.

我仍然没有得到的是,为什么它在过去有效,但现在它已不复存在了.

jde*_*aan 8

我想我现在更好地理解了这个问题,CodedUI不会在运行之间使用相同的对象,也不会使用相同的AppDomain,上次运行中使用的AppDomain可能会被丢弃.这种方式可以CodedUI生成可重复的测试,这些测试不依赖于先前运行的致命状态.