Is there any way to check if NUnit test is passed before the test is done

1k Views Asked by At

what I have here is my script in selenium c# using NUnit framework with multiple test as you can see below and it's connected to my TestRail and I am wondering if there is any way to check if my test is passed before the actual test is done.

    [Test, Order(0)]
    public void Login()
    {
        Navigate.ToLoginPage();
        Log.In("user");
        TestRailServer.Result("1"); //1 is testcase id
    }

    [Test, Order(1)]
    public void RegisterUser()
    {
        Add.User();
        TestRailServer.Result("2"); //2 is testcase id
    }

The TestRailServer.Result("1") you see in the test is my connection in testrail it will decide if the test case in testrail is passed or failed based in the TestMethod(Login and RegisterUser)

Here's my code in TestRail connection

        private static TestRailClient trClient;
    public static void Result(string testCaseID)
    {
        ulong testRunID = "testRailID";
        string testrailComment;
        trClient = new TestRailClient("https://client.testrail.net/", 
        "username", "password");
        var result = TestContext.CurrentContext.Result.Outcome.Status;
        var testrailStatus = result switch
        {
            TestStatus.Failed => ResultStatus.Failed,
            TestStatus.Passed => ResultStatus.Passed,
            _ => ResultStatus.Retest
        };
        testrailComment = (testrailStatus == ResultStatus.Failed) ? TestContext.CurrentContext.Result.Message : "Passed";
        trClient.AddResultForCase(testRunID, ulong.Parse(testCaseID), testrailStatus, testrailComment);
    }

But the problem is the TestRailServer.Result is called before the test is done so I need a way to tell if my test passed before it executes the TestRailServer

Thank for the help if there is any and apology if you find it difficult to undestand

1

There are 1 best solutions below

0
On

I've not used testrail, but I've used Nunit a fair amount.

My understand is that NUnit only knows if it has failed by doing an assert. If you don't have an assert in your code (and I don't see any at the level you've posted) your tests will always pass on completion. The challenge with your approach is that a failing assert will stop the test and you potentially won't get any result. That's how NUnit is designed - it's for unit tests that should take miliseconds afterall.

Instead of you ending every test line interacting with the reporter, move it to a [Teardown] method. That way the test completes (in a pass or fail state) and reporting is done after the event.

Something like:

        public string testId;

        [Test, Order(0)]
        public void test1()
        {
            testId= "1";
            Navigate.ToLoginPage();
            Log.In("user");
            //assert here
        }

        [Test, Order(1)]
        public void test2()
        {
            testId= "2";
            Add.User();
            //assert here
        }

        [TearDown]
        public void TearDown()
        {
            TestRailServer.Result(testId);
        }

(and add asserts to the tests if you don't already) :-)