java - 如何在TestNG测试方法中使用 "allow-return-values="true""?

标签 java selenium selenium-webdriver testng

我是 TestNG 的初学者。我在官方TestNG文档中了解了测试方法。我不清楚 allow-return-values="true" 的使用,任何人都可以用示例简要解释一下。我做了一个例子,但我不明白如何使用,

这是我的 .xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="newSuite" parallel="methods" thread-count="5" allow-return-values="true">
<parameter name="browser" value="Firefox"></parameter>
<test name="dataprovider" allow-return-values="true">
   <classes>
      <class name="com.tets.SampleTest"></class>
   </classes>
</test>
</suite>

我的测试类如下,

public class SampleTest {

@AfterMethod
public void teardown(Object returnValueFromTest){
    //inspect returnValueFromTest and perform necessary clean up.
}


@Test
public String testEventGeneration(){
    //generate event

    //returning generated e vent id.
    return "E1234";

}

@Test
public String testMarketGeneration(){
    //generate market

    //returning generated market.
    return "hai";
}}

如何以及在哪里可以从 testMarketGeneration 测试方法获取返回值?

最佳答案

选项“allow-return-values”并不意味着测试返回值“hai”。它的目的是允许在具有返回值的方法上使用 @Test 注释。

默认情况下,TestNG 遵循单元测试的最佳实践,这意味着单元测试方法不应该有返回值。但是有时您想将 @Test 注释添加到具有返回值的方法中。如果您没有在 Suite 配置中指定“allow-return-values”,TestNG 将打印出一条警告,并且不会将该方法作为测试运行。

TestNG 给出的警告类似于:

Method public java.util.List a.B.getC(java.util.List) has a @Test annotation but also a return value: ignoring it. Use 'suite allow-return-values="true"' to fix this

到目前为止,我发现的允许返回值的唯一有用的示例是当您的测试类实现某个接口(interface)并且您希望通过添加 @Test 注释来测试所有接口(interface)方法时。

参见http://testng.org/doc/documentation-main.html以获得真正的文档。

关于java - 如何在TestNG测试方法中使用 "allow-return-values="true""?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23149807/

相关文章:

java - 缩小后无法在 selenium webdriver 中找到 webelement

python - 使用 Selenium 获取用户按下的按钮

java - Selenium 获取文本

java - 读取.class文件

python - 获取或操作 Selenium Webdriver 中的所有 cookie

java - 何时在 Selenium Webdriver 中使用显式等待与隐式等待?

java - phantomjs 不能与 selenium java 一起使用

java - 因此,我正在使用此应用程序,它没有显示任何错误并已成功安装,但运行后便崩溃了

java - 应用服务器启动时间比较?

java - 在方法及其引用中搜索对象