为什么一个ClassInitialize的装饰方法,让我所有的测试失败? [英] Why is a ClassInitialize decorated method making all my tests fail?

查看:365
本文介绍了为什么一个ClassInitialize的装饰方法,让我所有的测试失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,从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.

这篇关于为什么一个ClassInitialize的装饰方法,让我所有的测试失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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