仅针对NUnit中的特定测试跳过SetUp方法?

Nar*_*esh 15 nunit unit-testing

我有一个测试,我不需要在运行测试之前运行该SetUp方法(归因于[SetUp]).我需要SetUp为其他测试运行该方法.

是否有可以使用的不同属性或基于非属性的方法来实现此目的?

Mik*_*ill 24

您还可以添加类别并检查设置中的类别列表:

public const string SKIP_SETUP = "SkipSetup"; 

[SetUp]
public void Setup(){
   if (!CheckForSkipSetup()){
        // Do Setup stuff
   }
}

private static bool CheckForSkipSetup() {
    ArrayList categories = TestContext.CurrentContext.Test
       .Properties["_CATEGORIES"] as ArrayList;

    bool skipSetup = categories != null && categories.Contains( SKIP_SETUP );
    return skipSetup ;
}

[Test]
[Category(SKIP_SETUP)]
public void SomeTest(){
    // your test code
}
Run Code Online (Sandbox Code Playgroud)

  • @DebendraDash'_CATEGORIES'在更新版本的NUnit中称为"类别".这是依赖库内部时很容易发生的事情. (4认同)

Mic*_*ren 17

您应该为该测试创建一个新类,它只需要设置(或缺少设置).

或者,您可以将设置代码解释为所有其他测试调用的方法,但我不建议使用此方法.