Sometime it is very useful to create tests dynamically. For instance, I had to test that every URL returned by a method is valid and the content can be downloaded. In this case I don't know how many URL the method was returning, and I wanted to have a separate and independent test for every URL so that I can catch all the errors in a single run. If you create a single test with a loop, the first failure will stops the other tests to run. This approach worked fine: I caught 5 type of errors in over 2000 tests running on that method (one per url), some url was returning http status 404, some 500, 403, etc... everything documented in a test report, and promptly reported in the bug tracker.
This example, on the usual Adder, is about how to dynamically create tests:
1public class TestAdd extends TestCase { 2 private Adder adder; 3 private long expected; 4 5 public TestAdd(int a, int b, long expected) { 6 super("test" + a + "addedTo" + b + "equals" + expected); 7 this.adder = new Adder(a, b); 8 this.expected = expected; 9 } 10 11 protected void runTest() throws Throwable { 12 assertEquals(expected, adder.sum()); 13 } 14 15 public static Test suite() { 16 TestSuite suite = new TestSuite(TestAdd.class.getName()); 17 Random r = new Random(); 18 for(int i = 0; i < 10; i++) { 19 int a = r.nextInt(100); 20 int b = r.nextInt(100); 21 long expected = a + b; 22 suite.addTest(new TestAdd(a, b, expected)); 23 } 24 return suite; 25 } 26}
I don't know if this can be so easily done with JUnit4 and annotations. I still prefer JUnit 3, I think it's much easier.
4 Responses to “Dynamic tests with JUnit 3”
Leave a Reply
Search
Calendar
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| « Oct | Dec » | |||||
| 1 | 2 | |||||
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 24 | 25 | 26 | 27 | 28 | 29 | 30 |
Archives
Categories
- Android (3)
- Apple (27)
- Books (7)
- Eclipse (14)
- Errors (4)
- Firefox (7)
- Git (2)
- Hardware (17)
- Horror Code (8)
- Internet (21)
- Java (102)
- JavaScript (9)
- Life, universe and everything (46)
- Lifehacks (25)
- Linux (51)
- Opinions (26)
- OSX (6)
- Python (1)
- Software (31)
- Speeches and Conferences (8)
- Unix (4)
- Web (23)
- Windows (19)
Tag Cloud
Android apple architecture Bash configuration CSS Development Düsseldorf Eclipse Git Google Hardware hdr How-To Java JAXB job junit Karmic Linux lion MacBook music Open Source Opinion oracle OSX patterns Pitfalls Practices Resume Security Software Suspend TDD Testing tip tonemapped Tricks Ubuntu unix video Web Workaround XML
WP Cumulus Flash tag cloud by Roy Tanck and Luke Morton requires Flash Player 9 or better.
Blog License
Blogs I like
Books on the desk
Friends' Blogs
- Antonio Terreno & Valter Bernardini
- Bruno Bossola
- Daniele Galluccio
- Domenico Ventura
- Ed Schepis
- Fabrizio Gianneschi
- Luca Grulla
- Luigi Zanderighi
- Marcello Teodori
- Mida Boghetich
- Muralidharan Chandrasekaran
- Piero Ricca
- Renzo Borgatti
- Simone Bordet
- Simone Bruno
- Uberto Barbini
- Valvolog
- Webtide blogs (Greg Wilkins & Jan Bartel)
Links




















This test did not run. Do you have a working example?
It does run. What problem are you finding?
Works great. Very useful, thanks!
JUnit4: http://stackoverflow.com/questions/358802/junit-test-with-dynamic-number-of-tests