The same object instance is used when running tests in parallel
See original GitHub issueI recently switched from MsTest to Unit3 and I noticed that when my tests are running in parallel I receive the same hashcode for an object set up in [SetUp] method.
using System;
using System.Threading;
using NUnit.Framework;
[assembly: Parallelizable(ParallelScope.All)]
namespace Nunit3netcore
{
public class User
{
}
public class Tests
{
public User User { get; set; }
[SetUp]
public void Setup()
{
User = new User();
}
[Test]
public void Test1()
{
Thread.Sleep(3000);
TestContext.WriteLine(User.GetHashCode());
}
[Test]
public void Test2()
{
Thread.Sleep(3000);
TestContext.WriteLine(User.GetHashCode());
}
[Test]
public void Test5()
{
Thread.Sleep(3000);
TestContext.WriteLine(User.GetHashCode());
}
}
}
In test output for each test, I see that the hash code is the same. When I comment out assembly:Parralilzable attribute and my code is running in non-paralell the hash code is different for each test. In MsTest I was generated a new object with new hash code within [TestInitialize] method. Packages I use: Nunit 3.12.0 Nunit3TestAdapter 3.16.1 Microsoft.NET.Test.Sdk (16.5.0)
Issue Analytics
- State:
- Created 2 years ago
- Comments:18 (11 by maintainers)
Top Results From Across the Web
testng: running parallel=methods while there is an instance ...
This is a question about TestNG (not JUnit) and the answer is: No -- this is not thread safe; the instance of the...
Read more >TestNG Parallel Execution - How to run Selenium tests in ...
Instances : This value will run all the test cases parallelly inside the same instance.
Read more >Parallel=instances run tests of same instance on different ...
The documentation says that • parallel="instances": TestNG will run all the methods in the same instance in the same thread, but two methods ......
Read more >Running Tests in Parallel | TestComplete Documentation
Via the Parallel script object You can orchestrate parallel runs of cross-platform web tests and cloud-compatible mobile tests from script tests by using ......
Read more >Parallel Testing: The Essential Guide
Parallel Testing is a process to leverage automation testing capabilities by allowing the execution of the same tests simultaneously in multiple ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

When NUnit runs test in Parallel, it creates one instance of the test class and then calls each test method in parallel. So, what is happening is that Setup is being called multiple times creating and overwriting the
Userobject then the tests all run using the lastUsercreated.You can use thread local storage, or an easier fix is to add
[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]to the test class. If you do this, NUnit will create a new instance of your test class for each test method that is run.See https://docs.nunit.org/articles/nunit/writing-tests/attributes/fixturelifecycle.html
The logger is just the small class above. It doesn’t contain anything suspicious. And the output from NUnit.Framework is on the other hand very suspicious. I think this issue should be transferred to the NUnit repro.