Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼


Project of the Month: TestNG


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:

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.

More by Cédric Beust

Breaking Away From The Unit Test Group Think

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.