2

我正在使用 Junit 运行 Cucumber+Serenity 测试:

代码片段:

    JUnitCore engine = new JUnitCore();
    engine.addListener(new TextListener(System.out));
    engine.run(featureClass);

对于每个测试执行,都会生成单独的 html 报告,但不会直接使用 HtmlAggregateStoryReporter 生成聚合(组合)报告(参考:https ://github.com/serenity-bdd/serenity-core/issues/244 )

这是我使用的代码片段,它被挂起并且聚合未完成。我在所有 Cucumber+Serenity 测试完成后调用:

        HtmlAggregateStoryReporter reporter = new HtmlAggregateStoryReporter("PoC-Test");          
        File sourceDirectory = new File("C:\\PoC-Test\\target\\site\\serenity\\");
        reporter.setSourceDirectory(sourceDirectory);
  reporter.generateReportsForTestResultsFrom(reporter.getSourceDirectory());

C:\PoC-Test\target\site\serenity\ 是生成各个测试报告的位置,您能帮我找出这段代码有什么问题吗?

请分享任何示例工作代码,如果有的话?

4

2 回答 2

1

HtmlAggregateStoryReporter - 生成 HTML 格式的聚合验收测试报告。从输出目录读取所有报告以生成汇总结果的 HTML 报告。此类连接到 JIRA 并在报告中生成需求选项卡。

类的源代码 => HtmlAggregateStoryReporter.java

该课程要求:

  • 源目录:修昔底德测试报告从这里读取
  • 输出目录:此处生成汇总报告
  • 吉拉详细信息
    • issueTrackerUrl:用于生成问题编号链接的问题跟踪系统的 URL。
    • jiraUrl : JIRA 的基本 URL,如果您使用 JIRA 作为问题跟踪系统,如果指定此属性,则无需指定 issueTrackerUrl。
    • jira 用户名和密码:jira 凭据

代码片段:

private void generateHtmlStoryReports() throws IOException {
        getReporter().setSourceDirectory(sourceOfTestResult());
        getReporter().setOutputDirectory(outputDirectory);
        getReporter().setIssueTrackerUrl(issueTrackerUrl);
        getReporter().setJiraUrl(jiraUrl);
        getReporter().setJiraProject(jiraProject);
        getReporter().setJiraUsername(jiraUsername);
        getReporter().setJiraPassword(jiraPassword);
        getReporter().generateReportsForTestResultsFrom(sourceOfTestResult());
    }

完整代码:(引用自massapi )

import net.thucydides.core.Thucydides;
import net.thucydides.core.ThucydidesSystemProperty;
import net.thucydides.core.guice.Injectors;
import net.thucydides.core.reports.html.HtmlAggregateStoryReporter;
import net.thucydides.core.util.EnvironmentVariables;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.File;
import java.io.IOException;
import java.util.Locale;

/**
* Generate aggregate XML acceptance test reports.
x *
*/
@Mojo(name = "aggregate", requiresProject = false)
public class ThucydidesAggregatorMojo extends AbstractMojo {

    /**
     * Aggregate reports are generated here
     */
    @Parameter(property = "thucydides.outputDirectory", defaultValue = "${project.build.directory}/site/thucydides", required=true)
    public File outputDirectory;

    /**
     * Thucydides test reports are read from here
     */
    @Parameter(property = "thucydides.source", defaultValue = "${project.build.directory}/site/thucydides", required=true)
    public File sourceDirectory;

    /**
     * URL of the issue tracking system to be used to generate links for issue numbers.
     */
    @Parameter
    public String issueTrackerUrl;

    /**
     * Base URL for JIRA, if you are using JIRA as your issue tracking system.
     * If you specify this property, you don't need to specify the issueTrackerUrl.
     */
    @Parameter
    public String jiraUrl;

    @Parameter
    public String jiraUsername;

    @Parameter
    public String jiraPassword;

    /**
     * JIRA project key, which will be prepended to the JIRA issue numbers.
     */
    @Parameter
    public String jiraProject;

    /**
     * Base directory for requirements.
     */
    @Parameter
    public String requirementsBaseDir;

    EnvironmentVariables environmentVariables;

    /**
     * Thucydides project key
     */
    @Parameter(property = "thucydides.project.key", defaultValue = "default")
    public String projectKey;

    protected void setOutputDirectory(final File outputDirectory) {
        this.outputDirectory = outputDirectory;
    }

    protected void setSourceDirectory(final File sourceDirectory) {
        this.sourceDirectory = sourceDirectory;
    }

    public void prepareExecution() {
        if (!outputDirectory.exists()) {
            outputDirectory.mkdirs();
        }
        configureEnvironmentVariables();
    }

    private EnvironmentVariables getEnvironmentVariables() {
        if (environmentVariables == null) {
            environmentVariables = Injectors.getInjector().getProvider(EnvironmentVariables.class).get() ;
        }
        return environmentVariables;
    }

    private void configureEnvironmentVariables() {
        Locale.setDefault(Locale.ENGLISH);
        updateSystemProperty(ThucydidesSystemProperty.THUCYDIDES_PROJECT_KEY.getPropertyName(), projectKey, Thucydides.getDefaultProjectKey());
        updateSystemProperty(ThucydidesSystemProperty.THUCYDIDES_TEST_REQUIREMENTS_BASEDIR.toString(),
                             requirementsBaseDir);
    }

    private void updateSystemProperty(String key, String value, String defaultValue) {
        if (value != null) {
            getEnvironmentVariables().setProperty(key, value);
        } else {
            getEnvironmentVariables().setProperty(key, defaultValue);
        }
    }

    private void updateSystemProperty(String key, String value) {
        if (value != null) {
            getEnvironmentVariables().setProperty(key, value);
        }
    }

    private HtmlAggregateStoryReporter reporter;

    protected void setReporter(final HtmlAggregateStoryReporter reporter) {
        this.reporter = reporter;
    }

    public void execute() throws MojoExecutionException {
        prepareExecution();

        try {
            generateHtmlStoryReports();
        } catch (IOException e) {
            throw new MojoExecutionException("Error generating aggregate thucydides reports", e);
        }
    }

    protected HtmlAggregateStoryReporter getReporter() {
        if (reporter == null) {
            reporter = new HtmlAggregateStoryReporter(projectKey);
        }
        return reporter;

    }

    private void generateHtmlStoryReports() throws IOException {
        getReporter().setSourceDirectory(sourceOfTestResult());
        getReporter().setOutputDirectory(outputDirectory);
        getReporter().setIssueTrackerUrl(issueTrackerUrl);
        getReporter().setJiraUrl(jiraUrl);
        getReporter().setJiraProject(jiraProject);
        getReporter().setJiraUsername(jiraUsername);
        getReporter().setJiraPassword(jiraPassword);
        getReporter().generateReportsForTestResultsFrom(sourceOfTestResult());
    }

    private File sourceOfTestResult() {
        if ((sourceDirectory != null) && (sourceDirectory.exists())) {
            return sourceDirectory;
        } else {
            return outputDirectory;
        }

    }
}
于 2018-06-26T12:04:09.360 回答
1

您是否将 Serenty 的aggregate目标添加到您的构建中?您使用的是什么构建工具?

这是一个适用于 Maven 的解决方案:

任何一个

  1. serenity:aggregate目标添加到您的通话声明中。这将运行您的构建并执行报告的聚合。例如:

mvn test -Dserenity.outputDirectory=C:/PoC-Test/target/site/serenity serenity:aggregate

或者

  1. 只需serenity:aggregate在构建完成后调用,例如:

mvn serenity:aggregate -Dserenity.outputDirectory=C:/PoC-Test/target/site/serenity

于 2016-12-06T10:44:09.343 回答