当前位置: 首页 > 知识库问答 >
问题:

如何为testNG suite xml中包含的方法获取(相同)参数?

薛高澹
2023-03-14

以下几年前的帖子概述了在testng套件xml中指定方法级参数的可能性:

例如,需要将ID传递给方法,该方法将是唯一的(每个配置):

<methods>
        <include name="testX">
          <parameter name="ID" value ="3452"/>
        </include>
        <include name="testY">
           <parameter name="ID" value ="3453"/>
        </include>
</methods>

这还有可能吗?我没有看到它提到在测试留档。(我需要在方法级别包括一个特定的参数,而不是测试级别)

如果这是可能的,我如何获得方法参数的值,因为它似乎没有在测试用例类文件中使用@Parameters获得。

引用的帖子确实指使用:

iTestResult.getMethod().findMethodParameters(iTestContext.getCurrentXmlTest())

但是,如果这仍然适用,一些关于将其放在正确的侦听器方法中的指导将是不胜感激的。

谢谢

共有1个答案

邵宜年
2023-03-14

这是非常可能的,并且仍然在TestNG中得到支持。这里有一个完整的例子,展示了所有这些在行动。

我正在为此使用TestNG 6.13.1(这是截至今天最新发布的TestNG版本)

样本测试类

package com.rationaleemotions.stackoverflow.qn48171506;

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class SampleTestClass {
    @Test
    @Parameters(value = "name")
    public void helloA(String name) {
        System.err.println("helloA() says " + name);
    }
    @Test
    @Parameters(value = "anotherName")
    public void helloB(String name) {
        System.err.println("helloB() says " + name);
    }
    @Test
    @Parameters(value = "someOtherName")
    public void helloC(String name) {
        System.err.println("helloC() says " + name);
    }
}

样本测试监听器

java prettyprint-override">package com.rationaleemotions.stackoverflow.qn48171506;

import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestResult;

public class TestListener implements IInvokedMethodListener {
    @Override
    public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
        showMessage("About to run ", method, testResult);
    }

    @Override
    public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
        showMessage("Completed running ", method, testResult);
    }

    private static void showMessage(String prefix, IInvokedMethod method, ITestResult testResult) {
        String msg = prefix + method.getTestMethod().getMethodName() + "() with the parameters " +
                method.getTestMethod().findMethodParameters(testResult.getTestContext().getCurrentXmlTest());
        System.err.println(msg);
    }
}

套件xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Sample_Suite" verbose="2">
    <listeners>
        <listener class-name="com.rationaleemotions.stackoverflow.qn48171506.TestListener"/>
    </listeners>
    <test name="sample_test" verbose="2">
        <classes>
            <class name="com.rationaleemotions.stackoverflow.qn48171506.SampleTestClass">
                <methods>
                    <include name="helloA">
                        <parameter name="name" value="Jack"/>
                    </include>
                    <include name="helloB">
                        <parameter name="anotherName" value="Daniel"/>
                    </include>
                    <include name="helloC">
                        <parameter name="someOtherName" value="Craig"/>
                    </include>
                </methods>
            </class>
        </classes>
    </test>
</suite>

执行输出

...
... TestNG 6.13.1 by Cédric Beust (cedric@beust.com)
...
About to run helloA() with the parameters {name=Jack}
helloA() says Jack

Completed running helloA() with the parameters {name=Jack}
About to run helloB() with the parameters {anotherName=Daniel}
helloB() says Daniel
Completed running helloB() with the parameters {anotherName=Daniel}

About to run helloC() with the parameters {someOtherName=Craig}
helloC() says Craig
Completed running helloC() with the parameters {someOtherName=Craig}
PASSED: helloA("Jack")
PASSED: helloB("Daniel")
PASSED: helloC("Craig")

===============================================
    sample_test
    Tests run: 3, Failures: 0, Skips: 0
===============================================

===============================================
Test Dependencies
Total tests run: 3, Failures: 0, Skips: 0
===============================================
 类似资料:
  • 我正在使用Spring、REST和Hibernate创建一个web应用程序。在这里,我从数据库中提取记录使用唯一的用户名,这是来自URL。但问题是,如果我写的是简单字符串,那么它可以正常工作,但在username中,我写的是dot(.)则数据库中没有任何结果。 这是我的控制器 这是我的DAO类

  • 问题内容: 我的应用程序中有一个奇怪的异常,我想记录它发生的时间,并包括完整的请求字符串(包括参数)。 当我尝试 我得到了请求字符串,但没有得到包含的参数?和&。 例: 我只看到 我可以把整个字符串放在某个地方吗? 问题答案: 参见HttpServletRequest#getQueryString() 如果需要整个字符串,则必须将请求url和查询字符串附加在一起,因为没有方法可以获取整个字符串。

  • 本文向大家介绍java中如何获取相关参数,包括了java中如何获取相关参数的使用技巧和注意事项,需要的朋友参考一下 此文通过一段代码来展示java获取相关参数的方法分享给大家: 希望大家能够喜欢。

  • 我需要从url得到字符串在哪里是“?”但是控制器不接受“?” 我需要发送一些类似“你好世界?”但我只得到“你好世界” 我找到了点(.)的解决方案--value=“{texttoTransform:.+}”

  • 问题内容: 嗨,我想知道我的问题是否有简单的解决方案, 我有一个: 它们都实现一个方法- 调用时飞。我知道我可以使用此方法访问超类的通用方法和属性 很好-但是现在我想访问子类具有的方法。我可以通过将as 强制转换为: 在我的情况下,我不想这样做,因为这意味着我对的每个子类型都有3套上述代码。 似乎有点多余,是否有更好的方法? 问题答案: 实际上,从超类执行此操作不是一个好方法,因为每个子类的行为都

  • 问题内容: 考虑以下示例: 失败并出现以下错误 为什么呢 生成的方法没有重叠。事实上,这实际上意味着 那里没有重叠。那为什么会失败呢? 如果您想知道我在做什么,并且有更好的解决方案:我有一堆Event和Listener接口,它们几乎与上述类完全一样。我想创建一个适配器和一个适配器接口,为此,我需要使用特定事件扩展所有Listener接口。这可能吗?有一个更好的方法吗? 问题答案: 不,你不能。这是