【问题标题】:AssemblyInitialize method doesnt run before testsAssemblyInitialize 方法在测试之前不运行
【发布时间】:2017-05-24 08:43:34
【问题描述】:

我正在使用 MsTest V2 框架进行测试。 我有测试自动化框架 (TAF) 项目和带有测试的项目。 测试项目继承自 TAF,仅包含测试。 在 TAF 中,我有一个包含应该在所有测试之前运行但它根本不起作用的方法的类。 顺便说一下,BeforeTest 方法工作正常。

public class TestBase
{
    [AssemblyInitialize]
    public static void BeforeClass(TestContext tc)
    {
        Console.WriteLine("Before all tests");
    }
    [TestInitialize]
    public void BeforeTest()
    {
        Console.WriteLine("Before each test");
    }
}


[TestClass]
public class FirstTest : TestBase
{
    [TestMethod]
    public void FailedTest()
    {
        Assert.IsTrue(false,"ASDASDASD");
    }
}

如果我将“AssemblyInitialize”方法用于测试项目,那么它就可以工作。

我做错了什么?

【问题讨论】:

    标签: c# unit-testing mstest


    【解决方案1】:

    只需将[TestClass] 放到您的TestBase 上:

    [TestClass]
    public class TestBase
    {
        [AssemblyInitialize]
        public static void BeforeClass(TestContext tc)
        {
           Console.WriteLine("Before all tests");
        }
    
        [TestInitialize]
        public void BeforeTest()
        {
               Console.WriteLine("Before each test");
        }
    }
    

    【讨论】:

    • 因为 TestBase 类和 FirstTest 类有不同的程序集,所以它不起作用。
    • 我想我找到了答案。 MsTest 框架有问题github.com/Microsoft/testfx/issues/143
    • 我还发现在 [AssemblyInitialize] 和 [TestInitialize] 方法中使用断言很有用。如果断言失败,则不会启动相应的测试,而是将其标记为失败。
    猜你喜欢
    • 2013-02-27
    • 2016-04-26
    • 2020-03-04
    • 2011-09-19
    • 2023-04-11
    • 2013-07-21
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多