Question

I would like to be able to add a "message" to a unit test, such that it actually appears within the TestResult.xml file generated by NUnit. For example, this is currently generated:

<results>
    <test-case name="MyNamespace.Tests.MyTest" executed="True" success="True" time="0.203" asserts="4" />
</results>

I would like to be able to have an additional attribute (or node as the case may be), such as:

<results>
    <test-case name="MyNamespace.Tests.MyTest" executed="True" success="True" time="0.203" asserts="4" message="Tested that some condition was met." />
</results>

The idea is that "message" above would somehow be defined within the test method itself (in my case, generated at run-time). Is there a property somewhere that I'm missing to be able to do something like this?

Was it helpful?

Solution

This may be missing the point, but how about naming the tests so they indicate what they test - then you may not even need the message.

If it proves to be absolutely necessary, I think you'll need to produce your own testrunner that would (off the top of my head) read an additional attribute off the TestCase and attach it to the output.

OTHER TIPS

In the recent NUnit releases you can do:

Assert.AreEqual(250.00, destination.Balance, "some message here");

Where "Some message here" can be a constant message or a message generated at runtime and stored in a string variable. These messages will only appear in the output however if the assertion fails. Usually, however, you only need information about failing tests so I recommend building up a string by adding each previous message and then using that string variable as the message in all of your asserts. This allows you to get all of the information you need from failing tests.

I can't see anything available at run time, but there are a couple of features that you might want to investigate: the Description attribute and the Property attribute both add text to the XML output file. Unfortunately, they're both defined at compile time.

You can use the TestContext to easily write out any message you want. Here is how I am setup.

Each of my tests are inherited from a testbase class. This removes redundant code.

[TestFixture]
public class TestBase
{

    public IWebDriver driver;

    //[OneTimeSetUp] and [OneTimeTearDown] go here if needed

    [SetUp]
    public void Setup(){
         driver = Shortcuts.SetDriver("my browser");
    }

    [TearDown]
    public void TearDown()
    {
        driver.Quit();
        Comment("@Result: " + TestContext.CurrentContext.Result.Outcome.ToString());
    }

    public void Comment(string _comment)
    {
        TestContext.Out.WriteLine(_comment);
    }
    public void Error(string _error)
    {
        TestContext.Error.WriteLine(_error);
    }

}

You can see the bottom two functions write out any message or error in said TestContext. This will work nicely with parallizable tests also.

I can then use that parent class to setup my tests, and write to my console.

//Role Management
public class RoleManagementTests : TestBase
{
    [TestCase]
    public void RoleManagement_7777_1()
    {
        Comment("Expected: User has the ability to view all roles in the system.");
        //Test goes here
    }
}

Now you can see the results in the output (Visual Studio) and in the TestResult.xml using NUnit Console Runner.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top