Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically ignore some acceptance tests using TechTalk.SpecFlow and C#?

I have several feature files with some scenarios. I need to ignore several scenarios, or features, marked with some @tag depending on some condition. I have read specflow documentation but didn't find there something that can be useful for my solution. I want to use something like

[BeforeScenario("sometag")]
public static void BeforeScenario()
{
    if(IgnoreTests)
    {
       // This is the hot spot
       Scenario.DoSomethingToIgnoreScenarioIfConditionButRunScenarioIfConditionFalse();
    }        
}

Also I tried dynamically add or remove tags

[BeforeScenario("sometag")]
public static void BeforeScenario()
{
    if(IgnoreTests)
    {
       ScenarioContext.Current.ScenarioInfo.Tags.ToList().Add("ignore");
    }        
}

but it didn't work. Maybe is there some other way to dynamically add or remove tags? Or some methods in ScenarioContext class which will ignore current scenario?

like image 288
Dmytro Avatar asked Sep 25 '12 15:09

Dmytro


People also ask

How do you ignore tests in SpecFlow?

Ignored Tests Just like with normal unit tests, you can also ignore SpecFlow tests. To do so, tag the feature or scenario with the @ignore tag.

What is SpecFlow ScenarioContext?

ScenarioContext helps you store values in a dictionary between steps. This helps you to organize your step definitions better than using private variables in step definition classes. There are some type-safe extension methods that help you to manage the contents of the dictionary in a safer way.


1 Answers

You have at least 3 options:

  1. Configure Specflow to treat pending steps as ignore with missingOrPendingStepsOutcome="Ignore" then you can write:

    if(IgnoreTests)
    {
        ScenarioContext.Current.Pending();
    } 
    

    It is maybe not what you want depending on your requirements for pending steps.

  2. Use your unit testing framework built in method to ignore the test during runtime. So if you are using e.g. NUnit then with the Assert.Ignore():

    if(IgnoreTests)
    {
        Assert.Ignore();
    }
    

    I think this is the cleanest/easiest solution.

  3. Or if you want do it a testing framework-agnostic way and you are not afraid to mess with Specflow internals then you can use the IUnitTestRuntimeProvider interface:

    if (IgnoreTests)
    {
        var unitTestRuntimeProvider = (IUnitTestRuntimeProvider) 
        ScenarioContext.Current
           .GetBindingInstance((typeof (IUnitTestRuntimeProvider))); 
        unitTestRuntimeProvider.TestIgnore("ignored");
    }
    

    This will work even if you change your unit testprovider but it's not guaranteed that this API won't break in future.

like image 60
nemesv Avatar answered Oct 15 '22 15:10

nemesv