gradle 合并 cucumber json报道

kuuvgm7e  于 2023-10-19  发布在  其他
关注(0)|答案(1)|浏览(80)

我使用cucumber编写测试用例,使用Java 17编写测试NG。
我们创建了多个功能文件,对于每个功能,我们都有一个单独的runner类。
runner类的构造如下:

@CucumberOptions(features = {"src/test/resources/features/wallet/A.feature"},
        glue = {"com.test.a"},
        plugin = {"pretty", "json:build/cucumber-reports/a.json"})
@Test(groups = {"A", "regression"}, alwaysRun = true)
public class RunnerA extends AbstractTestNGCucumberTests {

}

@CucumberOptions(features = {"src/test/resources/features/wallet/B.feature"},
        glue = {"com.test.b"},
        plugin = {"pretty", "json:build/cucumber-reports/b.json"})
@Test(groups = {"B", "regression"}, alwaysRun = true)
public class RunnerB extends AbstractTestNGCucumberTests {

}

当我运行测试用例时,它在cucumber-reports文件夹中生成了两个JSON文件,分别为a.jsonb.json
我如何将这两个json文件合并到一个json文件中。report.json
我使用gradle 7作为cucumber 7.11.0的构建工具
最终目标:我想使用API在Xray中上传json文件

djmepvbi

djmepvbi1#

我通过编写一个自定义gradle任务解决了这个问题。
以下是我的解决方案:
Gradle任务:

task aggregatReport(type: JavaExec) {
    println("Running aggregatReport task")
    description 'Combine multiple cucumber report json file into one json file'
    classpath sourceSets.main.runtimeClasspath
    mainClass = "com.ReportAggregator"
    def reportFolderPath = "./build/cucumber-reports/";
    File file = file(reportFolderPath);
    args file.toPath().toAbsolutePath().toString()
}

代码:

public class ReportAggregator {
    public static void main(String[] args) throws IOException {
        System.out.println("ReportAggregator with argument -> " + Arrays.toString(args));
        ObjectMapper objectMapper = new ObjectMapper();
        File reportFolder = new File(args[0]);
        System.out.println("Report folder  -> " + reportFolder.isDirectory() + " " + reportFolder.exists());
        List<Path> reportFiles = Files.walk(reportFolder.toPath()).filter(file -> file.toFile().getName().endsWith(".json")).toList();
        List<Map<String, Object>> finalReportList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(reportFiles)) {
            System.out.println("Total report file ->" + reportFiles.size());
            reportFiles.forEach(path -> {
                try {
                    String jsonString = FileUtils.readFileToString(path.toFile(), Charset.defaultCharset());
                    TypeReference<List<Map<String, Object>>> typeRef = new TypeReference<>() {
                    };
                    List<Map<String, Object>> hashMaps = objectMapper.readValue(jsonString, typeRef);
                    finalReportList.addAll(hashMaps);
                } catch (IOException e) {
                    System.err.println("Can not read file -> " + path.toFile().getAbsolutePath());
                }
            });

            if (CollectionUtil.isNotEmpty(finalReportList)) {
                String finalReportFileName = reportFolder.getAbsolutePath() + "/final.json";
                File finalReportFile = new File(finalReportFileName);
                if (finalReportFile.exists()) {
                    boolean delete = finalReportFile.delete();
                    System.out.println("File is deleted -> " + delete);
                }
                FileUtils.writeStringToFile(finalReportFile, objectMapper.writeValueAsString(finalReportList), Charset.defaultCharset());
                System.out.println("Report file created");
            } else {
                System.err.println("Empty report is created");
                throw new ReportException("Empty report created");
            }
        } else {
            throw new ReportException("No report file found under folder ->" + reportFolder.getAbsolutePath());
        }
    }
}

和gralde任务相关性设置:

test {
    println("Running test task")
    useTestNG() {
        suites 'src/test/testng.xml'
    }
    finalizedBy aggregatReport
}

task importJunitResultsToXrayCloud() {
    dependsOn 'test'
    description 'Imports Cucumber test results to Xray Test Management for Jira Cloud.'

    doLast {
       //code to upload report
    }
}

上传报告只需运行./gradlew importJunitResultsToXrayCloud

相关问题