gpt4 book ai didi

c# - 如何在每节课后运行 ClassCleanup (MSTest)?

转载 作者:可可西里 更新时间:2023-11-01 08:55:41 24 4
gpt4 key购买 nike

我有几个带有测试套件的类(class)。

每个测试类都从 ClassInitialize 开始,到 ClassCleanup 结束。我的问题是 ClassCleanup 没有在每个类的末尾调用,它仅在三个类的所有测试之后调用。我可以解决这个问题吗?谢谢!

[ClassInitialize]
public static void SetUpBrowser(TestContext context)
{
pageObjectBase.SetBrowser("chrome");
pagesManager.GetPageObjectBase();
}

[TestMethod]
public void FindCriticalBug()
{
bla-bla-bla();
}

[ClassCleanup]
public static void CloseBrowser()
{
pageObjectBase.Stop();
pagesManager.GeneralClearing();
}

最佳答案

测试无序运行,包括跨类测试。请参阅此博客文章:

https://learn.microsoft.com/archive/blogs/ploeh/classcleanup-may-run-later-than-you-think

引用:

In any case, here's the result from my Output Window:

AssemblyInitialize
TestClass1: ClassInitialize
TestClass1: TestInitialize
TestClass1: MyTestCase1
TestClass1: TestCleanup
TestClass2: ClassInitialize
TestClass2: TestInitialize
TestClass2: MyTestCase2
TestClass2: TestCleanup
TestClass1: ClassCleanup
TestClass2: ClassCleanup
AssemblyCleanup

...this doesn't mean that TestClass1's ClassCleanup executes immediately after the last test case in the class! In fact, it waits until all test cases are executed, and the executestogether with TestClass2's ClassCleanup.

This surprised me at first, but that was obviously only because Ihadn't really thought it through: Since tests are, in principle,unordered, there's not guarantee that all tests in TestClass1 areexecuted in immediate succession. Theoretically, the execution enginemay pick a test case from TestClass1, then one from TestClass2, thenanother from TestClass1, etc. Since that is the case, there's noguarantee that all tests from one test class have been executed beforea new test class is initialized, and thusly, all ClassCleanup methodsmay as well be deferred until all test cases have been executed.

不幸的是,如果这对您不起作用,您将不得不查看有序测试或不同的单元测试框架。

关于c# - 如何在每节课后运行 ClassCleanup (MSTest)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24117139/

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