Search code examples
c#unit-testingautomated-testsnunittestrail

Map Testcase ID with NUnit


I'm currently building out my automation framework using NUnit, I've got everything working just fine and the last enchancement I'd like to make is to be able to map my automated test scripts to test cases in my testing software.

I'm using TestRail for all my testcases.

My ideal situation is to be able to decorate each test case with the corresponding testcase ID in test rail and when it comes to report the test result in TestRail, I can just use the Case id. Currently I'm doing this via matching test name/script name.

Example -

[Test]
[TestCaseId("001")]
    public void NavigateToSite()
    {
        LoginPage login = new LoginPage(Driver);
        login.NavigateToLogInPage();
        login.AssertLoginPageLoaded();
    }

And then in my teardown method, it would be something like -

[TearDown]
public static void TestTearDown(IWebDriver Driver)
    {
       var testcaseId = TestContext.CurrentContext.TestCaseid;
       var result = TestContext.CurrentContext.Result.Outcome;
       
      //Static method to report to Testrail using API
      Report.ReportTestResult(testcaseId, result);
    }

I've just made up the testcaseid attribute, but this is what I'm looking for.

[TestCaseId("001")]

I may have missed this if it already exists, or how do I go about possibly extending NUnit to do this?


Solution

  • You can use PropertyAttribute supplied by NUnit.

    Example:

    [Property("TestCaseId", "001")]
    [Test]
    public void NavigateToSite()
    {
    ...
    }
    
    [TearDown]
    public void TearDown()
    {
        var testCaseId = TestContext.CurrentContext.Test.Properties["TestCaseId"];
    }
    

    In addition you can create custom property attribute - see NUnit link