3

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()
        {

        }

    }
}

Abhi
  • 61
  • 7
  • Instead of having logic to skip one tests, create two tests put them into different traits. All test runners support running/skipping tests based on trait. – Fabio Dec 04 '19 at 11:05
  • :) or you always can do inside the test `if (i_want_to_skip_this_test == true) return;` – Fabio Dec 04 '19 at 11:05
  • Does this answer your question? [How do I skip specific tests in xUnit based on current platform](https://stackoverflow.com/questions/4421328/how-do-i-skip-specific-tests-in-xunit-based-on-current-platform) – Ruben Bartelink Dec 04 '19 at 19:32

1 Answers1

-1

You can do something like this:


public class MyTestClass
{
    private const string SomeCondition = "false"

    [Fact(Skip=SomeCondition)]
    void MyTestMethod()
    {

    }
}
TomDane
  • 1,010
  • 10
  • 25