为什么一个ClassInitialize的装饰方法,让我所有的测试失败?让我、测试、方法、ClassInitialize

2023-09-04 22:57:14 作者:街角ㄨ旳夨落

据我所知,从MSDN,即 ClassInitialize 的标记将尽设置code所有测试,一次,所有测试运行之前的方法。当我有以下删节固定这样的方法,所有的测试失败。当我注释掉,他们再次通过。

  [TestClass的]
公共类AuthenticationTests
{
    [ClassInitialize]
    公共无效SetupAuth()
    {
        变种X = 0;
    }

    [测试方法]
    公共无效TestRegisterMemberInit()
    {
        Assert.IsTrue(真正的);
    }
}
 

解决方案

[ClassInitialize] 装饰方法应该是静态的,并采取类型的只有一个参数的TestContext

  [ClassInitialize]
公共静态无效SetupAuth(的TestContext上下文)
{
    变种X = 0;
}
 

事实上,如果我复制粘贴您的code到一个干净的VS项目中,TestRunner的解释正是在错误信息:

  

方法UnitTestProject1.AuthenticationTests.SetupAuth有错误的签名。该方法必须是静态的,公开的,没有返回值,应该采取类型的TestContext的一个参数。

心理测试准吗 是怎么回事

I understand, from MSDN, that ClassInitialize is to mark a method that will do setup code for all tests, once, before all tests run. When I include such a method in the abridged fixture below, all tests fail. As soon as I comment it out, they pass again.

[TestClass]
public class AuthenticationTests
{
    [ClassInitialize]
    public void SetupAuth()
    {
        var x = 0;
    }

    [TestMethod]
    public void TestRegisterMemberInit()
    {
        Assert.IsTrue(true);
    }
}

解决方案

The [ClassInitialize] decorated method should be static and take exactly one parameter of type TestContext:

[ClassInitialize]
public static void SetupAuth(TestContext context)
{
    var x = 0;
}

In fact, if I copy-paste your code into a clean VS project, the testrunner explains exactly that in the error message:

Method UnitTestProject1.AuthenticationTests.SetupAuth has wrong signature. The method must be static, public, does not return a value and should take a single parameter of type TestContext.