November 30, 2019

How to Attach result files to MsTest tests (Local and in Azure)

  November 30, 2019
The results we can see along with any test written using MsTest is in the Output window. This window will be having all the logs we write during the execution.


But what if we use a third party report like extent report and we need to attach the report file as part of the test from test explorer.

In those cases we have to use the below syntax in the [TestCleanup] section of Mstest, This can be used once the report is generated (After the flush step of the report).

TestContext.AddResultFile("<Report path>");

A sample test cleans up using this is given below.

        [TestCleanup]
        public void AfterTest()
        {
            String resStatus = "PASS";

            if (boolFailFlag == true || !(TestContext.CurrentTestOutcome == UnitTestOutcome.Passed))
            {
                WriteLog("fail", "Test failed due to following issue \n " + strFailMsg);
                resStatus = "FAIL";
            }
            extent.Flush();
            driver.Close();
            driver.Quit();
            driver.Dispose();

            TestContext.AddResultFile(ChangeResultFileStatus(RunFile, resStatus));

            if (boolFailFlag == true || !(TestContext.CurrentTestOutcome == UnitTestOutcome.Passed))
            {
                Assert.Fail("Test failed due to following issues \n " + strFailMsg);
            }

        }

TestContext.AddResultFile(), is the default method provided by MsTest to do this. We can attach any number of files (of any type) with test results using this.

If there are any files attached, it will be shown in the output window of the visual studio below.


If we are associating this test with any test case in test plans of Azure, the Same results will be uploaded to the Test case as part of the test run. For this to happen we need to run the test case from the Azure portal and map the test suite with the release which contains the tests.

If mapped, we can see the attachments as below in the test run as well as in the results pane of the test suite.

In Test run


In Test plan


Clicking on the View execution history will provide us with the list of last execution results as below.


logoblog

Thanks for reading How to Attach result files to MsTest tests (Local and in Azure)

Previous
« Prev Post

No comments:

Post a Comment

Fixing javascript error: Cannot read properties of null (reading 'querySelector') issue when using CSS identifier in selenium

 JavaScript is another way to interact with web elements when normal selenium methods fail to act. But one issue with javascript is, it does...