范围报告:在并行执行测试时,测试步骤将合并到范围报告中的最后一个测试中

时间:2019-06-03 12:49:07

标签: selenium testng extentreports

“测试步骤”和“测试日志”已合并到最后一个测试中。

范围报告3.2

实际报告 功能1日志

enter image description here

功能2日志[具有所有步骤]

enter image description here

我的项目结构是

enter image description here

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 -->

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

3 个答案:

答案 0 :(得分:2)

这是您的问题:

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对象。

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

在创建测试的类中,可以将其设为定义扩展报告类和变量的类的子类。现在,在子类(具有测试)中,您可以创建多个范围测试实例。

因此,为每个新测试创建一个新实例