SpecFlow icon indicating copy to clipboard operation
SpecFlow copied to clipboard

Specflow AfterFeature not working once a test fails on Assert within a scenario

Open sirlukman opened this issue 8 years ago • 15 comments

I have this bunch of scenarios within a feature file and I do something like resetting my organisation job reference number back to the state I met it before my feature file was run. My major problem is that, if the last test in the feature file fails on Assert, then my program does not execute the code in my after feature block. I think irrespective of the faith of the test, after feature should always run, could this be a bug in specflow after feature.

I will appreciate all your feed backs, thanks in advance guys.

sirlukman avatar Jul 27 '17 15:07 sirlukman

So basically you say that the AfterFeature hook is not executed after a failing scenario? Which SpecFlow version you use?

gasparnagy avatar Jul 27 '17 18:07 gasparnagy

I have the latest version of specflow (2.1.0) and thanks for your interest in this matter, I really need all the help I can get, so I can get to the bottom of this problem. Cheers.

sirlukman avatar Jul 28 '17 07:07 sirlukman

@sirlukman The latest version is 2.2 and not 2.1. So what are you using really? Please have a look in your package.config.

SabotageAndi avatar Jul 28 '17 07:07 SabotageAndi

specflow @SabotageAndi Please see above, as I am pretty sure I am using specflow version 2.1.0 and Visual Studio didnt give me to option to update 2.2, as 2.1.0 is what I have as the latest stable version. Could the issue I am having be down to not having version 2.2 ???

sirlukman avatar Jul 28 '17 12:07 sirlukman

@sirlukman You have a custom feed selected in your package source (top right corner of the "manage packages for solution"- tab). Please change that to nuget.org. There we publish our packages.

SabotageAndi avatar Jul 28 '17 13:07 SabotageAndi

I have the same issue with [AfterScenario] My test is failed on Nunit assert, but code in AfterScenario method isn't executing. I guess the reason is that I'm running bunch of tests

-SpecFlow 3.1.74 -Nunit 3.12.0

bogdanChernichenko avatar Dec 23 '19 13:12 bogdanChernichenko

I also have the same issue with [AfterScenario]. When my test fails on the assert, then I reach the breakpoint in my AfterScenario, but the steps I call from there (with method When()) is not executed.

  • SpecFlow: 3.1.86
  • MsTest 2.1.0

smeile avatar Feb 27 '20 15:02 smeile

Same for me. If an assert fails in a test, Specflow methods in [AfterScenario] are not executed. :( Please have a look.

dguidali avatar Feb 28 '20 07:02 dguidali

Are you handling your exceptions? From the documentation on hooks:

Note: If a hook throws an unhandled exception, subsequent hooks of the same type are not executed. If you want to ensure that all hooks of the same types are executed, you need to handle your exceptions manually.

Stephen-Mc avatar Mar 02 '20 12:03 Stephen-Mc

That would be another scenario. In my scenario, the exception occurs in the test run and not in the AfterScenario.

smeile avatar Mar 02 '20 12:03 smeile

I need some code to always run even if the test fails, This is because there is very important cleanup to do.

If I follow the advice here and catch the exception, then the test won't fail because Nunit.Assert works by raising an exception.

I need the test to fail and then AfterScenario to run to do the cleanup. I'm also open to another way of doing the cleanup.

We call through to a native library which requires stopping and cleaning up, if we don't clean it up then it will segfault, that segfault will end the process, and because the process was ended SpecFlow doesn't print anything (logs are delayed until the test run finished). So you are completely in the dark with no idea what failed.

trampster avatar May 17 '21 04:05 trampster

I tried to install .net 6 following the instructions on https://dotnet.microsoft.com/download/dotnet/thank-you/sdk-6.0.100-preview.3-linux-x64-binaries

However it didn't work, the version is still .net 5 instead of .net 6

daniel@WS4320:~/Downloads$ mkdir -p $HOME/dotnet && tar zxf dotnet-sdk-6.0.100-preview.3.21202.5-linux-x64.tar.gz -C $HOME/dotnet
daniel@WS4320:~/Downloads$ export DOTNET_ROOT=$HOME/dotnet
daniel@WS4320:~/Downloads$ export PATH=$PATH:$HOME/dotnet
daniel@WS4320:~/Downloads$ dotnet --version
5.0.203
daniel@WS4320:~/Downloads$ 

trampster avatar May 18 '21 23:05 trampster

@trampster SpecFlow doesn't yet support .NET 6. We will have a look at this in summer.

About your hooks problem. This issue is about that if you have an exception in a hook that the other hooks aren't executed. What you describe looks for me you are doing in a step. In that case the AfterScenario hooks are always executed. If needed you can adjust with the Order parameter in which order the hooks are executed.

SabotageAndi avatar May 21 '21 08:05 SabotageAndi

What's the status on this issue? Experiencing the same as @dguidali

If an assert fails in a test, Specflow methods in [AfterScenario] are not executed. :(

ebwinters avatar Apr 05 '22 19:04 ebwinters

What's the status on this issue? Experiencing the same as @dguidali

If an assert fails in a test, Specflow methods in [AfterScenario] are not executed. :(

Actually, scratch that. the [AfterFeature] hook works as expected. I had an error in the actual code being executed below the hook, which silently failed.

ebwinters avatar Apr 05 '22 19:04 ebwinters