在TestCleanup中传递参数

ssi*_*ngh 6 .net unit-testing mstest

我想在单元测试中执行TestCleanup,但我需要将参数传递给清理方法.但由于自动调用默认的TestCleanup,我无法将任何参数传递给它.

有人可以建议一种方法吗?

Zor*_*ayr 4

您可以使用测试类实例变量在设置、测试和清理测试方法之间进行通信:

namespace YourNamespace
{
    [TestClass]
    public class UnitTest1
    {
        private string someValue;

        [TestMethod]
        public void TestMethod1()
        {
            someValue = "someValue";
        }

        [TestCleanup]
        public void CleanUp()
        {
            // someValue is accessible here.
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

由于该CleanUp()方法将在每个单元测试之后运行,someValue因此将绑定到正确的单元测试的上下文。

希望这可以帮助。