gpt4 book ai didi

c# - 如何从 TestCleanup 方法中获取测试名称?

转载 作者:太空狗 更新时间:2023-10-29 21:37:49 25 4
gpt4 key购买 nike

我注意到 [TestCleanup] 方法不能采用 TestContext 参数。那么我应该如何知道正在清理哪个测试?

最佳答案

你可以在你的类上有一个名为 TestContext 的公共(public)属性,它将由 MSTest 设置,例如:

[TestClass]
public class UnitTest1
{
public TestContext TestContext { get; set; }
[TestMethod]
public void TestMethod1()
{
var x = 2;
var y = 1 + 1;
Assert.AreEqual(x, y);
}

[TestMethod]
public void TestMethod2()
{
Assert.AreEqual(true, true);
}

[TestCleanup]
public void TestCleanup()
{
Debug.WriteLine(TestContext.TestName);
}
}

关于c# - 如何从 TestCleanup 方法中获取测试名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44146900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com