I discovered that NCrunch never runs our AfterTestRun method.
If i run the tests with ReSharper TestRunner, the method gets called.
Is there a configuration that can fix this problem?
I made a sample project to reproduce the problem, unfortunately I cannot upload it with this post.
But it should be easy to reproduce:
1. Create a new class library project in VS2013
2. Install nuget package nunit and specflow
3. Add a new class with the following source code:
using System;
using System.Diagnostics;
using NUnit.Framework;
using TechTalk.SpecFlow;
namespace SpecFlowTest
{
[Binding]
public static class TestContext
{
[BeforeScenario]
public static void BeforeScenario()
{
Debug.WriteLine("Before scenario");
}
[AfterScenario]
public static void AfterScenario()
{
Debug.WriteLine("After scenario");
}
[BeforeTestRun]
public static void SetUpFeature()
{
Debug.WriteLine("Before test run");
}
[AfterTestRun]
public static void AfterTestRun()
{
Debug.WriteLine("After test run");
}
}
}
4. Create a test to execute
5. Configure ncrunch
6. Set a breakpoint in the AfterTestRun method
7. Debug the test with ncrunch and with resharper to see the differene
Edit:
NCrunch Version: 2.16.0.13
I hope I am not the only one that has this problem.
Thanks for sharing this issue. I've managed to reproduce it by following your instructions.
The problem is being caused by NUnit being unable to load the SpecFlow NUnit plugin which is used to hook the shut-down of the test environment. NCrunch doesn't officially support NUnit plugins, as the way in which these plugins work can make it impossible to warrant that the test environment will behave the way that it should. I grant that SpecFlow itself is a bit of a special case considering it is a test framework itself, but it looks like work will be required to allow such a hook to work. At this stage, I am not certain whether implementing support for this feature is feasible.
I recommend steering away from using such a hook-point to execute code after your tests have run. I'm not sure what you are using this hook for. If you are using it to clean up resources at the end of a test run, I recommend considering a different design.
Even when this feature is working correctly, there is no guarantee that the cleanup code at the end of a test run will always be called. For example, the process may experience a stack overflow, out of memory exception, or any other unexpected problem that destabilises it and causes it to terminate unexpectedly. This could be as simple as a debug session that is interrupted. A good design for cleaning up resources created by frequently executed tests is often to ensure that tests clean up any stale resources at the beginning of their execution. In this way, you can be sure the test will always start with a clean slate.