anshooarora / extentreports-csharp

Community version of Extent API for .NET has moved to https://github.com/extent-framework/
http://extentreports.com/
Other
47 stars 43 forks source link

i am running nunit 3 with parallelizable attribute and only the last module test results is displayed in the html report #59

Open xenaree opened 7 years ago

xenaree commented 7 years ago

Only the second TestFixture is displayed in the html report

using NUnit.Framework;
using OpenQA.Selenium;
using Seloger.com.seloger.common.custom;
using SeLoger.com.seloger.Model;
using System;
using RelevantCodes.ExtentReports;
using System.Reflection;

namespace Seloger.com.seloger.controller.Search
{
    [TestFixture("chrome")]
    [TestFixture("firefox")]
    class AfficherTypeProjet
    {
        private static IWebDriver _driver;
        private string _browser;
        public ExtentReports _report;
        ExtentTest _test;

        public AfficherTypeProjet(string browser)
        {
            _browser = browser;
        }

        [OneTimeSetUp]
        public void OneTimeSepUp()
        {
            string pth = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
            string actualPath = pth.Substring(0, pth.LastIndexOf("bin"));
            string projectPath = new Uri(actualPath).LocalPath;

            string reportpath = projectPath + "com.seloger.reports\\AfficherTypeProjet-"+DateTime.Now.ToString("yyyyMMdd") +".html";

            _report = new ExtentReports(reportpath, true);
            _report.AddSystemInfo("HostName", "Kais")
                .AddSystemInfo("Environment", "PROD")
                .AddSystemInfo("User Name", "Kais KHALIFA");
            _report.LoadConfig(projectPath + "extentReport-config.xml");
        }

        [SetUp]
        public void SetUp()
        {
            //_driver = Custom.DriverSetupCBT(_browser, Constant.sSearchPage, this.GetType().Name+" : "+ TestContext.CurrentContext.Test.Name);
            _driver = Custom.DriverSetupLocal(_browser, Constant.sSearchPage);
        }

        [TearDown]
        public void TearDown()
        {   _report.EndTest(_test);
            _driver.Dispose();
        }

        [OneTimeTearDown]
        public void OneTimeTearDown()
        {
            _report.Flush();
            _report.Close();
        }

        [Test Category("1")]
        public void ProjetElementAffiche()
        {
            _test = _report.StartTest(MethodBase.GetCurrentMethod().Name + "(" + _driver.ToString() + ")", 
                MethodBase.GetCurrentMethod().Name);
            #region Step 1 : Cliquer sur "Projet" et vérifier que les éléments de "Projet" sont affichés"
            BarreDeRecherche.Projet(_driver).Click();
            try
            {
                Assert.True(Projet.Acheter(_driver).Displayed);
                _test.Log(LogStatus.Pass, "'Projet' est affiché");
                Assert.True(Projet.Louer(_driver).Displayed);
                _test.Log(LogStatus.Pass, "'Louer' est affiché");
                Assert.True(Projet.ConstruireMaison(_driver).Displayed);
                _test.Log(LogStatus.Pass, "'Constuire sa maison' est affiché");
                Assert.True(Projet.Investir(_driver).Displayed);
                _test.Log(LogStatus.Pass, "'Investir' est affiché");
                Assert.True(Projet.Viager(_driver).Displayed);
                _test.Log(LogStatus.Pass, "'Viager' est affiché");
                _test.Log(LogStatus.Pass, "Le test s'est terminé avec succés");
            }
            catch (Exception e)
            { 
                _test.Log(LogStatus.Fail, "Test Failed with error: " + e.ToString());
            }
            #endregion
        }
    }
}
anshooarora commented 7 years ago

This is because a new instance is created and the last instance is overwriting all the previous runs. Can this code come from a static class instead of a new instance each time the method runs:

string reportpath = projectPath + "com.seloger.reports\\AfficherTypeProjet-"+DateTime.Now.ToString("yyyyMMdd") +".html";

_report = new ExtentReports(reportpath, true);
_report.AddSystemInfo("HostName", "Kais")
    .AddSystemInfo("Environment", "PROD")
    .AddSystemInfo("User Name", "Kais KHALIFA");
_report.LoadConfig(projectPath + "extentReport-config.xml");

Example:

if (_report == null)
    _report = ExtentManager.Instance;

ExtentManager (http://extentreports.com/docs/versions/3/net/#helper-classes):

public class ExtentManager
{
    private static readonly ExtentReports _instance = new ExtentReports();

    static ExtentManager()
    {
        var htmlReporter = new ExtentHtmlReporter("Extent.html");
        Instance.AttachReporter(htmlReporter);
    }

    private ExtentManager() { }

    public static ExtentReports Instance
    {
        get
        {
            return _instance;
        }
    }
}
xenaree commented 7 years ago

Thank you for your reply. Shouldn't this fix the issue? _report = new ExtentReports(reportpath, false) instead of true?

anshooarora commented 7 years ago

It may but I wouldn't recommend this in a parallel fashion as 2 threads writing to the same file may cause conflicts. You can make this block synchronized and it should work but a better way still is using the above approach.