Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nunit3 how to change testcase name based on parameters passed from TestFixtureSource

I'm using NUnit 3.0 and TestFixtureSource to run test cases inside a fixture multiple times with different parameters/configurations (I do want to do this at TestFixture level). Simple example:

[TestFixtureSource(typeof (ConfigurationProvider))]
public class Fixture
{
    public Fixture(Configuration configuration)
    {
        _configuration = configuration;
    }

    private Configuration _configuration;

    [Test]
    public void Test()
    {
        //do something with _configuration
        Assert.Fail();
    }
}

Let's say Test() fails for one of the configurations and succeeds for another. In the run report file and in Visual Studio's Test Explorer the name for both the failed and the succeeded runs will be displayed as just Test(), which doesn't tell me anything about which setup caused issues.

Is there a way to affect the test cases names in this situation (i.e. prefix its name per fixture run/configuration)? As a workaround I'm currently printing to the results output before each test case fires but I would rather avoid doing that.

Since NUnit 3.0 is in beta and this feature is fairly new I wasn't able to find anything in the docs. I found TestCaseData but I don't think it's tailored to be used with fixtures just yet (it's designed for test cases).

like image 625
wade Avatar asked Sep 15 '15 23:09

wade


1 Answers

I can't find a way to change the testname, but it should not be neccessary, because NUnit3 constructs the testname by including a description of the testfixture.

The example class Fixture from the question can be used unchanged if the Configuration and ConfigurationProvider has an implementation like this:

public class Configuration
{
    public string Description { get; }

    public Configuration(string description)
    {
        Description = description;
    }

    public override string ToString()
    {
        return Description;
    }
}

public class ConfigurationProvider : IEnumerable
{
    public IEnumerator GetEnumerator()
    {
        yield return new Configuration("Foo");
        yield return new Configuration("Bar");
        yield return new Configuration("Baz");
    }
}

The 'trick' is to make sure the constructor-parameter to the fixture is a string or has a ToString-method that gives a sensible description of the fixture.

If you are using NUnit 3 Test Adapter in Visual Studio, then the testfixtures will be displayed as Fixture(Foo), Fixture(Bar) and Fixture(Baz) so you can easily distinguish between their tests. The xml-output from nunit3-console.exe also uses descriptive names, fx: fullname=MyTests.Fixture(Bar).Test

<test-case id="0-1003" name="Test" fullname="MyTests.Fixture(Bar).Test" methodname="Test" classname="MyTests.Fixture" runstate="Runnable" result="Failed" ... >
    <failure>
        <message><![CDATA[]]></message>
        <stack-trace><![CDATA[at MyTests.Fixture.Test() in ... ]]></stack-trace>
    </failure>
    ...
</test-case>

Using NUnit 3 Test Adapter in Visual Studio

like image 182
Daniel Brixen Avatar answered Oct 20 '22 17:10

Daniel Brixen