单击测试在"全部运行"上失败,但未在"运行所选测试"中全部选中

Ila*_*lan 30 .net unit-testing exception-handling visual-studio-2012

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

失败的测试是抛出一个反射错误:System.Reflection.TargetException: Non-static method requires a target.在我正在测试的DLL代码中定义的类型.这个类似乎没有什么奇怪的东西 - 在dll中定义了很多其他类,反映很满意.我在下面列出了测试堆栈跟踪.

NB这是一个复杂的测试 - 它从.xlsx文件读取输入和预期答案,使用xlsx中的数据填充LocalDb,使用LocalDB中的数据执行计算,然后比较计算值和预期值.但是正如我所说,当我运行所有测试时(使用select all> Run Selected Test),它正在工作并且有效.

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
Run Code Online (Sandbox Code Playgroud)

所以这结果是(a)我的问题 - 我的单元测试共享状态与另一个单元测试和(b)订单问题.请注意,TestExplorer将在哪个命令中运行您的测试并不明显.我创建了一个新的UnitTestProject,其中包含2个UnitTest .cs文件和3个TestMethods,即:

的UnitTest1.cs

    [TestMethod]
    public void ONE_AAA() {}

    [TestMethod]
    public void ONE_BBB() {}

    [TestMethod]
    public void ONE_CCC() {}
Run Code Online (Sandbox Code Playgroud)

UnitTest2.cs

    [TestMethod]
    public void TWO_CCC() {}

    [TestMethod]
    public void TWO_BBB() {}

    [TestMethod]
    public void TWO_AAA() {}
Run Code Online (Sandbox Code Playgroud)

然后通过两种方法运行这些测试,即(1)全部运行(2)选择全部并运行选定的测试,并记录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
Run Code Online (Sandbox Code Playgroud)

Dea*_*nOC 13

过去我遇到过运行多个单元测试的问题,测试运行的顺序可能与测试类中声明的顺序不同,实际上可能是测试方法名称的顺序.例如,如果我有

[Test]
public void PreviousTest()
{
}

[Test]
public void LaterTest()
{
}
Run Code Online (Sandbox Code Playgroud)

然后LaterTestPreviousTest按字母顺序排序之前,首先运行它的名称.

如果您的所有测试都是完全独立的,那么无关紧要,但如果他们正在修改共享资源,那么如果您预期LaterTest更改不会PreviousTest因为它被声明为第二,那么您可能会遇到异常行为.


小智 6

确保在设置范围内初始化您的成员,否则可以重复使用它们.

[TestFixture]
public class RegistrationInteractorTests
{
    private  IRegistrationService _registrationService;
    private  IRegistrationValidationService _registrationValidationService;
  ......

    [SetUp]
    public void Init()
    {
        _registrationService = A.Fake<IRegistrationService>();
        _registrationValidationService = A.Fake<IRegistrationValidationService>();
    }
Run Code Online (Sandbox Code Playgroud)

....

}

//错误的方式是这样的:

 [TestFixture]
    public class RegistrationInteractorTests
    {
        protected readonly IRegistrationService _registrationService = A.Fake<IRegistrationService>();

        protected readonly IRegistrationValidationService _registrationValidationService =
            A.Fake<IRegistrationValidationService>();



        [SetUp]
        public void Init()
        {

        }
Run Code Online (Sandbox Code Playgroud)