Build/Test Issues

Colon in NUnit TestCase breaks ExclusivelyUsesAttribute

Started by bartj on 6,470 views

We would like to use the ExclusivelyUsesAttribute to prevent some issues with parallel execution of some legacy code. However, there appears to be a bug in this feature (NCrunch 1.48.0.5) when a colon is present in NUnit TestCase data. Below is a test that reproduces the issue. The tests should all run in parallel, but the test named "broken" does not pick up the ExclusivelyUsesAttribute. You can verify this by adding the "Exclusively Used Resources" column to the NCrunch Tests window.


using System;
using System.Threading;
using NCrunch.Framework;
using NUnit.Framework;

//Copied directly from the NCrunch documentation
namespace NCrunch.Framework
{
    public abstract class ResourceUsageAttribute : Attribute
    {
        private readonly string[] _resourceNames;

        public ResourceUsageAttribute(params string[] resourceName)
        {
            _resourceNames = resourceName;
        }

        public string[] ResourceNames
        {
            get { return _resourceNames; }
        }
    }

    public class ExclusivelyUsesAttribute : ResourceUsageAttribute
    {
        public ExclusivelyUsesAttribute(params string[] resourceName)
            : base(resourceName) { }
    }
}

namespace Demo
{
    [TestFixture]
    [ExclusivelyUses("Everything")]
    public class Class1
    {
        Mutex _testRunning = new Mutex(false, "TestLock");

        [SetUp]
        public void NewTestStarted()
        {
            if (!_testRunning.WaitOne(0))
                throw new InvalidOperationException("There should only be one test running at a time.");
        }

        [TearDown]
        public void TestCompleted()
        {
            _testRunning.ReleaseMutex();
        }

        [TestCase("okay","a")]
        [TestCase("okay", "b")]
        [TestCase("okay", "c")]
        [TestCase("okay", "d")]
        [TestCase("d", "cmkds:", TestName = "broken")]
        public void AllTestsShouldRunInSeries5(string dummy, string dummy2)
        {
            Thread.Sleep(3000);
        }
    }
}
Hi,

Thanks for reporting this issue. It looks like the test name is interfering with NCrunch's ability to properly identify the test method. I'll see if I can get this fixed.

You should be able to work around this problem by setting your Framework utilisation type for NUnit to StaticAnalysis. A drawback here is that you'll lose the granularity of your test cases - so it may be worth redesigning the test for the time being so that the colon isn't in the name.

Thanks again,

Remco
Thanks for getting back to me so quickly.

We've worked around it by replacing colons with pipes in the TestCase and then performing a string replace on them within the test. This works for now, but is obviously not ideal, particularly because it is easy for developers to forget to apply the workaround when they write new tests.

Do you have any idea when this will be fixed?

Thanks,
Bart
Hi Bart -

Right now I'm aiming to have this in the next v2 beta (v2.2b) expected to go out next week.

Post a reply

Log in to reply.