多个TestInitialize和TestCleanup
问题描述:
是否有一种方法可以为每个测试用例提供特定的初始化和清理方法。问题是每个测试用例都有不同的清理工作。
Is there a way that we can have particular initialize and cleanup methods for each test case. The issue is that there are different cleanups for each test case.
请提供最佳实践建议。
答
mn1987,
mn1987,
在测试类中为清理方法创建布尔变量,并使用它来驱动初始化/清理方法中的相应操作。
Create boolean variables in your test class for the cleanup methods and use it to drive the appropriate action(s) in your initialize/cleanup methods.
[CodedUITest]
public class CodedUITest1
{
static bool executeCleanup1 = true;
static bool executeCleanup2 = false;
public CodedUITest1()
{
}
[TestMethod]
public void CodedUITestMethod1()
{
executeCleanup1 = false;
executeCleanup2 = true;
this.UIMap.RecordedMethod1();
}
public void CodedUITestMethod2()
{
executeCleanup1 = false;
executeCleanup2 = true;
this.UIMap.RecordedMethod2();
}
#region Additional test attributes
[TestCleanup()]
public void MyTestCleanup()
{
if (executeCleanup1)
{
//executeCleanup1
}
if (executeCleanup2)
{
//executeCleanup2
}
}
#endregion