打算写一下springboot调用tesng并产出报告

添加依赖

 <dependency>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
                <version>7.4.0</version>
            </dependency>

创建Controller

@RestController
public class TestController {

    @GetMapping("/run-tests")
    public String runTests() throws IOException {
        TestNG testng = new TestNG();
        testng.setTestSuites(Collections.singletonList("src/main/resources/testng.xml"));
        testng.run();
        // 移动文件
        moveTestOutputToStatic();
        return "TestNG tests executed. Check the test-output directory for reports.";
    }
    private void moveTestOutputToStatic() throws IOException {
        String projectPath = System.getProperty("user.dir");
        Path sourceDir = Paths.get(projectPath + "/test-output");
        Path targetDir = Paths.get(projectPath + "/target/classes/static/test-output");

        if (!Files.exists(targetDir)) {
            Files.createDirectories(targetDir);
        }

        Files.walk(sourceDir).forEach(sourcePath -> {
            try {
                Path targetPath = targetDir.resolve(sourceDir.relativize(sourcePath));
                Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }
}

编写文件

<suite name="Test Suite" verbose="1">
    <test name="Test Test">
        <classes>
            <class name="com.example.jvmdd.ExampleTest"/>
        </classes>
    </test>

</suite>

编写testcase

package com.example.jvmdd;


import org.testng.Assert;
import org.testng.annotations.Test;

public class ExampleTest {

    @Test
    public void testMethod1() {
        System.out.println("Test Method 1");
        Assert.assertEquals(1,2);
    }

    @Test
    public void testMethod2() {
        System.out.println("Test Method 2");
    }
}

操作步骤
http://localhost:8080/run-tests
报告查看
http://localhost:8080/test-output/index.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值