Friday, September 16, 2016

Using ExtentReports with MSTest framework on C#

When i was dealing with coded UI ,Selenium and Appium test result reporting i found this super cool reporting framework called "ExtentReports".





I love the UI.
And this can be use with MSTest and Nunit framework too.

Below are the steps to how to deal with the ExtentReports on MSTest.

1.     Downloadthe latest ExtentReports DLL, or use this command with NuGet manager.
Install-Package ExtentReports
2.     Add the downloaded file as a reference.
3.     Use the namespace “using RelevantCodes.ExtentReports;” in order to use the newly downloaded dll.
4.     Then on your test project at the class level define below variables;
·         public static ExtentTest test { get; set; }
·         public static ExtentReports extent { get; set; }
·         public TestContext TestContext { get; set; }

5.     Use an assembly initialize attribute and provide the folder for the HTML report.
[AssemblyInitialize]
static public void TestSetUp(TestContext TestContext)
{
extent = new ExtentReports("../../Reports/ExtentScreenshot.html", true);
        }

6.  Inside your test method write down the things or steps that you are performing by the Extended Reports way as on below.

[TestMethod]
        public void Test_OtherTestatNewClass()
        {

            test = extent.StartTest("Test case one");<-Test case name.

            test.Log(LogStatus.Pass, "Start Compairing two values");<-Explain the step
            Assert.AreEqual("123", "1233");
            test.Log(LogStatus.Pass, "End Compairing two values");

            LogStatus status = test.GetCurrentStatus();
            test.Log(LogStatus.Info, "Test Finished");
            test.Log(LogStatus.Info, status.ToString());  
            test.Log(LogStatus.Pass, "Test Passed");
        }

7.  Then use a test cleanup method and it will do the things if the above test get failed.
[TestCleanup]
        public void LogError()
        {
         
            var status = TestContext.CurrentTestOutcome.ToString();
            var status2 = Console.Error.ToString();

            this.TestContext.WriteLine(Console.Error.ToString());

            if (status =="Failed")
            {
                test.Log(LogStatus.Fail);
                test.Log(LogStatus.Fail,"Test Failed");
              

            }
           
            extent.EndTest(test);
        }

8.  Then at last use an assembly cleanup method in order to write everything to the the HTML file

[AssemblyCleanup]
        static public void EndReport()
        {
            extent.Flush();       
            extent.Close();
        }

DONE

No comments:

Post a Comment