MS Test V2-失败时初始化类的行为

问题描述 投票:0回答:1

我将VS2019与MS Test V2框架和测试适配器一起使用。我对ClassInitialize的预期行为不太确定。

当我从一个测试类运行一组测试时:

  • 在MS Test V1.3中,如果ClassInitialize失败,则所有测试都报告为失败。
  • 在MS Test V2中,似乎只有第一个测试被报告为失败,其他所有报告为通过。

当我独立运行测试(一次运行一次)时,每个测试都报告为失败。因此,我认为这可能是并行执行的问题,但是即使在禁用并行执行之后,其行为在MS Test v2中也相同。

是否打算让ClassInitialize失败不会使所有测试都失败?

c# mstest
1个回答
0
投票

FWIW我可以使用MSTest 2.0.0通过.NET Core UnitTestProject再现此行为。

这是测试班的代码:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace UnitTestProject2
{
    [TestClass]
    public class UnitTest1
    {
        [ClassInitialize]
        public static void ClassInit(TestContext tc)
        {
            throw new Exception("DIE IN INITIALIZE");
        }

        [TestMethod] public void TestMethod1() { }
        [TestMethod] public void TestMethod2() { }
        [TestMethod] public void TestMethod3() { }
    }
}

事实上,如果您在TestExplorer中查看结果,您会看到只有TestMethod1被标记为Failed,而TestMethod2TestMethod3被标记为Successful

与控制台相同的结果。

❯ dotnet test
Test run for C:\source\stuff\UnitTestProject1\UnitTestProject2\bin\Debug\netcoreapp3.1\UnitTestProject2.dll(.NETCoreApp,Version=v3.1)
Microsoft (R) Test Execution Command Line Tool Version 16.3.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...

A total of 1 test files matched the specified pattern.
  X TestMethod1
  Error Message:
   Class Initialization method UnitTestProject2.UnitTest1.ClassInit threw exception. System.Exception: System.Exception: DIE IN INITIALIZE.
  Stack Trace:
     at UnitTestProject2.UnitTest1.ClassInit(TestContext tc) in C:\source\stuff\UnitTestProject1\UnitTestProject2\UnitTest1.cs:line 12


Test Run Failed.
Total tests: 3
     Passed: 2
     Failed: 1
 Total time: 0,8928 Seconds

有趣的是,如果将一些Console.WriteLine语句添加到测试方法中,则可以看到它们确实已被执行。因此,这似乎并非仅是输出中的不幸事故。

© www.soinside.com 2019 - 2024. All rights reserved.