MSTest中[TearDown]和[SetUp]的替代方案是什么?


244

当我使用MSTest Framework并复制Selenium IDE为我生成的代码时,MSTest无法识别[TearDown][SetUp]。有什么替代方法?

Answers:


276

您将分别使用[TestCleanup][TestInitialize]


7
设置= [TestInitialize]; TearDown = [TestCleanup]
ZaneDarken

267

请记住,您的Initialize / Cleanup方法必须使用正确的签名。

http://msdn.microsoft.com/zh-CN/library/microsoft.visualstudio.testtools.unittesting.classinitializeattribute.aspx

    [AssemblyInitialize()]
    public static void AssemblyInit(TestContext context) {}

    [ClassInitialize()]
    public static void ClassInit(TestContext context) {}

    [TestInitialize()]
    public void Initialize() {}

    [TestCleanup()]
    public void Cleanup() {}

    [ClassCleanup()]
    public static void ClassCleanup() {}

    [AssemblyCleanup()]
    public static void AssemblyCleanup() {}

9
此处未提及的AssemblyInitialize和AssemblyCleanup +1此处stackoverflow.com/a/1873572/864201
Rodolpho Brock

@dunken我们是否知道即使两次之间的测试用例执行失败,也都会执行TestCleanup()?
kjosh

104

[TestInitialize]以及[TestCleanup]个人测试级别[ClassInitialize][ClassCleanup]班级级别。


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.