Search code examples
xunit

How do I Skip xUnit.net test based on certain bool condition in test class?


I have a need where i need to skip the test method based on certain bool condition in a class. is it possible? how can it be achieved? i have tried extending the FactAttribute but i cannot get the instance of the Test class.

my code below:

using System;
using System.Collections.Generic;
using System.Text;
using Xunit;

namespace XUnitTestProject1
{
    public class MyTestClass
    {
        bool SomeCondition;
        public MyTestClass()
        {
            SomeCondition = false;
        }

        [Fact] //I WANT TO SKIP THIS TEST AS SOMECONDITON == FALSE
        void MyTestMethod()
        {

        }

    }
}


Solution

  • You can do something like this:

    
    public class MyTestClass
    {
        private const string SomeCondition = "false"
    
        [Fact(Skip=SomeCondition)]
        void MyTestMethod()
        {
    
        }
    }