Hi!
I am running into an issue where it appears there is some "state" or information being shared between tests. I noticed this issue while working with Entity Framework 7.
In a nutshell, it appears that the tests run fine if I run them manually one by one using the Ncrunch Tests window, however, if I select two tests at once and click the play button (Run selected tests in new process) then typically one test fails and the other passes.
I have created a simple solution with 2 projects to demonstrate this behavior. There is a library project which contains the application code and a test project which contains the three tests. I uploaded my test solution to GitHub. Please see this repository:
https://github.com/rog1039/Rogero.NcrunchTests
Here is an outline of the solution:
ApplicationProject
- class DatabaseContext : DbContext
- override void OnModelCreating()
- class EntityA
- int Id {get;set;}
TestProject
- class ContextSharedStateTests
- Constructor to set up test
- private int _onModelCreatingCallCount = 0;
- void OnModelCreatingShouldBeCalledA()
- void OnModelCreatingShouldBeCalledB()
- void OnModelCreatingShouldOnlyBeCalledOnce()
In my library project I have two classes. I have an Entity Framework DbContext [DatabaseContext] class and a simple Entity class [EntityA] with a single int property called Id. My DatabaseContext class takes in an action that is stored for later execution during the OnModelCreating method. When the DatabaseContext is created for the first time, this OnModelCreating class is executed which notifies the test that the method was called. The test simply verifies that this method was called.
If I run the tests manually one at a time, then it seems OnModelCreating is always called each test run. When the run in close proximity to each other as mentioned above, then often a test will fail.
All my code with lots of comments is available via the GitHub repo mentioned above. To run that code however, you will need to do a package restore to get the relevant packages like xUnit and EntityFramework. To get the entity framework packages you'll need to use the EF nightlies nuget feed [https://www.myget.org/F/aspnetvnext/]
If it would be easier I have zipped up the entire directory which you I can send you. It is 10 MB but includes all the binaries so you won't have to mess with nuget for the Entity Framework binaries.
Thanks,
Paul