单元测试在“全部运行”上失败,但在“全部选定”下运行时失败 [英] Unit Test fails on Run All but not on Run Selected Tests with all selected

查看:69
本文介绍了单元测试在“全部运行”上失败,但在“全部选定”下运行时失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些奇怪的行为。如果我在测试资源管理器中单击全部运行,则我的1个单元测试失败,但是如果我选择了所有测试并单击运行所选的测试,则该单元测试通过。



失败的测试抛出反射错误: System.Reflection.TargetException:非静态方法需要一个目标。在dll代码中定义的类型上我正在测试。这个类似乎没有什么奇怪的-dll中定义了很多其他类,反射很高兴。我在下面包括了测试堆栈跟踪。



NB这是一个复杂的测试-它读取输入& .xlsx文件的预期答案,使用xlsx中的数据填充LocalDb,使用LocalDB中的数据执行计算,然后将计算结果与预期值进行比较。但是,正如我所说的那样,当我运行所有测试(使用全选>运行选定的测试)时,它是可行的。



全部运行有何不同?任何见识将不胜感激。



我尝试了一次干净的整修,没有运气。捕获和记录反射错误表明,我尝试对该类型访问的每个属性都抛出GetValue调用-但仅当由全部运行运行且仅对此一种类型运行时? (如果我发现了错误,则所有GetValues都会在所有其他类型上都成功)。



堆栈跟踪

 测试名称:IT_CheckCashOnly1DepositOutputValues 
测试全名:Lib.AE.Tests.Integration.CalculationTests.IT_CheckCashOnly1DepositOutputValues
测试源:c:\netreturn.co.za\Main\ NetReturn\Lib.AE.Tests\IntegrationTests\CalculationTest.cs:第23行
测试结果:失败
测试持续时间:0:00:00.1661906

结果消息:
测试方法Lib.AE.Tests.Integration.CalculationTests.IT_CheckCashOnly1DepositOutputValues引发异常:
System.Reflection.TargetException:非静态方法需要目标。
结果StackTrace:System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)的
System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj,BindingFlags invokeAttr,Binder活页夹,Object []参数的
, CultureInfo文化)System.Reflection.RuntimeMethodInfo.Invoke(对象obj,BindingFlags invokeAttr,活页夹活页夹,Object []参数,CultureInfo文化)System.Reflection.RuntimePropertyInfo.GetValue(Object obj,BindingFlags invokeAttr,活页夹活页夹,Object []索引,CultureInfo文化)在System.Reflection.RuntimePropertyInfo.GetValue(Object obj,Object []索引)
在Lib.AE.Xlsx.XlsxHelper.Compare [T ](在C:,netreturn.co.za\Main\NetReturn\Lib.AE\Xlsx\XlsxHelper.cs中的(预期的T,已计算的T,ExcelWorksheet ws,Int32 r,Int32 colStart,布尔值和有效值) :位于Lib.AE.Xlsx.XlsxWorkSheet_SharePNL.CompareXlsx的第101行
,位于c:pnetreturn.co.za\Main\NetRet中(ExcelPackage pck,列出1个预期Xlsx,ValuationCalculation calc) urn\Lib.AE\Xlsx\XlsxSharePNL.cs:行143
在c:\netreturn.co.za中的Lib.AE.Tests.Integration.CalculationTests.CheckCalculationResults(String xlsxDocToLoad,WorkSheets testingScenarios) \Main\NetReturn\Lib.AE.Tests\IntegrationTests\CalculationTest.cs:c:\netreturn.co中位于Lib.AE.Tests.Integration.CalculationTests.IT_CheckCashOnly1DepositOutputValues()的第64行
.za\Main\NetReturn\Lib.AE.Tests\IntegrationTests\CalculationTest.cs:第23行






解决方案



所以这原来是我的问题-我的单元测试与另一个单元测试共享状态,并且(b)订单问题。请注意,TestExplorer将以哪种顺序运行测试并不明显。我创建了一个新的UnitTestProject,其中包含2个UnitTest .cs文件和三个TestMethod,即:



UnitTest1。 cs

  [TestMethod] 
public void ONE_AAA(){}

[TestMethod]
public void ONE_BBB(){}

[TestMethod]
public void ONE_CCC(){}

UnitTest2.cs

  [TestMethod] 
public void TWO_CCC( ){}

[TestMethod]
公共无效TWO_BBB(){}

[TestMethod]
公共无效TWO_AAA(){}

然后通过两种方法运行这些测试,即(1)全部运行(2)选择全部并运行选定的测试,以及记录了TestExplorer启动测试的顺序。运行选定测试的结果相当不直观:

 -运行所有
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_CCB
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

-全选>运行选定的
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信息TestCleanup:ONE_AAA


解决方案

我过去因运行多个单元测试而陷入困境,因为测试的运行顺序可能与在测试类中声明的顺序不同,实际上可能是按照测试方法名称的顺序。例如如果我有

  [测试] 
public void PreviousTest()
{
}

[测试]
public void LaterTest()
{
}

然后 LaterTest 首先运行,因为其名称按字母顺序显示在 PreviousTest 之前。 / p>

这是否无关紧要,如果您的所有测试都完全独立,但是如果它们正在修改共享资源,那么如果您期望 LaterTest 的更改不会对 PreviousTest 产生任何影响,因为它被宣布为第二名。


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.

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.

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).

What is different about Run All? Any insights would be appreciated.

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).

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.:

UnitTest1.cs

    [TestMethod]
    public void ONE_AAA() {}

    [TestMethod]
    public void ONE_BBB() {}

    [TestMethod]
    public void ONE_CCC() {}

UnitTest2.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:

-- 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

解决方案

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 LaterTest gets run first as its name appears before PreviousTest when ordered alphabetically.

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 PreviousTest due to it being declared second.

这篇关于单元测试在“全部运行”上失败,但在“全部选定”下运行时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆