NUnit 3:禁止并行运行测试。

时间:2021-08-20 15:40:49

I have the latest NUnit(3.2.0) installed and I have all my tests run in parallel. 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. I've read the documentation and it states that by default tests aren't run in parallel but in fact they are!

我安装了最新的NUnit(3.2.0),所有的测试都是并行运行的。它看起来可能是可取的行为但我没有要求它实际上它破坏了我的一些测试。我在[OneTimeSetUp]中有一些初始化,这是线程依赖的,似乎我不能强制NUnit按顺序运行我的测试。我已经阅读了文档,它指出默认测试不是并行运行的,但实际上它们是!

Moreover, I've tried to add the following attribute: [assembly: Parallelizable(ParallelScope.None)] — no luck.

此外,我还尝试添加以下属性:[程序集:可并行化(ParallelScope.None)]——没有运气。

Does anybody know how to change this behavior?

有人知道如何改变这种行为吗?

P.S. I run it with ReSharper but also tried with MSVS add-in.

我用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. If threads are different(between a test and the setup method) then the action under test gets executed asynchronously and my tests fail.

UPD:我正在使用MVVM Light DispatcherHelper.Initialize()(在[OneTimeSetUp]中)存储dispatcher对象,稍后进行两个测试。如果线程是不同的(在测试和设置方法之间),那么在测试下的操作就会异步执行,而我的测试失败了。

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. This is a completely separate facility from Engine Parallel Test Execution, although it is possible to use both in the same test run.

NUnit 3.0框架可以在程序集内并行运行测试。这是一个完全独立于引擎并行测试执行的工具,尽管它可以在相同的测试运行中使用。

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?

如果不意味着程序集内的测试不应该并行运行,直到显式指定,那么它意味着什么?以及为什么[程序集:并行化(并行化)]对并行执行的测试没有影响?


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()中,您只需要从一个timesetup中删除这个初始化,并在每个使用dispatcher的测试中放入以下的行:

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

2 个解决方案

#1


13  

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.

文档只说明测试将按顺序运行或并行运行。您可能会解释,这意味着它们在同一个线程上运行,但是内部实现可能需要测试在不同的线程上运行的原因有很多。超时就是一个例子,如果测试超时,我们将生成一个线程并将其杀死,但是还有许多其他线程。

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光,所以我不能建议把它编组到一个分时的线程。

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. This will be in the next release, either 3.4, or in a hotfix 3.2.1 release. If you want to track progress, see the issue and the pull request.

更新——由于这是web和async的常见用法,NUnit团队已经决定提供一个属性,该属性将要求测试在与fixture的OneTimeSetup相同的线程上运行。这将在下一个版本中发布,要么是3.4,要么是在一个hotfix 3.2.1版本中。如果您想跟踪进度,请查看问题和pull请求。

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中,以向runner表示一个时间单元,一个timeteardown和所有的子测试必须在同一个线程上运行。

#2


0  

You can prevent tests from running in parallel by adding the [NonParallelizable] attribute, which can be added in test, class and assembly level.

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

#1


13  

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.

文档只说明测试将按顺序运行或并行运行。您可能会解释,这意味着它们在同一个线程上运行,但是内部实现可能需要测试在不同的线程上运行的原因有很多。超时就是一个例子,如果测试超时,我们将生成一个线程并将其杀死,但是还有许多其他线程。

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光,所以我不能建议把它编组到一个分时的线程。

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. This will be in the next release, either 3.4, or in a hotfix 3.2.1 release. If you want to track progress, see the issue and the pull request.

更新——由于这是web和async的常见用法,NUnit团队已经决定提供一个属性,该属性将要求测试在与fixture的OneTimeSetup相同的线程上运行。这将在下一个版本中发布,要么是3.4,要么是在一个hotfix 3.2.1版本中。如果您想跟踪进度,请查看问题和pull请求。

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中,以向runner表示一个时间单元,一个timeteardown和所有的子测试必须在同一个线程上运行。

#2


0  

You can prevent tests from running in parallel by adding the [NonParallelizable] attribute, which can be added in test, class and assembly level.

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