3
votes

I am having trouble detecting Inconclusive test results in the [AfterScenario] hook.

I've got a large suite of Specflow tests that I run most nights and in the hooks section I am logging whether the test passed or failed and some info on the tags, then at the end of the test run I output this to a file. I am currently deciding whether a test has passed or failed by doing:

bool failed = ScenarioContext.Current.TestError != null;
string result = failed ? "failed" : "passed";

This works most of the time, but when a test doesn't have all the steps completed (scenario result is inconclusive) that method reports the scenario as passing, which isn't really what I want. I've tried setting missingOrPendingStepsOutcome to error or ignore in App.Config, but neither of them have any effect upon the TestError property, so again it'll get calculated as "passed".

I've noticed a couple of handy-looking properties ( MissingSteps and PendingSteps ) in the ScenarioContext.Current, unfortunately they're private, so I can't access them.

I am using C#.4.5.2 and Specflow 1.9.0.77 with NUnit 2.6.4.14350 and running the tests in ReSharper 9.2's Unit Test Sessions window in Visual Studio Enterprise 2015 on Windows 7 x64

2

2 Answers

3
votes

@Florent B. solution is better but if you don't want to do it by reflexion, you can use a trick that i found in SpecFlow 1.9.

Indeed, the [BeforeStep] hook is never called when the step does not exists.

[BeforeScenario]
public void BeforeScenario()
{
    ScenarioContext.Current.Set(false, "HasNotImplementedStep");
}

[BeforeStep]
public void BeforeStep()
{
    ScenarioContext.Current.Set(true, "IsBeforeStepCalled");
}

[AfterStep]
public void AfterStep()
{
    var beforeStepCalled = ScenarioContext.Current.Get<bool>("IsBeforeStepCalled");
    if (!beforeStepCalled)
    {
        ScenarioContext.Current.Set(true, "HasNotImplementedStep");
    }

    ScenarioContext.Current.Set(false, "IsBeforeStepCalled");
}

[AfterScenario]
public void AfterScenario()
{
    var hasNotImplementedStep = ScenarioContext.Current.Get<bool>("HasNotImplementedStep");
    if (hasNotImplementedStep)
    {
        // Do your stuff
    }
}
2
votes

You could check that the TestStatus property is not OK:

bool failed = ScenarioContext.Current.TestStatus != TestStatus.OK;