简体   繁体   English

NUnit 3:禁止并行运行测试

[英]NUnit 3: Forbid tests to run in parallel

I have the latest NUnit(3.2.0) installed and I have all my tests run in parallel.我安装了最新的 NUnit(3.2.0),并且我的所有测试都并行运行。 It might look like desirable behavior but I didn't ask for it and actually it broke some of my tests.它可能看起来像是理想的行为,但我没有要求它,实际上它破坏了我的一些测试。 I have some initialization in [OneTimeSetUp] which is thread-dependent and it seems I can't do anything to force NUnit to run my tests sequentially.我在[OneTimeSetUp]有一些依赖于线程的初始化,似乎我无法强制 NUnit 按顺序运行我的测试。 I've read the documentation and it states that by default tests aren't run in parallel but in fact they are!我已经阅读了文档,它指出默认情况下测试不是并行运行的,但实际上它们是!

Moreover, I've tried to add the following attribute: [assembly: Parallelizable(ParallelScope.None)] — no luck.此外,我尝试添加以下属性: [assembly: Parallelizable(ParallelScope.None)] — 没有运气。

Does anybody know how to change this behavior?有人知道如何改变这种行为吗?

PS I run it with ReSharper but also tried with MSVS add-in. PS 我使用 ReSharper 运行它,但也尝试使用 MSVS 插件。


UPD: I'm using MVVM Light DispatcherHelper.Initialize() (inside [OneTimeSetUp] ) to store the dispatcher object which is later used by a couple of tests. UPD:我正在使用 MVVM Light DispatcherHelper.Initialize() (在[OneTimeSetUp] )来存储调度程序对象,该对象稍后由几个测试使用。 If threads are different(between a test and the setup method) then the action under test gets executed asynchronously and my tests fail.如果线程不同(在测试和设置方法之间),则被测操作将异步执行并且我的测试失败。

I've checked the thread ids in different tests and they all are different.我检查了不同测试中的线程 ID,它们都不同。


UPD2: Excerpt from the documentation: UPD2:摘自文档:

The NUnit 3.0 framework can run tests in parallel within an assembly . NUnit 3.0 框架可以在程序集中并行运行测试 This is a completely separate facility from Engine Parallel Test Execution, although it is possible to use both in the same test run.这是一个完全独立于引擎并行测试执行的工具,尽管可以在同一个测试运行中使用两者。

By default, no parallel execution takes place .默认情况下,不会发生并行执行 Attributes are used to indicate which tests may run in parallel and how they relate to other tests.属性用于指示哪些测试可以并行运行以及它们与其他测试的关系。

If it doesn't mean the tests within an assembly should not be run in parallel until explicitly specified then what does it mean?如果这并不意味着在明确指定之前不应并行运行程序集中的测试,那么这意味着什么? And why [assembly: Parallelizable(ParallelScope.None)] has no effect on the tests parallel execution?为什么[assembly: Parallelizable(ParallelScope.None)]对测试并行执行没有影响?


UPD3: Answer to the question might be found below but if you are stuck(as I was) with the DispatcherHelper.Initialize() you just need to remove this initialization from the OneTimeSetUp and put the following lines in every test that uses a dispatcher: UPD3:问题的答案可能会在下面找到,但是如果您(像我一样)被DispatcherHelper.Initialize()卡住了,您只需要从OneTimeSetUp删除此初始化,并在每个使用调度程序的测试中添加以下行:

DispatcherHelper.Reset();
DispatcherHelper.Initialize();

NUnit does not guarantee that all of your tests will run on the same thread, so the observation that your tests are running on different threads does not mean they are running in parallel. NUnit 不保证您的所有测试都在同一线程上运行,因此观察到您的测试在不同线程上运行并不意味着它们是并行运行的。

The documentation only states that tests will run sequentially or in parallel.该文档仅说明测试将按顺序或并行运行。 You may construe that this means they run on the same thread, but there are many reasons that the internal implementation might require tests to run on different threads.您可能会认为这意味着它们在同一个线程上运行,但是内部实现可能需要在不同线程上运行测试的原因有很多。 Timeout is an example, where we spawn a thread and kill it if the test times out, but there are many others. Timeout 是一个例子,如果测试超时,我们会生成一个线程并杀死它,但还有很多其他的。

Parallel test runs are new to NUnit 3, so the internal implementation changed from NUnit 2. An attribute that forces all tests within a thread to run on the same thread might be useful, so feel free to submit an enhancement request .并行测试运行是 NUnit 3 的新功能,因此内部实现从 NUnit 2 更改。强制一个线程内的所有测试在同一线程上运行的属性可能很有用,因此请随时提交增强请求

Sorry, I am unfamiliar with MVVM Light, so I can't suggest ways to marshal back to the OneTimeSetup thread.抱歉,我不熟悉 MVVM Light,因此我无法建议将回送至 OneTimeSetup 线程的方法。

Update - Since this is a common usage with web and async, the NUnit team has decided to provide an attribute that will demand tests be run on the same thread as the fixture's OneTimeSetup .更新- 由于这是 web 和 async 的常见用法,NUnit 团队决定提供一个属性,该属性将要求在与夹具的OneTimeSetup相同的线程上运行测试。 This will be in the next release, either 3.4, or in a hotfix 3.2.1 release.这将出现在下一个版本 3.4 或修补程序 3.2.1 版本中。 If you want to track progress, see the issue and the pull request .如果您想跟踪进度,请参阅问题拉取请求

Update 2 - You can now add SingleThreadedAttribute to a TestFixture to indicate to the runner that the OneTimeSetUp , OneTimeTearDown and all the child tests must run on the same thread.更新 2 - 您现在可以将SingleThreadedAttribute添加到 TestFixture 以向运行程序指示OneTimeSetUpOneTimeTearDown和所有子测试必须在同一线程上运行。

您可以通过添加[NonParallelizable]属性来阻止测试并行运行,该属性可以在测试、类和程序集级别添加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM