Open giang-hai-nguyen opened 8 years ago
.NET or Java? Which TestRunner? Can you share a sample?
Hi Anshooarora, I am using extentreports .NET and NUnit testing in VS. Sorry but my project have a little complex so i don't know how to share a sample here.
thanks
Are you logging the same was as in shown in the TearDown block of this example?
Hi Anshoo, I tried to use the example above to run TCs but i want to store test report for each run time into dynamic folder therefore i cannot use it for hard code test file. private static readonly ExtentReports _instance =new ExtentReports("Extent.Net.html", DisplayOrder.OldestFirst);
Could you please help me if there is any way to custom the example above for dynamic folder?
thanks,
Hi Anshoo, i noticed that i am using AppendChild method. Maybe this is problem since if i run single TC it works fine
Hi Anshoo, Here is my code. ExtentBase class
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using RelevantCodes.ExtentReports;
using System;
using System.IO;
namespace Test_Nunit
{
public abstract class ExtentBase
{
protected ExtentReports extentReports;
protected ExtentReports extentReportSummary;
protected static ExtentTest test;
protected static ExtentTest testSummary;
static string driverDirectory = Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location).Parent.Parent.Parent.FullName;
protected string resultDirectory = Path.Combine(driverDirectory, "TestResults");
protected string reportName, screenshotName, reportSummaryName;
static string resultSummaryDirectory = Path.Combine(Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location).Parent.Parent.Parent.FullName, "TestResults", "ReportsSummary", DateTime.Now.ToString("yyyy-MM-dd_HH.mm.ss"));
[OneTimeSetUp]
public void OneTimeSetUp()
{
reportSummaryName = "TestSummary_" + DateTime.Now.ToString("yyyy-MM-dd") + ".html";
if (!Directory.Exists(resultSummaryDirectory))
{
Directory.CreateDirectory(resultSummaryDirectory);
}
extentReportSummary = new ExtentReports(resultSummaryDirectory + "\\" + reportSummaryName, false);
testSummary = extentReportSummary.StartTest(TestContext.CurrentContext.Test.Name, "Report summary");
}
[SetUp]
public void SetUp()
{
StartTest();
}
[TearDown]
public void TearDown()
{
CreateReportResult();
}
[OneTimeTearDown]
public void OneTimeTearDown()
{
extentReportSummary.Flush();
extentReportSummary.EndTest(testSummary);
}
protected void StartTest()
{
resultDirectory = Path.Combine(resultDirectory, DateTime.Now.ToString("yyyy-MM-dd"), TestContext.CurrentContext.Test.MethodName);
if (!Directory.Exists(resultDirectory))
{
Directory.CreateDirectory(resultDirectory);
}
resultDirectory += "\\";
reportName = TestContext.CurrentContext.Test.MethodName + "_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".html";
screenshotName = TestContext.CurrentContext.Test.MethodName + "_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".png";
extentReports = new ExtentReports(resultDirectory + reportName, true);
test = extentReports.StartTest(TestContext.CurrentContext.Test.MethodName, "");
}
protected void CreateReportResult()
{
var status = TestContext.CurrentContext.Result.Outcome.Status;
var stacktrace = string.IsNullOrEmpty(TestContext.CurrentContext.Result.StackTrace)
? ""
: string.Format("<pre>{0}</pre>", TestContext.CurrentContext.Result.StackTrace);
LogStatus logstatus;
switch (status)
{
case TestStatus.Failed:
logstatus = LogStatus.Fail;
break;
case TestStatus.Skipped:
logstatus = LogStatus.Skip;
break;
case TestStatus.Inconclusive:
logstatus = LogStatus.Unknown;
break;
default:
logstatus = LogStatus.Pass;
break;
}
test.Log(logstatus, "Test ended, result is: " + logstatus + stacktrace);
testSummary.AppendChild(test);
extentReports.Flush();
extentReports.EndTest(test);
resultDirectory = Path.Combine(driverDirectory, "TestResults");
}
protected void stepLogging(String logMessage)
{
test.Log(LogStatus.Info, "Test step: " + logMessage);
}
}
}
Test1 class
using NUnit.Framework;
using RelevantCodes.ExtentReports;
namespace Test_Nunit
{
[TestFixture]
public class Test1: ExtentBase
{
[Test]
public void Test_001()
{
stepLogging("Info");
stepLogging("Info");
stepLogging("Info");
stepLogging("Info");
test.Log(LogStatus.Pass, "Pass");
// intentional failure
Assert.True(test.GetCurrentStatus() == LogStatus.Fail);
}
}
}
Test2 class
using NUnit.Framework;
using RelevantCodes.ExtentReports;
namespace Test_Nunit
{
[TestFixture]
public class Test1: ExtentBase
{
[Test]
public void Test_002()
{
stepLogging("Info");
stepLogging("Info");
stepLogging("Info");
stepLogging("Info");
test.Log(LogStatus.Pass, "Pass");
// intentional failure
Assert.True(test.GetCurrentStatus() == LogStatus.Fail);
}
}
}
Thanks, will update you soon.
@anshooarora do you have any solution here?
I noticed that the problem happens not only in first TC but also all TCs except the last one.
Hey @anshooarora, could you please re-check & have some update here?
I will check today and revert back. Will provide a solution with NUnit version 3.5.
Here you go:
/* the class setup class */
[SetUpFixture]
public abstract class Base
{
protected ExtentReports _extent;
protected ThreadLocal<ExtentTest> _parentTest;
protected ThreadLocal<ExtentTest> _test;
[OneTimeSetUp]
protected void OneTimeSetUp()
{
_parentTest = new ThreadLocal<ExtentTest>();
_test = new ThreadLocal<ExtentTest>();
string dir = TestContext.CurrentContext.TestDirectory + "\\";
var fileName = this.GetType().ToString() + ".html";
_extent = new ExtentReports(dir + fileName);
}
[SetUp]
public void BeforeClass()
{
var testContext = TestContext.CurrentContext.Test;
var parentTest = _extent.StartTest(testContext.ClassName);
_parentTest.Value = parentTest;
}
[TearDown]
protected void AfterClass()
{
_extent.EndTest(_parentTest.Value);
_extent.Flush();
}
}
/* the test setup class */
[TestFixture]
public abstract class TestHooks : Base
{
[SetUp]
public void BeforeTest()
{
var test = _extent.StartTest(TestContext.CurrentContext.Test.Name);
_test.Value = test;
}
[TearDown]
public void AfterTest()
{
var test = _test.Value;
var status = TestContext.CurrentContext.Result.Outcome.Status;
var stacktrace = string.IsNullOrEmpty(TestContext.CurrentContext.Result.StackTrace)
? ""
: string.Format("<pre>{0}</pre>", TestContext.CurrentContext.Result.StackTrace);
LogStatus logstatus;
switch (status)
{
case TestStatus.Failed:
logstatus = LogStatus.Fail;
break;
case TestStatus.Skipped:
logstatus = LogStatus.Skip;
break;
case TestStatus.Inconclusive:
logstatus = LogStatus.Unknown;
break;
default:
logstatus = LogStatus.Pass;
break;
}
test.Log(logstatus, "Test ended, result is: " + logstatus + stacktrace);
_parentTest.Value.AppendChild(_test.Value);
}
}
/* tests */
[TestFixture]
public class Tests : TestHooks
{
[Test]
public void Test_001()
{
var test = _test.Value;
test.Log(LogStatus.Pass, "Pass");
// intentional failure
Assert.True(test.GetCurrentStatus() == LogStatus.Fail);
}
}
Please let me know if it still does not work correctly.
Hi @anshooarora , Thank you for your time to help me on this issue. I have tried your code and it seem still not work correctly. I put one more test case into Tests class and the result as below screenshot (even though the result icons are correct):
Please kindly help me re-check one more time.
Hi Anshooarora, I have run all TCs in multiple classes and wrote them into 1 html file. And i realize that All LogStatus icons are Pass in first TC. Please see screenshot below & kindly help me on this situation.