2

这就是我的 testNG 测试的样子:-

public class orderTest{
    @Test
    public void meth1() throws InterruptedException{
        System.out.println("1");
        Reporter.log("1");
    }
    @Test
    public void meth2() throws InterruptedException{
        System.out.println("2");
        Reporter.log("2");
    }
    @Test
    public void meth3() throws InterruptedException{
        System.out.println("3");
        Reporter.log("3");
    }
    @Test
    public void meth4() throws InterruptedException{
        System.out.println("4");
        Reporter.log("4");
    }
}

当我在 Eclipse 上运行它时,控制台显示为:- 1 2 3 4 PASSED: meth1 PASSED: meth2 PASSED: meth3 PASSED: meth4

但是当我打开 testNG 报告时,单击报告器输出链接,它显示为:- 报告器输出 - meth1 1 meth4 4 meth3 3 meth2 2

为什么测试报告中的顺序不正确?执行顺序是 1,2,3,4,但报告顺序是 1,4,3,2。

4

2 回答 2

1

也可以在输出报表中按执行顺序显示。为此,您的 TestNG Reporter 应该实现 IReporter。

一个使用它的好插件是ReportNG。您可以覆盖它的generateReport方法,以按其父XML套件顺序在 Html 报告中显示套件,如下所示:

public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectoryName) {
        ...
        Comparator<ISuite> suiteComparator = new TestSuiteComparator(xmlSuites);
        suites.sort(suiteComparator);
        ...
}

其中TestSuiteComparator如下:

public class TestSuiteComparator implements Comparator<ISuite> {

    public List<String> xmlNames;

    public TestSuiteComparator(List<XmlSuite> parentXmlSuites) {
        for (XmlSuite parentXmlSuite : parentXmlSuites) {
            List<XmlSuite> childXmlSuites = parentXmlSuite.getChildSuites();
            xmlNames = new ArrayList<String>();
            xmlNames.add(parentXmlSuite.getFileName());
            for (XmlSuite xmlsuite : childXmlSuites) {
                xmlNames.add(xmlsuite.getFileName());
            }
        }
    }

    @Override
    public int compare(ISuite suite1, ISuite suite2) {
        String suite1Name = suite1.getXmlSuite().getFileName();
        String suite2Name = suite2.getXmlSuite().getFileName();
        return xmlNames.indexOf(suite1Name) - xmlNames.indexOf(suite2Name);
    }
}
于 2015-07-19T10:17:35.390 回答
0

If you want the classes and methods listed in this file to be run in an unpredictible order, set the preserve-order attribute to false.

这就是TestNG文档所说的,所以除非拼写错误(不是说我的拼写是完美的 :),否则这是一个功能。

但在我看来,只有报告顺序是不可预测的,执行似乎是可以预测的。

dtd 说,

@attr preserve-order If true, the classes in this tag will be run in the same order as found in the XML file.

所以这与它实际所做的相匹配。

这没有错,但也许反过来。

似乎是使报告更具吸引力的一项功能:)

于 2013-02-19T14:36:31.347 回答