簡體   English   中英

如果 TestNG 類中的返回類型不為空,則不會執行測試

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

僅在將類文件作為 TestNG 執行時 執行測試方法之前。 在結果中跳過、失敗或通過測試用例計數 = 0。 在整個腳本執行過程中沒有錯誤或異常。 但是當我更改返回到 void 類時成功執行。 任何人都可以請提出這個原因嗎?

您可以在 testng 套件文件中使用 allow-return-values 將 testng 視為測試。 通常對於測試,返回值沒有意義,它們應該是獨立的單位——即使你添加了返回類型,如果允許返回值為真,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
===============================================

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM