【问题标题】:Extent Reports: Test Steps are getting merged in last test in Extent Reports, When executing test in Parallel范围报告:在并行执行测试时,测试步骤正在范围报告中的最后一个测试中合并
【发布时间】:2020-04-18 02:11:30
【问题描述】:

测试步骤和测试日志正在合并到最后一个测试中。

范围报告 3.2

实际报告 功能1日志

函数 2 日志 [包含所有步骤]

我的项目结构是

HomePage.java

package pom;

import test.BaseTest;

public class HomePage extends BaseTest
{

    public void setClick()
    {
        test.pass("This test is pass which is in click of home page");

    }

    public void setName()
    {
        test.fail("This test is fail which is in set of home page");

    }

    public void select()
    {
        test.pass("This test is info which is in selct of home page");

    }
}

Test1.java

package test;

import org.testng.annotations.Test;

import pom.HomePage;

public class Test1 extends BaseTest
{

    @Test
    public void funtion1() 
    {
        HomePage hp = new HomePage();
        hp.setName();
        hp.setClick();
        hp.select();

        test.pass("Test is Passed! ins funtion 2");

    }
}

Test2.java


package test;

import org.testng.annotations.Test;

import pom.HomePage;

public class Test2 extends BaseTest
{
    @Test
    public void funtion2()
    {
        HomePage hp = new HomePage();
        hp.setClick();
        hp.select();

        test.pass("Test is Passed!");

    }
}


BaseTest.Java

package test;
import java.lang.reflect.Method;

import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;



import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.markuputils.ExtentColor;
import com.aventstack.extentreports.markuputils.MarkupHelper;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.ChartLocation;
import com.aventstack.extentreports.reporter.configuration.Theme;

public class BaseTest
{
    public static ExtentHtmlReporter htmlReporter;
    public static ExtentReports extent;
    public static ExtentTest test;

    @BeforeSuite
    public void setUp()
    {
        htmlReporter = new ExtentHtmlReporter("./Reports/MyOwnReport.html");
        extent = new ExtentReports();
        extent.attachReporter(htmlReporter);

        extent.setSystemInfo("OS", "Mac Sierra");
        extent.setSystemInfo("Host Name", "Jayshreekant");
        extent.setSystemInfo("Environment", "QA");
        extent.setSystemInfo("User Name", "Jayshreekant S");

        htmlReporter.config().setChartVisibilityOnOpen(false);
        htmlReporter.config().setDocumentTitle("AutomationTesting.in Demo Report");
        htmlReporter.config().setReportName("My Own Report");
        htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
        //htmlReporter.config().setTheme(Theme.DARK);
        htmlReporter.config().setTheme(Theme.STANDARD);
    }

    @BeforeMethod
    public void startTest(Method m)
    {
        test = extent.createTest(m.getName(),"This is the description of Test" + m.getName());

    }


    @AfterMethod
    public void getResult(ITestResult result)
    {
        if(result.getStatus() == ITestResult.FAILURE)
        {
            test.log(Status.FAIL, MarkupHelper.createLabel(result.getName()+" Test case FAILED due to below issues:", ExtentColor.RED));
            test.fail(result.getThrowable());
        }
        else if(result.getStatus() == ITestResult.SUCCESS)
        {
            test.log(Status.PASS, MarkupHelper.createLabel(result.getName()+" Test Case PASSED", ExtentColor.GREEN));
        }
        else
        {
            test.log(Status.SKIP, MarkupHelper.createLabel(result.getName()+" Test Case SKIPPED", ExtentColor.ORANGE));
            test.skip(result.getThrowable());
        }
    }

    @AfterSuite
    public void tearDown()
    {
        extent.flush();
    }
}

testngall.xml

<suite name="Suite" parallel="tests">
    <test name="Test 1 ">
        <classes>
            <class name="test.Test1" />
        </classes>
    </test>
    <test name="Test 2">
        <classes>
            <class name="test.Test2" />
        </classes>
    </test>
</suite> <!-- Suite -->

这就是整个项目的代码结构,我得到了上次测试中附加的日志

【问题讨论】:

  • 你能把你宝贵的cmets贴出来吗? @Anshoo
  • 您的测试不是线程安全的。您必须为 ThreadLocal 类实例变量。像这样,private static ThreadLocal test = new InheritableThreadLocal();

标签: selenium testng extentreports


【解决方案1】:

这是你的问题:

public static ExtentTest test;

由于这是静态的,因此只有一个实例。当您并行运行测试时,此@BeforeMethod 会被调用两次。

@BeforeMethod
public void startTest(Method m)
{
    test = extent.createTest(m.getName(),"This is the description of Test" + m.getName());

}

第二次调用第一个测试可能还没有完成,但它仍在引用测试对象,因此您将获得第二个测试的输出以及第一个测试的某些部分尚未完成运行点 @BeforeMethod 被调用。 您将需要重写代码以不使用静态 test 对象。

【讨论】:

  • 如果删除 static 关键字,它会在 HomePage.java 中的 test.pass("This test is pass which is in click of home page"); 行抛出 NullPointerException,这也不起作用。
  • 这是TestNG的基本功能,每次执行测试时都会执行基类方法,不需要单独的实例,beforemethod实例每次都会创建一个新的测试实例。这就是范围报告的工作方式。
  • TestNG 不是魔术,它不能创建在基类中定义为单个对象的多个对象实例
  • 是的,如果你把代码放在前面的注解中,它将在运行时创建。
  • 每次调用@BeforeMethod@BeforeClass 注解时,它都会为对象分配一些新的东西。它绝对不会创建静态对象的多个实例。
【解决方案2】:

为了保证您的并行执行线程安全,您的 ExtentTest 必须使用 ThreadLocal 类实例变量。试试看,

private static ThreadLocal<ExtentTest> test = new InheritableThreadLocal<>();

【讨论】:

    【解决方案3】:

    在您创建测试的类中,您可以使其成为您定义范围报告类和变量的类的子类。现在在子类(有测试)中,您可以创建多个 Extent Test 实例。

    所以为每个新测试创建一个新实例

    【讨论】:

      猜你喜欢
      • 2019-03-19
      • 2019-06-09
      • 1970-01-01
      • 2021-03-06
      • 2018-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多