范围报告仅显示最后一个测试用例结果

时间:2017-01-11 12:29:40

标签: java selenium-webdriver extent

范围报告版本 - 3.0 语言 - Java和TestNG类

我有一个类 - ExtentManager.java

    package framewrk;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;

public class ExtentManager {

    private static ExtentReports extent;
    private static ExtentTest test;
    private static ExtentHtmlReporter htmlReporter;
    private static String filePath = "./extentreport.html";

    public static ExtentReports GetExtent(){
        extent = new ExtentReports();

        htmlReporter = new ExtentHtmlReporter(filePath);

        // make the charts visible on report open
        htmlReporter.config().setChartVisibilityOnOpen(true);

        // report title
        String documentTitle = prop.getProperty("documentTitle", "aventstack - Extent");
        htmlReporter.config().setDocumentTitle(documentTitle);
}

    public static ExtentTest createTest(String name, String description){
        test = extent.createTest(name, description);
        return test;
    }

    public static ExtentTest createTest(String name){
        test = extent.createTest(name, "");
        return test;
    }
}

和2个testNG类如下 TC1.java

package framewrk;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;

public class TC1 {
    static ExtentReports extent;
    static ExtentTest test;

    @BeforeClass
    public void setup(){
        extent = ExtentManager.GetExtent();
    }

    @Test
    public void OpenUT(){
        test = extent.createTest("Testing how fail works");
        test.log(Status.INFO, "fail check started");
        test.fail("Test fail");
    }

    @AfterClass
    public void tear()
    {
        extent.flush();
    }
}

TC2.java

package framewrk;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;

public class TC2 {
    static ExtentReports extent;
    static ExtentTest test;

    @BeforeClass
    public void setup(){
    extent = ExtentManager.GetExtent();
    }

    @Test
    public void OpenUT(){
        test = extent.createTest("Testing how pass works");
        test.log(Status.INFO, "pass check started");
        test.pass("Passed");
    }

    @AfterClass
    public void tear()
    {
        extent.flush();
    }
}

如果运行这两个测试用例,我只获得最后的测试用例结果,对于第一个测试用例结果,它不会显示在范围报告上。 请注意,范围报告3.0没有附加参数。 如何在范围报告中获得所有测试用例结果?

Only last test case result shown, how to get all tests results

4 个答案:

答案 0 :(得分:1)

在上述方法中,您将在每个类中创建一个新的范围报告。这就是为什么你只得到最新执行的测试结果。

您可以为TC1和TC2类创建公共超类。在超类中,您可以创建@AfterClass和@BeforeClass函数。然后它应该工作。

希望它有所帮助!

答案 1 :(得分:0)

我得到了一种效果很好的方法,首先检查扩展对象是否已经创建?如果是,则返回对象而无需重新初始化扩展对象,这是它的外观

在ExtentManager类下(如问题所示),添加此代码块

 @Component({
  selector: 'app-root',
  templateUrl: './app.component.html',   
  styles: ['./app.component.css']
})

现在在您的testNG测试下,在类注释之前,调用上述方法

public static ExtentReports getInstance() {
        if(extent == null) {
            GetExtent();
        }   
        return extent;
    }

答案 2 :(得分:0)

在您的情况下 ExtentManager.GetExtent(),此方法将覆盖先前创建的报告,因此,只有最后一个测试结果才会显示在报告中。确保仅在整个测试开始过程中才调用此方法,最好的方法是通过实现ITestListener来实现

答案 3 :(得分:-1)

Use this method extent.flush() in @aftersuite. because this statement generates the result

 {   
public class TC1 
{
    static ExtentReports extent;
    static ExtentTest test;

    @BeforeClass
    public void setup(){
        extent = ExtentManager.GetExtent();
    }

    @Test
    public void OpenUT(){
        test = extent.createTest("Testing how fail works");
        test.log(Status.INFO, "fail check started");
        test.fail("Test fail");
    }

    @Test
    public void OpenUT1(){
        test = extent.createTest("Testing how pass works");
        test.log(Status.INFO, "pass check started");
        test.pass("Passed");
    }

    @aftersuite
    public void tear()
    {
        extent.flush();
    }
}
相关问题