首页 > 解决方案 > 如何为动态生成的 XmlSuite 中配置的测试生成 Allure 报告

问题描述

我有一组测试方法(@Test),我可以使用 Gradle 任务运行它们,并且每个测试都有为其生成的 Allure 报告。

但是,当我通过动态构建测试 XmlSuite 来运行相同的测试时,测试会按预期执行,但不会生成 Allure 报告。

如果我将示例方法注释为@Test - 它将生成其 Allure 报告,但不会生成它触发的测试。

有没有办法触发以这种方式执行的测试的 Allure 报告?

    //@Test
    public static void guiTestInit() {  
        // this will read a props file after drag-and-drop and provide data for tests
        Application.launch(GatherInputGui.class);

        TestNG dynoTest = new TestNG();
        XmlSuite suite = new XmlSuite();
        suite.setName("Dyno Suite");
        XmlTest test = new XmlTest(suite);
        test.setName("Dyno Test");

        List<XmlClass> classes = new ArrayList<XmlClass>();

        XmlClass cl = new XmlClass("tests.MyTestClass");

        // Add an arbitrary number of tests/methods to execure
        if (getMethods().isEmpty()) {
            cl.getIncludedMethods().add(new XmlInclude("defaultTestMethod", 0));
        } else {
            for (int i = 0; i < getMethods().size(); i++) {
                cl.getIncludedMethods().add(new XmlInclude(getMethods().get(i), i));
            }
        }

        classes.add(cl);

        test.setXmlClasses(classes);
        List<XmlSuite> suites = new ArrayList<XmlSuite>();
        suites.add(suite);
        dynoTest.setXmlSuites(suites);
        //see the generated XML
        //System.out.println(suite.toXml());
        dynoTest.run();
    }

标签: testngallure

解决方案


最终为诱惑服务器构建了一个启动器。不幸的是,不记得我从哪里得到它的碎片。

package gui;

导入java.io.File;

公共类 AllureLauncherUtils {

public static void launchAllure(){
    try {

        String currentDirectory;
        File file = new File(".");
        currentDirectory = file.getAbsolutePath();
        System.out.println("Current working directory : "+currentDirectory);

        ProcessBuilder procBuilderGenerateReports = new ProcessBuilder();
        ProcessBuilder procBuilderDisplayReports = new ProcessBuilder();

        /** write output and errors to log **/
        File log = new File("ALLURE-LAUNCHER-LOG.log");
        procBuilderGenerateReports.redirectOutput(ProcessBuilder.Redirect.appendTo(log));
        procBuilderGenerateReports.redirectError(ProcessBuilder.Redirect.appendTo(log));
        procBuilderDisplayReports.redirectOutput(ProcessBuilder.Redirect.appendTo( log ));
        procBuilderDisplayReports.redirectError( ProcessBuilder.Redirect.appendTo( log ));

        /**
         *  As part of the installation of the test suite, the Allure server parts reside in a hidden directory '.allure'
         *  There are two files in the '.allure/allure-2.6.0/bin' directory:
         *  'allure' - a UN*X shell script
         *  and
         *  'allure.bat' - windows batch file
         *  They are used to start the embedded Jetty server (depending on your system) to display the test reports.
         *  They add allure and jetty jars to the class path and start the server, so it's impractical to try and
         *  start the allure server in a more granular way
         *
         **/



        /** GENERATE THE REPORTS **/
        procBuilderGenerateReports.command( currentDirectory + "\\.allure\\allure-2.6.0\\bin\\allure.bat", "generate", "--clean");

        Process processGenerateReports = procBuilderGenerateReports.start();
        StreamGobbler generateReportOutputGobbler = new StreamGobbler( processGenerateReports.getInputStream(), "OUTPUT" );
        StreamGobbler generateReportErrorGobbler = new StreamGobbler(processGenerateReports.getErrorStream(), "ERROR");
        generateReportOutputGobbler.start();
        generateReportErrorGobbler.start();


        /** START THE SERVER AND DISPLAY THE REPORTS **/
        procBuilderDisplayReports.command( currentDirectory + "\\.allure\\allure-2.6.0\\bin\\allure.bat", "serve", currentDirectory + "\\build\\allure-results");
        Process processDisplayAllureReports = procBuilderDisplayReports.start();
        StreamGobbler displayReportOutputGobbler = new StreamGobbler( processDisplayAllureReports.getInputStream(), "OUTPUT" );
        StreamGobbler displayReportErrorGobbler = new StreamGobbler(processDisplayAllureReports.getErrorStream(), "ERROR");


        /** capture output and error to console */
        displayReportOutputGobbler.start();
        displayReportErrorGobbler.start();

        //processDisplayAllureReports.destroy();
        //processGenerateReports.destroy();

        System.exit(0);

    } catch (Throwable t) {
        t.printStackTrace();
    }
}

}


推荐阅读