Search code examples
c#automated-testsnunittestrail

Integrate automation tests with TestRail


i'm trying to integrate my automation tests with TestRail. I work with c#, ef and NUnit

I have test, with his Id in TestRail. In the description, I indicated the Id of the case from TestRail

[Test(Description = "C596")]
[TestCase()]
[TestCase()] 

OneTimeSetUp

public void OneTimeSetUp(){
Client = new TestRailClient(Url, User, Password);
var commandResult = Client.AddRun(_projectId, 2, "test run demo" + DateTime.UtcNow.Ticks, "test run demo", 2);
}

My problem: i can't test case Id in TearDown for check test case result

In TearDown I tried to get test case id with this way

var id = TestContext.CurrentContext.Test.Id this method returns the wrong id, which i don't need

next I found this example, but I could not understand how it works

var id = TestContext.CurrentContext.Test.Properties.Get("Description").ToString().Replace("C","");

All my TearDown:

public void Dispose()
        {
            var id = TestContext.CurrentContext.Test.ID;
            var result = TestContext.CurrentContext.Result.Outcome.Status;
            var testRailStatus = result switch
            {
                TestStatus.Failed => ResultStatus.Failed,
                TestStatus.Passed => ResultStatus.Passed,
                _ => ResultStatus.Retest
            };

            var resultForCase = Client.AddResultForCase(_runId, ulong.Parse(id), testRailStatus);
            Console.WriteLine(resultForCase.WasSuccessful);
        }

How I can get test case Id for check his result?
Thanks!


Solution

  • When using TestCase attributes, the Description property must be specified inside each TestCase attribute.

    [TestCase(Description = "C596")]
    [TestCase(Description = "C596")] 
    

    Now to get id, you can use your code:

    var id = TestContext.CurrentContext.Test.Properties.Get("Description").ToString().Replace("C","");