Search code examples
coded-ui-testsspecflow

BeforeFeature/AfterFeature does not work using SpecFlow and Coded UI


I am not able to define a [BeforeFeature]/[AfterFeature] hook for my feature file. The application under test is WPF standalone desktop applications.

If I use [BeforeScenario]/[AfterScenario] everything works fine, the application starts without any problem, the designed steps are performed correctly and the app is closed.

Once I use the same steps with [BeforeFeature]/[AfterFeature] tags the application starts and the test fails with:

The following error occurred when this process was started: Object reference not set to an instance of an object.

Here is an example:

[Binding]
public class Setup
{   
    [BeforeScenario("setup_scenario")]
    public static void BeforeAppScenario()
    {
        UILoader.General.StartApplication();
    }

    [AfterScenario("setup_scenario")]
    public static void AfterAppScenario()
    {
        UILoader.General.CloseApplication();
    }

    [BeforeFeature("setup_feature")]
    public static void BeforeAppFeature()
    {
        UILoader.General.StartApplication();
    }

    [AfterFeature("setup_feature")]
    public static void AfterAppFeature()
    {
        UILoader.General.CloseApplication();
    }
}

StartApplication/CloseApplication were recorded and auto-generated with Coded UI Test Builder:

public void StartApplication()
{
    // Launch '%ProgramFiles%\...
    ApplicationUnderTest Application = ApplicationUnderTest.Launch(this.StartApplicationParams.ExePath, this.StartApplicationParams.AlternateExePath);
}

public class StartApplicationParams
{    
    public string ExePath = "C:\\Program Files..."
    public string AlternateExePath = "%ProgramFiles%\\..."
}

Noteworthy: I'm quite new with SpecFlow. I can't figure it out why my test fails with [BeforeFeature] and works fine with [BeforeScenario].

It would be great if somebody could help me with this issue. Thanks!


Solution

  • I ran into a similar problem recently. Not sure if this can still help you, but it may be of use for people who stumble upon this question.

    For BeforeFeature\AfterFeature to work, the feature itself needs to be tagged, tagging just specific scenarios will not work.

    Your feature files should start like this:

    @setup_feature
    Feature: Name Of Your Feature
    
    @setup_scenario
    Scenario: ...