如果返回类型在 TestNG 类中不为 void,则不执行 Test



仅在执行测试方法之前将类文件作为TestNG执行。在结果中,跳过、失败或通过的测试用例计数 = 0。在脚本的整个执行过程中没有错误或异常。但是当我将返回更改为 void 类时,已成功执行。谁能提出原因?

您可以在 testng 套件文件中使用 allow -return-values for testng 将这些值视为测试。 通常对于测试,返回值没有意义,它们应该是独立的单元 - 即使你添加了返回类型,如果允许返回值为 true,Testng 也会简单地忽略它们。

下面是一个示例,演示了此操作。

import org.testng.Reporter;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class TestClassSample {
    @BeforeMethod
    public void beforeMethod() {
        Reporter.log("beforeMethod() executed", true);
    }
    @Test
    public String testMethod() {
        Reporter.log("testMethod() executed", true);
        return null;
    }
}

这是相应的套件 xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="46765400_Suite" verbose="2" allow-return-values="true">
    <test name="46765400_test">
        <classes>
            <class name="com.rationaleemotions.stackoverflow.qn46765400.TestClassSample"/>
        </classes>
    </test>
</suite>

这是执行输出

...
... TestNG 6.12 by Cédric Beust (cedric@beust.com)
...
beforeMethod() executed
testMethod() executed
PASSED: testMethod
===============================================
    46765400_test
    Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
46765400_Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

最新更新