Skip the SetUp method only for a particular test in NUnit?

后端 未结 6 1851
时光说笑
时光说笑 2021-01-01 19:47

I have a test where I do not need to run the SetUp method (attributed with [SetUp]) before running the test. I need the SetUp method t

6条回答
  •  一个人的身影
    2021-01-01 20:05

    Here is the code that I suggest for accomplishing what you want.

    public const string SKIP_SETUP = "SkipSetup";
    

    Now add the following method:

    private static bool CheckForSkipSetup()
    {               
        var categories = TestContext.CurrentContext.Test?.Properties["Category"];
    
        bool skipSetup = categories != null && categories.Contains("SkipSetup");
        return skipSetup;
    }
    

    Now check the condition as follows:

    [SetUp]
    public async Task Dosomething()
    {
        if (!CheckForSkipSetup())
        {
    
        }
    }
    

    Use these in test cases as follows:

    [Test]
    [Category(SKIP_SETUP)]
    public async Task Mywork()
    {
    
    }
    

提交回复
热议问题