Search code examples
parameter-passingtestngparameterized

Error in passing paramters from testng.xml in testng


I am unable to access paramterised value defined in testng.xml.Tried defining parameter at test and method level too which i got from other similar queries here but the erros remains same Error is- "Parameter 'myName' is required by @Test on method parameterTest but has not been marked @Optional or defined in C:\Windows\Temp\testng-eclipse-281832880\testng-customsuite.xml"

Below is my snippet of code,followed by testng.xml and error

Code snippet for one and two params

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


Solution

  • The following works for me [ You just need to ensure that the same class is not being included more than once in the same <test> tag.

    Here's a sample

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

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

    Here's the output

    [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