2

如何在 MSTest 中的 TestMethods 之间共享状态。这些测试将作为有序测试按顺序运行。

    private TestContext testContext;

    public TestContext TestContext
    {
        get { return this.testContext; }
        set { this.testContext = value;}
    }

    [TestMethod]
    public void Subscribe()
    {
        bool subscribed = true;
        TestContext.Properties.Add("subscribed", subscribed);
        Assert.IsTrue(subscribed == true, string.Format("Subscribed...{0}", this.GetHashCode()));
    }


    [TestMethod]
    public void GenerateEvent()
    {
        bool subscribed = (bool)TestContext.Properties["subscribed"];
        Assert.IsTrue(subscribed == true, string.Format("Subscribed...{0}", this.GetHashCode()));

    }

提前致谢。

4

4 回答 4

1

维亚斯,我同意乍得的观点,你仍然做错了。

也就是说,您可以考虑使用 TestContext 对象。

请参阅http://blogs.msdn.com/vstsqualitytools/archive/2006/01/10/511030.aspx

于 2008-10-17T02:25:53.990 回答
0

您可以使用静态成员在测试方法之间共享数据。

例如:

private static List<string> SharedValues = new List<string>();

[TestMethod]
public void TestMethod1()
{
   SharedValues.Add("Awesome!");
}

[TestMethod]
public void TestMethod2()
{
   SharedValues.Add("Thanks for the answer!");
}

[TestMethod]
public void TestMethod3()
{
    Assert.IsTrue(SharedValues.Contains("Awesome!"));
    Assert.IsTrue(SharedValues.Contains("Thanks for the answer!"));
}

复制此代码并创建一个新的有序测试,测试 TestMethod1、TestMethod2、TestMethod3。会过去的!

于 2008-12-31T16:54:51.263 回答
0

正如乍得指出的那样,似乎我别无选择,只能使用单个测试 [一旦工具迫使我做正确的事情;)] 来测试整个流程。

似乎我可以使用 TestContext.BeginTimer & EndTimer 来计时方法中的每个调用。

这是我收到此答案的 MSDN 论坛的链接

于 2008-10-19T04:21:34.440 回答
0

您可以通过将信息存储在类级静态字段或属性中而不是 TestContext 中来跨测试共享信息。这样一来,就只有一份了。此外,任何一项测试放入这些字段或属性中的任何内容都可用于任何后续测试。

话虽如此......有序和/或相互依赖的测试仍然是邪恶的。

于 2011-09-14T12:22:30.463 回答