Adding More Information to TestResult.xml from NUnit

I would like to add a “message” to the unit test so that it really appears in the TestResult.xml file generated by NUnit. For example, this is currently being generated:

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

I would like to have an additional attribute (or node depending on the situation), for example:

<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 the “message” above would be somehow defined in the testing method itself (in my case, generated at runtime). Is there somewhere property that I don’t have enough to do something like this?

+5
source share
4 answers

, , , , .

, , -, ( ) TestCase .

+2

NUnit :

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

" " , . , . , , , , , . .

+5

, , : Description, Property XML. , .

0

TestContext, , . .

Each of my tests inherits from the 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 that the bottom two functions write out any message or error in the specified TestContext. This will also work well with parallelized tests.

Then I can use this parent class to configure my tests and write to the 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 results (Visual Studio) and in TestResult.xml using the NUnit Console Runner.

0
source

All Articles