Dependencies
Assume that you are testing a website, and your test looks like this:
- Test that the website is up.
- Test that the servlet is deployed.
- Test 10 operations on this servlet.
Something goes wrong in the latest run, and the deployment of your servlet fails. You take a look at the morning test reports, and you see this: "1 Passed, 11 Failed." Uh oh!
Did someone actually break eleven tests? This seems unlikely. Because you know these tests, you suspect that you are looking at a cascade effect: One test failed and it took all the others down. You don't really need to fix eleven tests, you just know that if you find the one that actually failed, the other ten will automatically run successfully.
This is pretty obvious with a small example, but imagine that you are dealing with thousands of tests, most of which you didn't write yourself, and you are now looking at a report saying that 341 tests failed. Where do you start?
The way out of this problem is test dependencies.
You need to tell the test framework in what ways your tests depend on each other so it will be smarter at running them. After all, what's the point of running ten servlet tests when the servlet hasn't been deployed at all, and the testing framework already knows this?
Here is one way of doing this with TestNG:
@Test
public void webSiteShouldBeUp() { ... }
@Test(dependsOnMethods = "webSiteShouldBeUp")
public void servletShouldBeDeployed() { ... }
@Test(dependsOnMethods = "servletShouldBeDeployed")
public void testServlet1() { ... }
@Test(dependsOnMethods = "servletShouldBeDeployed")
public void testServlet2() { ... }
Now you run these tests with the same (failing) configuration, and your report reads: "1 Passed, 1 Failed, 10 Skipped."
This is a much more accurate report, and it points directly to where you should focus your efforts (the one test that failed). You can completely ignore the skipped tests as long as you have failed tests. This is made possible by the fact that the testing framework is now more knowledgeable about the structure of the tests, and thus, it can give you a much more accurate report of what actually happened.
As it turns out, the example I just showed is a bit suboptimal. For example, imagine that on top of servletShouldBeDeployed(), you need to do another sanity check (for example, databaseIsUp()) before your servlet tests can run. Doing so will force you to modify the ten test methods to add this dependency, which is quite annoying.
Fortunately, TestNG offers an elegant solution to this problem using the groups mentioned earlier. Dependencies can be used with groups as well. I can rewrite the example to introduce groups and have the test methods depend on groups instead of methods:
@Test(groups = "web-site-init")
public void webSiteShouldBeUp() { ... }
@Test(dependsOnGroups = "web-site-init", groups = "servlet-init")
public void servletShouldBeDeployed() { ... }
@Test(dependsOnGroups = "web-site-init", groups = "servlet-init")
public void databaseShouldBeUp() { ... }
@Test(dependsOnGroups = "servlet-init")
public void testServlet1() { ... }
@Test(dependsOnGroups = "servlet-init")
public void testServlet2() { ... }
You now have two groups, "web-site-init," which will run first, and then "servlet-init." Adding methods to any of these groups is trivial, and it guarantees that any method from the first group will run before the methods in the second group.
Wrapping Up
I've only scratched the surface of the features that TestNG offers in support of functional testing, but this brief overview should give you with a good idea of the kind of testing that TestNG enables. TestNG is open source using the Apache 2.0 license. The TestNG documentation covers the features explained here in greater detail and many more.
Cédric Beust is He is a former Google engineer now working on Android. He is also the founder and lead developer of TestNG project.


