简体   繁体   English

分别运行时,三个单元测试通过,而一起运行时,只有一个通过

[英]Three Unit tests pass when run separately, only one passes when run together

I have three tests, FunnyCarSteps.cs , CarLotMaxSteps.cs , and CarTypeSteps.cs . 我有三个测试, FunnyCarSteps.csCarLotMaxSteps.csCarTypeSteps.cs When run seperatley they all pass, but when run together only CarLotMaxSteps passes. 当运行seperatley时,它们全部通过,但是一起运行时,仅CarLotMaxSteps通过。

CarTypeSteps.cs fails at line 49 with a lambda method no source available error. CarTypeSteps.cs在第49行失败,使用lambda方法,没有可用的源错误。 FunnyCarSteps.cs fails at line 19 with an index out of range error. FunnyCarSteps.cs在第19行失败,索引超出范围错误。

I am new to unit testing and don't understand what is going on. 我是单元测试的新手,不了解发生了什么。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

Since the Program.Main is static , your CarList is probably also static. 由于Program.Mainstatic ,因此您的CarList可能也是静态的。 NUnit runs tests in parallel, so each of the tests is running at nearly the same time and each is clearing the list and adding to it at the same time. NUnit并行运行测试,因此每个测试几乎同时运行,并且每个清除列表并同时添加到列表中。

That is why your tests run fine on their own, but fail when run together. 这就是为什么您的测试可以正常运行,但同时运行会失败的原因。 One starts up and starts testing, then the second comes along and changes the data that the first test is working with. 一个启动并开始测试,然后第二个出现并更改第一个测试正在使用的数据。

You need to remove static from everything in Program except for main. 您需要从Program除main之外的所有内容中删除static。 Ideally, you should refactor all of your car list code out to a new non-static class. 理想情况下,您应该将所有汽车清单代码重构为新的非静态类。 Program is for running your program, it shouldn't have anything to do with cars. 程序用于运行您的程序,它与汽车无关。 You should be creating your CarLot (or whatever) class in Main and executing it, not adding properties to Program. 您应该在Main中创建CarLot(或任何类)类并执行它,而不是向Program添加属性。

Another option is to add the attribute [Parallelizable(ParallelScope.None)] to each of your test classes. 另一个选择是将属性[Parallelizable(ParallelScope.None)]到每个测试类。 This will cause them to run one at a time. 这将导致它们一次运行一次。 This will fix the problem with your tests, but it does not fix the fact that your code is incorrect for anything more than a simple exercise. 这将解决您的测试问题,但不能解决以下事实:除了简单的练习之外,您的代码是不正确的。

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

相关问题 一种方法仅在所有测试都运行时单元测试失败,但在单独测试时通过? - One method fails unit test only when all tests are run but passes when tested individually? 单元测试在一起运行时失败,单独传递 - Unit Tests fail when run together, pass individually ViewModel 单元测试一起运行时失败,但单独运行时通过 - ViewModel unit tests fail when run together but pass when run individually 一起运行时单元测试超时,单独运行时成功吗? - Unit Tests timeout when run together, succeed when run individually? 单元测试在Visual Studio中“全部运行”但单独传递时失败 - Unit Tests Fail when “Run All” in Visual Studio but passes individually MSTest单元测试自行通过,在运行其他测试时失败 - MSTest unit test passes by itself, fails when other tests are run 为什么当我选择 Run All 时我的单元测试(大部分)失败,但当我一次只运行一个时通过? - Why do my unit tests (mostly) fail when I select Run All but then pass when I only run one at a time? 单元测试仅在maven运行时失败 - Unit tests only fail when run by maven 一起运行时测试失败,但单独通过 - Test fails when run together but passes alone 单元测试在“全部运行”时失败,但在单次运行时通过 - Unit tests fail on Run All but pass when are run single
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM