Skipping Test Methods


From time to time, you may want to bypass the execution of certain test methods. Suppose you have a handful of failing methods. You might want to concentrate on getting a green bar on one failed method at a time before moving on to the next. The failure of the other test methods serves as a distraction. You want to "turn them off."


In JUnit, you can skip a method by either commenting it out or by renaming the method so that its signature does not represent a test method signature. An easy way to skip a test method is to precede its name with an X. For example, you could rename testCreate to XtestCreate. JUnit looks for methods whose names start with the word test, so it will not find XtestCreate.

You do not want to make a habit of commenting out tests. It is poor practice to leave methods commented out for any duration longer than your current programming session. You should avoid checking in code with commented-out tests. Other developers won't understand your intent. My first inclination when I see commented-out code, particularly test code, is to delete it.

Commenting out test methods is risky. It is easy to forget that you have commented out tests. It can also be difficult to find the tests that are commented out. It would be nice if JUnit could warn you that you've left tests commented out.

A similar problem exists for your new TestRunner class. The simplest way of bypassing a test would be to remove its @TestMethod annotation. The problem with doing that is the same as the problem with commenting out a test. It's easy to "lose" a test in a system with any significant number of tests.

For this exercise, you will make the necessary modifications to TestRunner to ignore designated methods. You will create a new annotation type, @Ignore, and change code in TestRunner to recognize this annotation. The @Ignore annotation will allow developers to supply a parameter with a text description of why the test is being skipped. You will modify TestRunner to print these descriptions.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net