Search code examples
c#special-charactersnunit-3.0

Why does nunit not run a test which contains control/special chars


If I have a test class like

public class Tests
{
    [TestCaseSource(nameof(TestSource))]
    public void Test(string c)
    {
        Assert.Pass();
    }

    public static IEnumerable<object> TestSource()
    {
        for (char c = '\0'; c < 255; c++)
        {
            yield return c.ToString();
        }
    }
}

then the test runner in the Visual Studio can discover all the tests but doesn't run all of them.

these are the ascii character ranges that are not running:

U_0001 - U_0006
U_000E - U_001F
U_007F - U_0084
U_0086 - U_009F

We currently use some of these characters like U_0002 for network work related test which are included in the test but currently do not run

This is the output for the test run

---------- Starting test discovery for requested test run ----------
NUnit Adapter 3.17.0.0: Test discovery starting
NUnit Adapter 3.17.0.0: Test discovery complete
========== Test discovery finished: 255 Tests found in 4,8 sec ==========
---------- Starting test run ----------
NUnit Adapter 3.17.0.0: Test execution started
Running selected tests in C:\Users\WEIK014\source\repos\TestSourceTestProject\TestSourceTestProject\bin\Debug\netcoreapp2.1\TestSourceTestProject.dll
   NUnit3TestExecutor discovered 199 of 199 NUnit test cases
NUnit Adapter 3.17.0.0: Test execution complete
========== Test run finished: 199 Tests run in 8,6 sec (199 Passed, 0 Failed, 0 Skipped) ==========

as you see it doesn't even recognize that it skips tests.

Versions in the project:

  • Project .Net Core 2.1
  • NUnit 3.13.1
  • NUnit3TestAdapter 3.17.0

Here is some visual proof that these test do not run enter image description here


Solution

  • Well that sounds like it's probably a bug somewhere in Visual Studio or the test adapter with displaying Unicode control characters. As a workaround, you can override the test display name for control characters:

    public static IEnumerable<TestCaseData> TestSource()
    {
        for (char c = '\0'; c < 255; c++)
        {
            var data = new TestCaseData(c.ToString());
            if (char.IsControl(c))
            {
                data.SetArgDisplayNames($"{(int)c:X4}");
            }
            yield return data;
        }
    }
    

    This will preserve the display for printable characters (e.g. Test("a")), and the control characters will display as Test(0001), etc.