从testng中的testng.xml传递参数时出错

时间:2016-10-26 07:09:00

标签: parameter-passing testng parameterized

我无法访问testng.xml中定义的参数化值。在测试和方法级别定义参数也是我从其他类似查询得到的但是错误保持不变 错误是 - "参数' myName' @Test在方法parameterTest上要求但未标记为@Optional或已定义 在C:\ Windows \ Temp \ testng-eclipse-281832880 \ testng-customsuite.xml"

下面是我的代码片段,后跟testng.xml和错误

Code snippet for one and two params

testng.xml with both param types 1-at test and method level both, 2- at method level

1 个答案:

答案 0 :(得分:1)

以下内容适用于我[您只需要确保同一个类在同一<test>代码中不会被多次包含。

这是一个样本

package organized.chaos.testng;

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

public class EmployeeTestNG {
    @Parameters ({"and", "abc"})
    @Test
    public void test1(String b, String a) {
        System.err.println("****" + b + a);
    }

    @Test
    @Parameters ("myName")
    public void parameterTest(String myName) {
        System.err.println("Parameterized value is " + myName);
    }
}

这是TestNG套件xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="employee-suite">
    <test name="Employee-test">
        <classes>
            <class name="organized.chaos.testng.EmployeeTestNG">
                <parameter name="myName" value="TestNG"/>
                <parameter name="and" value="KungFu"/>
                <parameter name="abc" value="Panda"/>
                <methods>
                    <include name="parameterTest"/>
                    <include name="test1"/>
                </methods>
            </class>
        </classes>
    </test>
</suite>

这是输出

[TestNG] Running:
  /Users/krmahadevan/githome/PlayGround/testbed/src/test/resources/employee.xml
Parameterized value is TestNG
****KungFuPanda

===============================================
employee-suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0