什么控制TestNG中的组测试顺序?

时间:2015-01-10 02:38:45

标签: java unit-testing testng

当我不使用dependsonGroup = X或priority = X等功能时,TestNG如何确定在组中运行测试的顺序?

长话短说,我正在看这个测试网站:

http://www.tutorialspoint.com/testng/testng_group_test.htm

我剪切并粘贴了3个示例文件的行,并尝试了解TestNG和组测试。当我运行它时,输出与网站所说的匹配。

结果良好:

Inside testPrintMessage()
.com
Inside testExitMessage()
www..com

===============================================
Suite1
Total tests run: 2, Failures: 1, Skips: 0
===============================================

接下来我做的是思考我正在整理代码并修复错字,这给了我一个奇怪的结果。从GroupTestExample.java,我将方法从testingExitMessage更改为testExitMessage。我不知道3个字母如何改变正在运行的测试顺序。

错误结果 - 查看测试函数的顺序如何交换,这会影响失败次数

Inside testExitMessage()
www..com
Inside testPrintMessage()
www..com

===============================================
Suite1
Total tests run: 2, Failures: 2, Skips: 0
===============================================

有人可以向我解释可能发生的事情,为什么更改方法名称会使测试的顺序发生变化?

MessageUtil.java

/*
* This class prints the given message on console.
*/
public class MessageUtil {
    private String message;

    // Constructor
    // @param message to be printed
    public MessageUtil(String message) {
        this.message = message;
    }

    // prints the message
    public String printMessage() {
        System.out.println(message);
    return message;
    }

    // add "tutorialspoint" to the message
    public String salutationMessage() {
        message = "tutorialspoint" + message;
    System.out.println(message);
    return message;
    }

    // add "www." to the message
    public String exitMessage() {
    message = "www." + message;
    System.out.println(message);
    return message;
    }
}

GroupTestExample.java

public class GroupTestExample {
    String message = ".com";
    MessageUtil messageUtil = new MessageUtil(message);

    @Test(groups = { "functest", "checkintest" })
    public void testPrintMessage() {
        System.out.println("Inside testPrintMessage()");
    message = ".com";
    Assert.assertEquals(message, messageUtil.printMessage());
    }

    @Test(groups = { "checkintest" })
    public void testSalutationMessage() {
    // Removed code since this method wasn't the problem method
    }

    @Test(groups = { "functest" })
    public void testingExitMessage() {
        System.out.println("Inside testExitMessage()");
        message = "www." + "tutorialspoint"+".com";
    Assert.assertEquals(message, messageUtil.exitMessage());
    }
}

的testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
    <test name="test1">
        <groups>
        <run>
        <include name="functest" />
        </run>
    </groups>
    <classes>
        <class name="GroupTestExample" />
    </classes>
    </test>
</suite>

“错误” GroupTestExample.java

    // same code as above except I changed this method from testingExitMessage to
    // testExitMessage b/c that's what the println msg shows and I thought I was 
    // fixing a typo.

    public void testExitMessage() {
        System.out.println("Inside testExitMessage()");
        message = "www." + "tutorialspoint"+".com";
    Assert.assertEquals(message, messageUtil.exitMessage());
    }

2 个答案:

答案 0 :(得分:1)

TestNG按照他们在XML文件中找到的顺序运行测试,在您的情况下,您没有在XML文件中指定方法并且测试以不可预测的顺序运行(我相信它取决于编译器)。

答案 1 :(得分:0)

您可以在测试中设置优先级。

离。 @Test (priority=1)

然后,在testNG.xml文件中,您可以设置preserve-order="true"