Monday, 13 October 2008

Book Review: Next Generation Java Testing by Cedric Beust and HaniSuleiman

Subtitled "TestNG and Advanced Concepts" and written by the people behind TestNG, I picked up this book expecting to read a definitive and encyclopedic work on TestNG. However, the authors decry this view in the preface. This book takes 'testing' as its focus and uses TestNG to illustrate the examples. (Although it does really start off as  "a book about TestNG").

So prior to reading the book my experience of TestNG amounted to the following:


  • read some of the tests people had written using TestNG at work
  • amended some of the tests
  • hacked about with the testng.xml file
  • fixed some tests
  • gone to the website to learn a little more about some of the annotations and the xml file
  • Read, and used, the examples on the home page
  • Run tests and suites within Eclipse
  • Skimmed the documentation

So, as a beginner I felt like I could already 'use' TestNG, but I didn't really understand some of the concepts 'properly' like dataproviders - sure I could write one, but I didn't really 'get' all the nuances. Hence the reason for reading this book.

( amazon.co.uk | amazon.com )



And I had learned the hard way that TestNG does not view the method that you annotate with @Test as a 'test' . TestNG views a test as that thing in the testng.xml file with <test></test> around it.

So we start by learning a little about the deficiencies in JUnit that led to the creation of TestNG - to support different styles of testing and more 'functional' testing. Then a few examples of the annotations and the testng.xml file.

Then into unit testing with TestNG and testing exceptions.

I learned that TestNG creates a testng-failed.xml file containing a suite of just the failing tests. Which I hadn't noticed when skimming the docs online. But I can quite clearly see it in section 5.10 online now.

Then we jump straight into Factories. Whoa! Sub-optimal organisation for the beginner. I think the early parts of the book help argue the case for 'another unit test framework' and make you ask the question 'can your unit test framework do this?'

I say this because we have a test suite running quite well at work using just <parameters> some @Before and @After annotations and a few dataproviders. I read the Factory example in the text a few times and I confess I still didn't get it. Yep - you can call me stoopid, but I don't class this book as a beginner book.

So, still a bit bemused, factory only takes 4 pages, then we move on to the official section on Data-Driven Testing and this does seem written more with the beginnner in mind - why try and scare me off with a Factory example?

The data providers in TestNG really have a lot of power.

In some ways TestNG has too much power because you move from simple example to complex example within the same breath and having just figured out why you might use a data provider you have to wrap your head around how you might use the test context functionality.

OK, so not again, definitely not structured with the beginner in mind. And as a beginner you end up skipping chunks to get what you need. And when you finally try it in your own code it starts to make sense. But the authors aimed this book at the java programmer - typically a more technically proficient beast than the tester.

I won't try and summarise the book contents in this review as you can read the contents online. Really the book tries to cover lots of different 'typical' Java testing situations and show ways of using TestNG to test your code. Some of them really interesting - I particularly liked the conciseness of some of the Enterprise testing examples.

For testers - htmlunit and Selenium get cursory mentions and probably won't help you very much. Check out the Selenium-grid examples which use TestNG for more examples.

A really important book. But not quite what I hoped for. I wanted a more tutorial approach to learning TestNG and its capabilities. I think I could find most of the information I want in here, but I'll have to read and re-read this book a few times, spread over a few months to really understand the flexibility of TestNG. And I'll have to look at all the example code and figure out if/how I could use the technique to aid my System testing. So I'll have to stick with the basics at the moment and hope that the more I use it, the more I'll understand it - no easy routes into this tool it seems.

This book will probably work really well for the Java programmer and it has lots of really focused examples for common Java testing approaches.

For the tester wanting to use it for your functional testing then you'll get the basics here and understand grouping but you'll still have to struggle to learn the optimum ways of using the tool. The enormous flexibility of TestNG still feels just out of reach for me at the moment but the future seems too tempting to stop trying.

Related Reading: