我有一些奇怪的行为。如果我在测试资源管理器中单击“全部运行”,那么我的 1 个单元测试将失败,但如果我选择所有测试并单击“运行选定的测试”,则单元测试通过。
失败的测试引发了反射错误:System.Reflection.TargetException: Non-static method requires a target.
在我正在测试的 dll 代码中定义的类型上。这个类似乎没有什么奇怪的——在 dll 中定义了许多其他类,反射很满意。我在下面包含了测试堆栈跟踪。
注意这是一个复杂的测试 - 它从 .xlsx 文件中读取输入和预期答案,使用 xlsx 中的数据填充 LocalDb,使用 LocalDB 中的数据执行计算,然后将计算出的值与预期值进行比较。但是,正如我所说的那样,当我运行所有测试(使用全选>运行选定测试)时它正在工作并且工作。
Run All 有什么不同?任何见解将不胜感激。
我尝试过干净和重建,但没有运气。捕获和记录反射错误表明 GetValue 调用正在为我尝试在该类型上访问的每个属性抛出 - 但仅在由“全部运行”运行且仅在这一类型上运行时?(如果我发现错误,那么所有 GetValues 在所有其他类型上都成功)。
堆栈跟踪
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
解决方案
所以这原来是(a)我的问题 - 我的单元测试与另一个单元测试共享状态和(b)订单问题。请注意,TestExplorer 将以哪个顺序运行您的测试并不明显。我创建了一个新的 UnitTestProject,其中包含 2 个 UnitTest .cs 文件和三个 TestMethod,即:
单元测试1.cs
[TestMethod]
public void ONE_AAA() {}
[TestMethod]
public void ONE_BBB() {}
[TestMethod]
public void ONE_CCC() {}
单元测试2.cs
[TestMethod]
public void TWO_CCC() {}
[TestMethod]
public void TWO_BBB() {}
[TestMethod]
public void TWO_AAA() {}
然后通过两种方法运行这些测试,即 (1) Run All (2) Select all 和 Run Selected Tests,并记录 TestExplorer 启动测试的顺序。运行选定测试的结果相当不直观:
-- 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