question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The same object instance is used when running tests in parallel

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Comments:18 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
rprousecommented, Sep 26, 2021

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 User object then the tests all run using the last User created.

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

1reaction
OsirisTerjecommented, Sep 24, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found