Skip to content

FixtureLifeCycle(LifeCycle.InstancePerTestCase) Not working with TestFixtureSource #3715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
xadvfh opened this issue Jan 11, 2021 · 10 comments
Closed

Comments

@xadvfh
Copy link

xadvfh commented Jan 11, 2021

I'm using the new FixtureLifeCycle(LifeCycle.InstancePerTestCase) with TestFixtureSource and am running into an issue where a new instance is not created.

At first, I thought this might be an issue with WireMock.Net but it works fine without TestFixtureSource. It also works fine when using an IDisposable class to simulate setup/teardown per test as @jnm2 has mentioned in the past.

In my tests, I am using WireMock.Net to create a new instance of the mock server for each test. Before FixtureLifeCycle, I was starting the server in the constructor of an IDisposable class and stopping the server in the Dispose method.

In my tests below I'm just writing out the URL of the mock server to show that they are using the same url:port. Each test should start a server on a unique port number but both tests are using the same port.

using NUnit.Framework;
using System;
using System.Collections;
using System.Linq;
using WireMock.Server;

namespace NUnitTestProject1
{
    [TestFixture]
    [Parallelizable(ParallelScope.All)]
    [FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
    [TestFixtureSource(typeof(BrowserData))]
    public class TestBase
    {
        protected WireMockServer Server { get; set; }
        protected string driverId;

        public TestBase(string driver)
        {
            driverId = driver;
        }

        [SetUp]
        public void BeforeTest()
        {
            Server = WireMockServer.Start();
        }


        [TearDown]
        public void AfterTest()
        {
            Server?.Stop();
        }
    }


    public class Tests : TestBase
    {
        public Tests(string driver) : base(driver)
        {
        }

        [Test]
        public void Test1()
        {
            TestContext.Out.WriteLine(Server.Urls.First());
        }

        [Test]
        public void Test2()
        {
            TestContext.Out.WriteLine(Server.Urls.First());
        }
    }


    public class BrowserData : IEnumerable
    {
        private readonly string[] _drivers;
        public BrowserData()
        {
            var driverId = "LocalChrome";
            _drivers = Array.ConvertAll(driverId.Split(",", StringSplitOptions.RemoveEmptyEntries), d => d.Trim());
        }

        public IEnumerator GetEnumerator()
        {
            return _drivers.GetEnumerator();
        }
    }
}
@gleb-osokin
Copy link

gleb-osokin commented Jan 11, 2021

I can confirm the bug, here's a refined example test that fails:

    [TestFixtureSource(nameof(FixtureArgs))]
    [FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
    public class LifeCycleWithTestFixtureSourceTest
    {
        private readonly int _initialValue;
        private int _value;

        public LifeCycleWithTestFixtureSourceTest(int num)
        {
            _initialValue = num;
            _value = num;
        }

        public static int[] FixtureArgs() => new[] { 1, 42 };

        [Test]
        public void Test1() => Assert.AreEqual(_initialValue, _value++);

        [Test]
        public void Test2() => Assert.AreEqual(_initialValue, _value++);
    }

@xadvfh
Copy link
Author

xadvfh commented Jan 11, 2021

@avilv since you worked on the original implementation would you mind taking a look at this?

@xadvfh
Copy link
Author

xadvfh commented Jan 13, 2021

@rprouse any way to get a bug fix release instead of waiting until 3.14?

@rprouse
Copy link
Member

rprouse commented Jan 13, 2021

@xadvfh I'm planning on doing a 3.13.1 release soon. I'm just tracking a few other bugs and giving it a bit of time for others to come to light.

@xadvfh
Copy link
Author

xadvfh commented Jan 13, 2021

sounds great, thank you for your help.

@rprouse rprouse modified the milestones: 3.14, 3.13.1 Jan 13, 2021
@rprouse
Copy link
Member

rprouse commented Jan 23, 2021

Fixed by #3720

@Lisaj
Copy link

Lisaj commented Jan 25, 2021

Hi, i have same problem with TestCaseSource. Is it fixed by this bug to?

@uchagani
Copy link

It is fixed but we’re waiting on a release which should be by the end of this month. I’m not sure if it has made its way into the nightly build.

@ivan-menshikov
Copy link

#3720 does not seem to fix the following:

[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
[Parallelizable(ParallelScope.All)]
public class FixtureLifeCycleTests
{
	private int _counter = 0;

	[TestCase(0)]
	[TestCase(1)]
	[TestCase(2)]
	[TestCase(3)]
	public void WorksWithTestCaseAttribute(int _)
	{
		Assert.AreEqual(1, Interlocked.Increment(ref _counter));
	}
}

@gleb-osokin
Copy link

@ivan-menshikov this issue relates specifically to TestFixtureSource parametrization.

I have created a new issue #3743, that addresses this new case as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants