Skip to content

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

Closed

Description

@xadvfh

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();
        }
    }
}

Activity

  1. gleb-osokin commented on Jan 11, 2021

    @gleb-osokin

    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++);
        }
  2. xadvfh commented on Jan 11, 2021

    @xadvfh
    Author

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

  3. added this to the 3.14 milestone on Jan 11, 2021
  4. xadvfh commented on Jan 13, 2021

    @xadvfh
    Author

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

  5. rprouse commented on Jan 13, 2021

    @rprouse
    Member

    @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.

  6. xadvfh commented on Jan 13, 2021

    @xadvfh
    Author

    sounds great, thank you for your help.

  7. modified the milestones: 3.14, 3.13.1 on Jan 13, 2021
  8. rprouse commented on Jan 23, 2021

    @rprouse
    Member

    Fixed by #3720

  9. Lisaj commented on Jan 25, 2021

    @Lisaj

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

  10. uchagani commented on Jan 25, 2021

    @uchagani

    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.

  11. ivan-menshikov commented on Jan 25, 2021

    @ivan-menshikov

    #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));
    	}
    }
  12. gleb-osokin commented on Jan 26, 2021

    @gleb-osokin

    @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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions