MSTest:同一 TestClass 内测试的执行顺序
使用 MSTest 执行测试时,我需要强制执行 TestClass
顺序。
每个类中的 TestClasses
和测试的顺序可以是随机的,但 MSTest 在执行完 ClassInitialize
之前不应从另一个 TestClass
中选择测试、类中的所有测试以及 ClassCleanup
。
我有全局 AssemblyInitialize
和 AssemblyCleanup
,因此以下代码不起作用,因为它会为每个测试初始化程序集:
MSTest.exe /testcontainer:MyUnitTests.dll /resultsfile:report.trx /test:TestClass1 /test:TestClass2
I need to enforce TestClass
order when executing tests with MSTest.
The order of TestClasses
and tests within each class can be random, but MSTest should not pick a test from another TestClass
until it is done executing ClassInitialize
, all tests in the class, and ClassCleanup
.
I have global AssemblyInitialize
and AssemblyCleanup
, therefore the following does not work, because it initializes the assembly for each test:
MSTest.exe /testcontainer:MyUnitTests.dll /resultsfile:report.trx /test:TestClass1 /test:TestClass2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我问了一个类似的问题这里 ,尽管这与测试类执行顺序无关。如果排序的原因是为了可以设置/维护某种状态,那么对测试进行排序可能会导致它们变得脆弱。如果您的测试就是这种情况,我建议您以与顺序无关的方式编写它们。
关于程序集级代码的问题,
AssemblyInitialize
和AssemblyCleanup
的解决方法如下:基本上,您可以强制触发程序集级方法只有一次。
I asked a similar question here, though it was not about test class execution order. Ordering tests can cause them to be brittle if the reason for the ordering is so that some sort of state can be setup/maintained. If this is the case with your tests, I would suggest instead writing them in a way that would be order-agnostic.
As regards your problem with the assembly-level code, a work around for the
AssemblyInitialize
andAssemblyCleanup
can be as follows:Basically, you can force the assembly-level methods to fire only once.