简体   繁体   中英

Test is not executed if return type is not void in a TestNG Class

On executing class file as TestNG only Before test method is executed. In the results Skipped, failed or passed test cases count=0. There is no error or exception throughout execution of the script. But when I change return to void class is executed successfully. Could anyone please suggest the reason for this?

You can use allow-return-values in the testng suite file for testng to consider those as tests. Normally for tests, return values have no significance, they are supposed to be independent units - even if you add the return types, Testng will simply ignore them if allow-return-values to true.

Here's a sample that shows this in action.

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;
    }
}

Here's the corresponding suite 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>

Here's the execution output

...
... 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
===============================================

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM