1

这会导致大量的调试混乱,我认为必须有一种方法来防止这种情况。现在,如果一个测试方法不存在(比如拼写错误),套件将跳过该方法并继续下一个没有问题的方法。这会导致很多问题,而且很难找到原因。这是一个例子:

    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Sampoe_testSuite" preserve-order="true">
    <listeners>
        <listener class-name="framework.Listener"/>
    </listeners>
    <test name="Sample_TestSuite-Part1" preserve-order="true">
        <classes>
            <class name="tests.FirstTest">
                <methods>
                    <include name="firstMethod"/>
                </methods>
            </class>
            <class name="tests.SecondTest">
                <methods>
                    <include name="secondMethod"/>
                    <include name="thirdMethod"/>
                </methods>
            </class>
            <class name="tests.ThirdTest">
                <methods>
                    <include name="fourthMethod"/>
                </methods>
            </class>
        </classes>
    </test>
</suite>

假设我拼错了 SecondTest 的 secondMethod。它实际上是代码中的 sceondMethod。当我运行这个套件时,不会有错误,但会发生什么:

Runs FirstTest.firstMethod
Runs SecondTest.thirdMethod
Runs ThirdTest.fourthMethod

请注意,它只是跳过拼写错误的方法并愉快地继续。我希望它使套件失败并说缺少方法。那可能吗?

4

1 回答 1

1

您必须实现自己的SuiteListener类来实现ISuiteListener. 实现onStart将 XML 文件中包含的方法与测试目录中存在的方法进行比较的方法。如果有,使用System.exit(0)停止执行。

import org.testng.*;
import org.testng.annotations.Test;
import org.testng.xml.XmlInclude;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SuiteListener implements ISuiteListener {

    @Override
    public void onStart(ISuite suite) {
        // getting included method names in XML suite file
        List<String> inclduedMethodsInXmlSuite =
                suite.getXmlSuite().getTests().stream()
                        .flatMap(xmlTest -> xmlTest.getClasses().stream()
                                .flatMap(xmlClass -> xmlClass.getIncludedMethods().stream()
                                        .map(XmlInclude::getName)))
                        .collect(Collectors.toList());

        // getting all test methods
        List<String> testMethods = new ArrayList<>();
        try (Stream<Path> paths = Files.walk(Paths.get("./src/test/java"))) { // path to test classes directory
            testMethods = paths
                    .filter(path -> path.getFileName().toString().endsWith(".java")) // filtering only classes, not directories
                    .map(path -> path.getParent().getFileName().toString() + "." + path.getFileName().toString()) // mapping to qualified name, e.g test.TestClass
                    .map(file -> file.substring(0, file.length() - 5)) // removing ".java"
                    .map(clazz -> {
                                try {
                                    return Class.forName(clazz); // casting to Class object
                                } catch (ClassNotFoundException e) {
                                    e.printStackTrace();
                                    return null;
                                }
                            }
                    )
                    .flatMap(clazz -> Arrays.stream(clazz.getDeclaredMethods()) // getting methods of a test class annotated with @Test
                            .filter(method -> method.isAnnotationPresent(Test.class)))
                    .map(Method::getName) // mapping to method name
                    .collect(Collectors.toList());
        } catch (IOException e) {
            e.printStackTrace();
        }

        // checking if any of xml suite methods does not exist in test methods
        final List<String> finalTestMethods = testMethods;
        List<String> notSupprtedMethods = inclduedMethodsInXmlSuite.stream()
                .filter(xmlMethod -> !finalTestMethods.contains(xmlMethod))
                .collect(Collectors.toList());

        if (notSupprtedMethods.size() > 0) {
            System.out.println("Unsupported methods in XML Suite file:");
            notSupprtedMethods.forEach(System.out::println);
            System.exit(0);
        }

    }

    @Override
    public void onFinish(ISuite suite) {

    }
}

于 2020-11-10T19:06:39.087 回答