[英]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.cs , CarLotMaxSteps.cs和CarTypeSteps.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.Main
是static
,因此您的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.