0

I'm using the NUnit framework. If my test-case failed with an exception like a timeout or any other exceptions. After the test it should go to [Teardown] but it is not going struck there abort the operation.

Agenda is trying to log the errors and capture it to the extent report.

       [Obsolete]
    [Test]
    public void TestMethod()
    {
        try
        {
           ...Throwing exceptions
          }
        catch(Exception ex)
         {
        // throw new NotImplementedException();
           // Assert.Fail("No exception was thrown");
           // AfterTest(); 
          }
         }

       [TearDown]
    public void AfterTest()
    {
        try
        {


            var status = TestContext.CurrentContext.Result.Outcome.Status;
            var stacktrace = " " +TestContext.CurrentContext.Result.StackTrace + " ";
            var errorMessage = TestContext.CurrentContext.Result.Message;
            Status logstatus;
            switch (status)
            {
                case TestStatus.Failed:
                    logstatus = Status.Fail;
                    string screenShotPath = Capture(driver, TestContext.CurrentContext.Test.Name);
                    _test.Log(logstatus, "Test ended with " +logstatus + " – " +errorMessage);
                    _test.Log(logstatus, "Snapshot below: " +_test.AddScreenCaptureFromPath(screenShotPath));
                    break;
                case TestStatus.Skipped:
                    logstatus = Status.Skip;
                    _test.Log(logstatus, "Test ended with " +logstatus);
                    break;
                default:
                    logstatus = Status.Pass;
                    _test.Log(logstatus, "Test ended with " +logstatus);
                    break;
            }
            driver.Quit();
        }
        catch (Exception e)
        {
            throw (e);
        }
    }
3
  • What do you mean by "After the test it should go to [Teardown] but it is not going struck there abort the operation"? So it is not going to the Teardown? Or it is going to the Teardown and you don't want it to? Or is it going to the Teardown and then the test hangs while the Teardown is executing? – Greg Burghardt Mar 5 '20 at 12:55
  • 1
    Assuming that the process should move to [Teardown] annotation. Then you should not use the try-catch inside the test annotations because Test annotations provide test results based on the exception so try by removing try-catch blocks – Mohamed Sulaimaan Sheriff Mar 5 '20 at 13:09
  • @GregBurghardt It is not going to Teardown, it was aborted in the [Test] once exception triggered. – Thiyagarajan Mar 5 '20 at 14:21
1

Assuming that the process should move to [Teardown] annotation.

Then you should not use the try-catch inside the test annotations because Test annotations provide test results based on the Exception so try by removing try-catch blocks

Reference: https://stackoverflow.com/a/111270/10858089

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.