I am not sure why I am getting one of my variables testlog is null. I am able to initialize and start the test but at the teardown method in my test class its not able to flush the reports and log it because testlog is null.. FYI.. I have created a method TestReportInitialize just to start the method so I can avoid trying createTest method in each Test case and I create LogReport which calls the LoggingTestStatusExtentReport() to log it to the report and flush it.
//Here is my ExtentReport class
public class ExtentReport
{
public static ExtentReports extent;
public static ExtentTest testlog;
public static bool testStarted = false;
public static void StartReport()
{
//To obtain the current solution path/project path
string path = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
string actualPath = path.Substring(0, path.LastIndexOf("bin"));
string projectPath = new Uri(actualPath).LocalPath;
string reportPath = projectPath + "Reports\\" + DateTime.Now.ToString("MM_dd_yyyy_HH_mm_ss") + ".html";
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(reportPath);
extent = new ExtentReports();
extent.AttachReporter(htmlReporter);
//Add QA system info to html report
extent.AddSystemInfo("Tester", Environment.UserName);
extent.AddSystemInfo("Environment", ConfigurationManager.AppSettings["TestEnvironment"]);
extent.AddSystemInfo("MachineName", Environment.MachineName);
}
public static void StartExtentTest(string testsToStart)
{
testlog = extent.CreateTest(testsToStart);
testlog.Log(Status.Pass, "Test steps start for test case " + TestContext.CurrentContext.Test.Name);
}
public static void LoggingTestStatusExtentReport()
{
try
{
var status = TestContext.CurrentContext.Result.Outcome.Status;
var stacktrace = string.Empty + TestContext.CurrentContext.Result.StackTrace + string.Empty;
var errorMessage = TestContext.CurrentContext.Result.Message;
Status logstatus;
switch (status)
{
case TestStatus.Failed:
logstatus = Status.Fail;
testlog.Log(Status.Fail, "Test steps NOT Completed for Test case " + TestContext.CurrentContext.Test.Name + " ");
testlog.Log(Status.Fail, "Test ended with " + Status.Fail + " – " + errorMessage);
break;
case TestStatus.Skipped:
logstatus = Status.Skip;
testlog.Log(Status.Skip, "Test ended with " + Status.Skip);
break;
default:
logstatus = Status.Pass;
testlog.Log(Status.Pass, "Test steps finished for test case " + TestContext.CurrentContext.Test.Name + " ");
testlog.Log(Status.Pass, "Test ended with " + Status.Pass);
break;
}
}
catch (Exception e)
{
throw e;
}
}
public void TestReportInitialize()
{
if (GetType().Name != typeof(BaseCrudHandler).Name)
{
if (testStarted == false)
ExtentReport.StartReport();
testStarted = true;
}
}
public void LogReport()
{
ExtentReport.LoggingTestStatusExtentReport();
ExtentReport.extent.Flush();
}
}
//Here is my Test class
public class ReportTest
{
static ExtentReport report = new ExtentReport();
[SetUp]
public void SetUp()
{
//calling the method to start the test
report.TestReportInitialize();
}
[Test]
public void GetTest()
{
Console.WriteLine("MyTest");
}
[TearDown]
public void EndOfExecution()
{
report.LogReport();
}
}