.net 单元测试在 Run All 上失败,但在 Run Selected Tests with all selected 上失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14349512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unit Test fails on Run All but not on Run Selected Tests with all selected
提问by Ilan
I'm getting some weird behavior. If I click "Run All" in Test Explorer then 1 of my unit tests fails, but if I select all tests and click "Run Selected Tests" then the unit test passes.
我有一些奇怪的行为。如果我在测试资源管理器中单击“全部运行”,则我的 1 个单元测试失败,但如果我选择所有测试并单击“运行选定的测试”,则单元测试通过。
The test that is failing is throwing a reflection error: System.Reflection.TargetException: Non-static method requires a target.on a type which is defined in the dll code which I'm testing. There doesn't appear to be anything odd with the class - there are plenty of other classes defined in the dll which reflection is happy with. I've included the test stack trace below.
失败的测试抛出了一个反射错误:System.Reflection.TargetException: Non-static method requires a target.在我正在测试的 dll 代码中定义的类型上。这个类似乎没有什么奇怪的 - 在 dll 中定义了很多其他类,反射很满意。我在下面包含了测试堆栈跟踪。
NB It is a complex test - it reads inputs & expected answers from an .xlsx file, populates a LocalDb with data from the xlsx, performs a calculation using the data in the LocalDB and then compares the calculated to expected values. However as I say it is working and works when I run all tests (using select all > Run Selected Test).
注意这是一个复杂的测试 - 它从 .xlsx 文件中读取输入和预期答案,使用来自 xlsx 的数据填充 LocalDb,使用 LocalDB 中的数据执行计算,然后将计算结果与预期值进行比较。然而,正如我所说,当我运行所有测试(使用全选 > 运行所选测试)时,它正在工作并且工作。
What is different about Run All? Any insights would be appreciated.
Run All 有什么不同?任何见解将不胜感激。
I have tried a clean and rebuild with no luck. Catching and logging reflection error indicates that the GetValue call is throwing for every property I attempt to access on the type - but only when run by "Run All" and only on this one type? (if I catch the error then all GetValues succeed on all other types).
我尝试了清理和重建,但没有运气。捕获和记录反射错误表明 GetValue 调用正在为我尝试在该类型上访问的每个属性抛出 - 但仅当通过“全部运行”运行且仅在这种类型上运行时?(如果我发现错误,那么所有其他类型的所有 GetValues 都会成功)。
Stack Trace
堆栈跟踪
Test Name: IT_CheckCashOnly1DepositOutputValues
Test FullName: Lib.AE.Tests.Integration.CalculationTests.IT_CheckCashOnly1DepositOutputValues
Test Source: c:\netreturn.co.za\Main\NetReturn\Lib.AE.Tests\IntegrationTests\CalculationTest.cs : line 23
Test Outcome: Failed
Test Duration: 0:00:00.1661906
Result Message:
Test method Lib.AE.Tests.Integration.CalculationTests.IT_CheckCashOnly1DepositOutputValues threw exception:
System.Reflection.TargetException: Non-static method requires a target.
Result StackTrace:
at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
at Lib.AE.Xlsx.XlsxHelper.Compare[T](T expected, T calculated, ExcelWorksheet ws, Int32 r, Int32 colStart, Boolean& valid) in c:\netreturn.co.za\Main\NetReturn\Lib.AE\Xlsx\XlsxHelper.cs:line 101
at Lib.AE.Xlsx.XlsxWorkSheet_SharePNL.CompareXlsx(ExcelPackage pck, List`1 expectedXlsx, ValuationCalculation calc) in c:\netreturn.co.za\Main\NetReturn\Lib.AE\Xlsx\XlsxSharePNL.cs:line 143
at Lib.AE.Tests.Integration.CalculationTests.CheckCalculationResults(String xlsxDocToLoad, WorkSheets testingScenarios) in c:\netreturn.co.za\Main\NetReturn\Lib.AE.Tests\IntegrationTests\CalculationTest.cs:line 64
at Lib.AE.Tests.Integration.CalculationTests.IT_CheckCashOnly1DepositOutputValues() in c:\netreturn.co.za\Main\NetReturn\Lib.AE.Tests\IntegrationTests\CalculationTest.cs:line 23
Solution
解决方案
So this turned out to be (a) my problem - my unit test shared state with another unit test and (b) an order problem. Be aware that it's not obvious which order TestExplorer will run your tests in. I created a new UnitTestProject with 2 x UnitTest .cs files and three TestMethods each i.e.:
所以这结果是 (a) 我的问题 - 我的单元测试与另一个单元测试共享状态和 (b) 订单问题。请注意,TestExplorer 将运行您的测试的顺序并不明显。我创建了一个新的 UnitTestProject,其中包含 2 个 UnitTest .cs 文件和三个 TestMethods,即:
UnitTest1.cs
单元测试1.cs
[TestMethod]
public void ONE_AAA() {}
[TestMethod]
public void ONE_BBB() {}
[TestMethod]
public void ONE_CCC() {}
UnitTest2.cs
单元测试2.cs
[TestMethod]
public void TWO_CCC() {}
[TestMethod]
public void TWO_BBB() {}
[TestMethod]
public void TWO_AAA() {}
Then ran these tests via two methods i.e. (1) Run All (2) Select all and Run Selected Tests, and logged the order in which the tests were started by TestExplorer. The results for Run Selected Tests are fairly unintuitive:
然后通过两种方法运行这些测试,即 (1) 全部运行 (2) 全选和运行选定的测试,并记录 TestExplorer 启动测试的顺序。Run Selected Tests 的结果相当不直观:
-- Run All
2013-01-16 11:53:47.4062 INFO TestInitialize: ONE_AAA
2013-01-16 11:53:47.4122 INFO TestCleanup: ONE_AAA
2013-01-16 11:53:47.4122 INFO TestInitialize: ONE_BBB
2013-01-16 11:53:47.4122 INFO TestCleanup: ONE_BBB
2013-01-16 11:53:47.4122 INFO TestInitialize: ONE_CCC
2013-01-16 11:53:47.4282 INFO TestCleanup: ONE_CCC
2013-01-16 11:53:47.4282 INFO TestInitialize: TWO_CCC
2013-01-16 11:53:47.4282 INFO TestCleanup: TWO_CCC
2013-01-16 11:53:47.4282 INFO TestInitialize: TWO_BBB
2013-01-16 11:53:47.4282 INFO TestCleanup: TWO_BBB
2013-01-16 11:53:47.4282 INFO TestInitialize: TWO_AAA
2013-01-16 11:53:47.4282 INFO TestCleanup: TWO_AAA
-- Select All > Run Selected
2013-01-16 11:55:26.0139 INFO TestInitialize: TWO_BBB
2013-01-16 11:55:26.0139 INFO TestCleanup: TWO_BBB
2013-01-16 11:55:26.0249 INFO TestInitialize: ONE_BBB
2013-01-16 11:55:26.0249 INFO TestCleanup: ONE_BBB
2013-01-16 11:55:26.0249 INFO TestInitialize: TWO_AAA
2013-01-16 11:55:26.0249 INFO TestCleanup: TWO_AAA
2013-01-16 11:55:26.0249 INFO TestInitialize: TWO_CCC
2013-01-16 11:55:26.0249 INFO TestCleanup: TWO_CCC
2013-01-16 11:55:26.0249 INFO TestInitialize: ONE_CCC
2013-01-16 11:55:26.0249 INFO TestCleanup: ONE_CCC
2013-01-16 11:55:26.0249 INFO TestInitialize: ONE_AAA
2013-01-16 11:55:26.0249 INFO TestCleanup: ONE_AAA
采纳答案by DeanOC
I have been caught out in the past with running multiple unit tests in that the order in which the tests run may not be in the order in which they are declared in the test class, and may in fact be in order of test method name. e.g. if I have
过去我一直在运行多个单元测试,因为测试运行的顺序可能不是它们在测试类中声明的顺序,实际上可能是测试方法名称的顺序。例如,如果我有
[Test]
public void PreviousTest()
{
}
[Test]
public void LaterTest()
{
}
Then LaterTestgets run first as its name appears before PreviousTestwhen ordered alphabetically.
然后LaterTest首先运行,因为PreviousTest按字母顺序排列时,它的名称出现在前面。
This won't matter if all of your tests are entirely independent, but if they are modifying shared resources then you may get unusual behaviour if you were expecting that LaterTest's changes wouldn't have any effect on PreviousTestdue to it being declared second.
如果您的所有测试都是完全独立的,这并不重要,但是如果它们正在修改共享资源,那么您可能会出现异常行为,如果您期望由于它被第二次声明而LaterTest不会对其产生任何影响PreviousTest。
回答by Gilad w
Make sure your members are being initialized in the setup scope, otherwise they can be reused.
确保您的成员在设置范围内被初始化,否则他们可以被重用。
[TestFixture]
public class RegistrationInteractorTests
{
private IRegistrationService _registrationService;
private IRegistrationValidationService _registrationValidationService;
......
[SetUp]
public void Init()
{
_registrationService = A.Fake<IRegistrationService>();
_registrationValidationService = A.Fake<IRegistrationValidationService>();
}
....
....
}
}
// The wrong way would be like this:
// 错误的方法是这样的:
[TestFixture]
public class RegistrationInteractorTests
{
protected readonly IRegistrationService _registrationService = A.Fake<IRegistrationService>();
protected readonly IRegistrationValidationService _registrationValidationService =
A.Fake<IRegistrationValidationService>();
[SetUp]
public void Init()
{
}
回答by VJPPaz
We tried to investigate this issue for a week now. Make sure that all your TEST Projects are using the same version of DLLs, especially the Nuget Packages.
我们试图调查这个问题一个星期了。确保所有 TEST 项目都使用相同版本的 DLL,尤其是 Nuget 包。
The thing is that, when you execute the "Run All", all the used libraries in all your TEST Projects are placed in one folder (something like this MyApp\TestResults\Deploy_user1 2018-11-20 15_52_15\Out). and because of these Dll libraries conflicted.
问题是,当您执行“全部运行”时,所有 TEST 项目中使用的所有库都放在一个文件夹中(类似这样MyApp\TestResults\Deploy_user1 2018-11-20 15_52_15\Out)。并且因为这些 Dll 库发生冲突。
回答by Codeman
Your [TestInitialize]method runs before every single test. Remove any single-test-dependent code from it and put it in the individual test.
您的[TestInitialize]方法在每次测试之前运行。从中删除任何与单个测试相关的代码并将其放入单个测试中。

