Re: [testng-users] How to solve test method dependencies?

2,856 views
Skip to first unread message

Yevhen Bilevych

unread,
May 25, 2013, 8:08:59 AM5/25/13
to testng...@googlegroups.com
Petter, 

Both methods should be annotated with @Test to participate in dependency resolution.

Thanks,
Yevhen 

Sent from my BlackBerry® PlayBook™
www.blackberry.com


From: "Petter Isberg" <icer...@gmail.com>
To: "testng...@googlegroups.com" <testng...@googlegroups.com>
Sent: May 25, 2013 1:16 PM
Subject: [testng-users] How to solve test method dependencies?

Basically I have a test method depending on another method. 

When reading Cédric's book about dependent testing [Next Generation Java Testing, p112] I got the impression that TestNG automatically would add the dependency methods,
"Test isolation is not compromised. Even though some of our test
methods depend on others to run correctly, TestNG calculates those
automatically, thereby allowing us to ask TestNG to “run this single
test method” and have this executed without having to trace all the
dependencies needed."

However I get an TestNGException telling me that some methods needed are not included in my xml file.
"dependingTest() is depending on method public void dependOnTest(), which is not annotated with @Test or not included."

Could it be an error from my side or is this the expected behavior?
Is there any other way to make sure these methods are included?
I looked into adding them manually in a IMethodInterceptor, but I don't know what I need to create a MethodInstance object (like the instance parameter for example).

Regards

Petter


--
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...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Petter Isberg

unread,
May 25, 2013, 8:53:56 AM5/25/13
to testng...@googlegroups.com
Yes, both methods are annotated with @Test. The problem is when the method isn't included in the xml-file.
TestNG tries to sort the methods to make sure the dependent methods are run in the correct order but it never includes the dependency methods.

My goal is that the users of the test classes shouldn't have to care about the dependencies but rather just include the test cases they want to run. The dependencies should be handled either by the framework or by my classes.


My code:

public class MyTest {
@Test
public void dependOnTest() {
assert true;
}
@Test(dependsOnMethods = { "dependOnTest" })
public void dependingTest() {
assert false;
}
}

XML file:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" >
<test name="Test1">
 <classes>
   <class name="MyTest">
     <methods>
       <include name="dependingTest" />
     </methods>
   </class>
 </classes>
</test>
</suite>

Result:

org.testng.TestNGException: 
MyTest.dependingTest() is depending on method public void MyTest.dependOnTest(), which is not annotated with @Test or not included.
at org.testng.internal.MethodHelper.findDependedUponMethods(MethodHelper.java:111)
at org.testng.internal.MethodHelper.topologicalSort(MethodHelper.java:240)
at org.testng.internal.MethodHelper.sortMethods(MethodHelper.java:317)
at org.testng.internal.MethodHelper.collectAndOrderMethods(MethodHelper.java:59)
at org.testng.TestRunner.initMethods(TestRunner.java:481)
at org.testng.TestRunner.init(TestRunner.java:235)
[...]

Petter


Cédric Beust ♔

unread,
May 25, 2013, 11:36:50 AM5/25/13
to testng...@googlegroups.com
Hi Petter,

Your understanding of the logic is correct but you misunderstand what the XML file does.

The <classes> tag is the list of classes and methods that TestNG should look at before it starts resolving things, such as which groups to run, what the dependencies are, etc... So the first thing to do is to include the entire class in your testng.xml:

    <classes>
      <class name="test.tmp.MyTest">
      </class>
    </classes>


Then for example, in Eclipse, you can select to run just "dependingTest()" and TestNG will run both methods.

Another more useful example is running groups. If you put only one method in a group:

  @Test
  public void dependOnTest() {
    assert true;
  }
  
  @Test(dependsOnMethods = { "dependOnTest" }, groups="a")
  public void dependingTest() {
    assert false;
  }

then you can still run the group "a" without knowing anything about its dependencies:

$ testng -groups a src/test/resources/testng-single.xml
[TestNG] Running:
  /Users/cbeust/java/testng/src/test/resources/testng-single.xml


===============================================
Suite1
Total tests run: 2, Failures: 1, Skips: 0
===============================================

Note that this is made possible by the fact that the XML file tells TestNG to look for dependencies in the entire class.

I understand this is not super intuitive and the main reason is that testng.xml is used to do two very different things: 1) "define the world" (which tests and classes to look at) and 2) "run the world" (which tests and classes should run).

It's something I would definitely do differently if I were to start TestNG from scratch today (although I'm not sure exactly how :-)).

-- 
Cédric


-- 
Cédric




Petter


Petter Isberg

unread,
May 26, 2013, 5:57:28 AM5/26/13
to testng...@googlegroups.com, ced...@beust.com
Hi Cédric! 

Thanks for the answer! It does make a little more sense when you explain it :-)

I have one test method that always needs to be run. To make it as intuitively as possible for the users of my test classes, I think I'll just use the IMethodInterceptor to pick out the needed method from the excluded list and insert it into the list of tests to be run. It's an easy enough solution, or can you see any major disadvantages with it?

Regards
Petter

Reply all
Reply to author
Forward
0 new messages