How to specify a test with method name, class name and parameters in testng.xml?

916 views
Skip to first unread message

akshay....@agrostar.in

unread,
Jun 5, 2017, 6:16:38 AM6/5/17
to testng-users

I wish to have test level parameters in my testng.xml and below is the code present in my testng.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="My Test Suite" verbose="1">
   <parameter name="param1" value="value1" />
   <parameter name="param2" value="value2" />
   <listeners>
      <listener class-name="TestListener" />
   </listeners>
   <test name="Test1">
      <parameter name="param" value="value" />
      <classes>
         <class name="ClassContainingTest1" />
         <methods>
            <include name="test1" />
         </methods>
      </classes>
   </test>
   <test name="Test2">
      <parameter name="param" value="value" />
      <classes>
         <class name="ClassContainingTest2" />
         <methods>
            <include name="test2" />
         </methods>
      </classes>
   </test>
</suite>

But the XML editor highlights the classes tag in red. It seems something is wrong with my XML structure, but I'm unable to figure out the same.

Please help.

Vimal Raj

unread,
Jun 5, 2017, 6:22:34 AM6/5/17
to testng...@googlegroups.com
The methods should be the child of class, not the classes.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+unsubscribe@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

akshay....@agrostar.in

unread,
Jun 6, 2017, 12:49:26 AM6/6/17
to testng-users
Hi Vimal,

Thanks for your response. I did not understand it completely. It would be great if you could mentioned the correct XML structure for one of those tests.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.

Krishnan Mahadevan

unread,
Jun 6, 2017, 12:53:08 AM6/6/17
to testng...@googlegroups.com

Change your xml to as below and try again.

The methods tag should be enclosed with the class tag and should not be outside it.

 

<?xml version="1.0" encoding="UTF-8"?>

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

<suite name="My Test Suite" verbose="1">

    <parameter name="param1" value="value1"/>

    <parameter name="param2" value="value2"/>

    <listeners>

        <listener class-name="TestListener"/>

    </listeners>

    <test name="Test1">

        <parameter name="param" value="value"/>

        <classes>

            <class name="ClassContainingTest1">

                <methods>

                    <include name="test1"/>

                </methods>

            </class>

        </classes>

    </test>

    <test name="Test2">

        <parameter name="param" value="value"/>

        <classes>

            <class name="ClassContainingTest2">

                <methods>

                    <include name="test2"/>

                </methods>

            </class>

        </classes>

    </test>

</suite>

 

 

 

Thanks & Regards

Krishnan Mahadevan

 

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

akshay....@agrostar.in

unread,
Jun 6, 2017, 2:05:43 AM6/6/17
to testng-users
Thanks Krishnan! It seems I was making a basic syntactic mistake in the XML file. I'm facing another issue now with the dependency. My test2 is dependent on test1 (i.e. I've mentioned the dependsOnMethods={"test1"} in the @Test of test2).

TestNG throws below exception in this case:
test2() is depending on method public void package.ClassContainingTest1.test1(java.lang.String), which is not annotated with @Test or not included.

I have no idea why I'm facing this. Do you have any pointers?

Krishnan Mahadevan

unread,
Jun 6, 2017, 2:07:26 AM6/6/17
to testng...@googlegroups.com

Can you please help share a working sample and also the error details so that we can understand a bit more context around your problem ?

--

akshay....@agrostar.in

unread,
Jun 6, 2017, 2:18:28 AM6/6/17
to testng-users
Here's the code that's not working. The same code works fine if I remove the dependsOnMethods code from the test class.

Test class:
public class SomeClass {
 
 
@Test (priority=1)
 
@Parameters ({"source"})
 
public void test1(String source) {
 
System.out.println("Inside test1");
 
}
 
 
@Test (priority=2, dependsOnMethods = {"test1"})
 
@Parameters ({"source"})
 
public void test2(String source) {
 
System.out.println("Inside test2");
 
}
 
 
@Test (priority=3, dependsOnMethods = {"test2"})
 
public void test3() {
 
System.out.println("Inside test3");
 
}
}


testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="AVS - CRM API Tests" verbose="1">
   
<parameter name="param" value="globalValue" />
   
<test name="test1">
     
<parameter name="param" value="localValue1" />
     
<classes>
         
<class name="package.SomeClass">

           
<methods>
               
<include name="test1" />
           
</methods>
         
</class>
     
</classes>
   
</test>

   
<test name="test2">
     
<parameter name="param" value="localValue2" />
     
<classes>
         
<class name="package.SomeClass">

           
<methods>
               
<include name="test2" />
           
</methods>
         
</class>
     
</classes>
   
</test>

   
<test name="test3">
     
<parameter name="param" value="localValue3" />
     
<classes>
         
<class name="package.SomeClass">
           
<methods>
               
<include name="test3" />

           
</methods>
         
</class>
     
</classes>
   
</test>
</suite>

akshay....@agrostar.in

unread,
Jun 6, 2017, 2:21:10 AM6/6/17
to testng-users
So if dependsOnMethods code is present in SomeClass, below exception is thrown by TestNG:

test2() is depending on method public void package.SomeClass.test1(java.lang.String), which is not annotated with @Test or not included.
<p class

Krishnan Mahadevan

unread,
Jun 6, 2017, 2:45:59 AM6/6/17
to testng...@googlegroups.com

Yes. I think TestNG is working as designed here.

You would need to include everything when there’s a dependency within a <test> tag (or) you might want to try using the attribute “ignoreMissingDependencies=true” (This attribute is false by default). This will cause TestNG to suppress these sort of edit checks.

--

akshay....@agrostar.in

unread,
Jun 6, 2017, 3:00:47 AM6/6/17
to testng-users
Thanks Krishnan for your inputs! I think I'll go ahead with the first solution of including a set of interdependent tests everything inside a single <test> tag since this approach best suits my requirement, the trade-off being I won't be able to have different parameters for each and every test with this approach. Thanks again!
Reply all
Reply to author
Forward
0 new messages