Skip to content

TestName in TestCase attribute not validated to be not empty #3038

Closed

Description

@sbakharev

Hi everyone! Thank you for NUnit!

Using NUnit 3.10.1

The usage of NUnit in our project in rather advanced. We use many publicly available features as well as some undocumented ones ;)

And we have a homebrew integration of NUnit and ReportPortal (by EPAM) which attaches itself to NUnit engine like a parasite, for the information about currently running tests to be transfered to RP in real time (not after the test run is completed) even when running tests in debug mode from VS. We use the information supplied by NUnit such as test names, descriptions, categories, ignore reasons etc to make test reports as easy to read as possible.

Several days ago reports at RP started indicating integration problems which occur very rarely. I took a deeper look and found errors at RP logs saying that test name constraint "not empty" fails test report creation. Further research pointed at a new test in our project which had its attributes defined like this:

[Test]
[TestCase(TestName = "")]
[Category("DB")]

We use this workaround with TestCase for having a human-readable test name supplied by NUnit instead of coding a custom attribute. And in many cases we use TestCase attribute for its intended purpose as well.

So obviously one programmer forgot to type in the value for TestName. The integration code was looking at ITest.Name property which appeared to be an empty string. And an empty string in its turn appeared to be unacceptable as a test name for RP.

If you run this test with Resharper, it displays a test icon with nothing next to it (no name), which means Resharper relies on the same NUnit functionality.

I've added a workaround:

internal static string GetName(this ITest test) 
    => string.IsNullOrWhiteSpace(test.Name) ? test.MethodName : test.Name;

But the expected behavior is one of these:

  1. NUnit exception during initialization, like it happens when parameters count/types of the test method do not match with those of TestCase attribute
  2. If TestName is empty then provide a default generated value instead, like as if there was no TestName property defined at all

Thanks in advance! :)

Activity

  1. ChrisMaddock commented on Oct 2, 2018

    @ChrisMaddock
    Contributor

    Hey @sbakharev - thanks for the clear report! 🙂

    Resharper and ReportPortal are both third party products in this instance. What result do you get if you run this test with the NUnit Console?

  2. sbakharev commented on Oct 2, 2018

    @sbakharev
    Author

    @ChrisMaddock wow, what a fast reaction!

    This test:

    [Test]
    [TestCase(TestName = "")]
    [Category("DB")]
    public void Test1()
    {
        TestContext.WriteLine("Test: " + TestContext.CurrentContext.Test.Name);
        Assert.NotNull(null);
    }

    Produces this test context output:

    Test: 
    

    And this is it's section from TestResult.xml (removed newlines of CDATA nodes to simplify reading):

    <test-case id="0-1001" name="" fullname="NUnitEmptyTestNameTest.Class1." methodname="Test1" classname="NUnitEmptyTestNameTest.Class1" runstate="Runnable" seed="1994871090" result="Failed" start-time="2018-10-02 12:35:30Z" end-time="2018-10-02 12:35:31Z" duration="0.103357" asserts="1">
        <failure>
            <message><![CDATA[  Expected: not null But was:  null ]]></message>
            <stack-trace><![CDATA[   at NUnitEmptyTestNameTest.Class1.Test1() in MY_SOLUTION_PATH\NUnitEmptyTestNameTest\Class1.cs:line 13 ]]></stack-trace>
        </failure>
        <assertions>
            <assertion result="Failed">
                <message><![CDATA[  Expected: not null But was:  null ]]></message>
                <stack-trace><![CDATA[   at NUnitEmptyTestNameTest.Class1.Test1() in MY_SOLUTION_PATH\NUnitEmptyTestNameTest\Class1.cs:line 13 ]]></stack-trace>
            </assertion>
        </assertions>
    </test-case>

    The issue is not about 3rd party products, I've described the process just to clarify the context. The issue is about Name property of the ITest and of the TestAdapter. Empty test name is anyway not normal - that's what I'm trying to tell :)

    P.S. NUnit console latest version 3.9.0

  3. ChrisMaddock commented on Oct 3, 2018

    @ChrisMaddock
    Contributor

    Thanks. What I was thinking of, was whether this currently causes any issue for NUnit itself, or if it's just a case of third party products which make assumptions that NUnit doesn't guarantee (e.g. Test names not being empty!) Based on the above - it seems to be the former.

    That said - I can't think of any sensible use for wanting empty string test names, so think it could be sensible to add this restriction to NUnit - to avoid user error, like you've had above. What do @nunit/framework-team think?

  4. jnm2 commented on Oct 3, 2018

    @jnm2
    Contributor

    I think requiring a non-whitespace string is sensible.

  5. ChrisMaddock commented on Oct 3, 2018

    @ChrisMaddock
    Contributor

    Great - then let's do it. @sbakharev - is this something you'd be willing to supply a pull request for?

  6. sbakharev commented on Oct 4, 2018

    @sbakharev
    Author

    @ChrisMaddock I've never had any deal with NUnit source code except for reading several portions of code to understand how some of the features work. I'll give it a try but first let @nunit/framework-team tell me which one of the two solutions (exception or providing a generated value) suits better.

  7. CharliePoole commented on Oct 4, 2018

    @CharliePoole
    Member

    I think that we would expect the test to be non-runnable, as in your first option.

  8. added a commit that references this issue on Dec 30, 2018
    7e7abb8
  9. self-assigned this
    on Dec 30, 2018
  10. added this to the 3.12 milestone on Dec 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions