Structuring JUnit tests for large package structures?

S

Stephen Riehm

Hi,

I'm an old-hat perl junkie that has grown very accustomed to setting up
a simple testing structure and then simply adding test cases whenever I
add functionality to my code. (ie: write test for new feature, write
feature, test, go back to step 1,...)

Now I'm branching into the world of Java and haven't quite 'got-it' when
using JUnit.

In the FAQ at http://junit.sourceforge.net/doc/faq/faq.htm#organize_2
Mike Clark presents a way of creating a TestSuite which contains a lot
of tests. So far so good. He does this by using a static method which is
called directly from the main method. Is it possible to wrap up a
TestSuite in a "higher" TestSuite?

In my case, I've split my application into several packages, just 5 so
far, but they'll be more. ie: myapp.data, myapp.gui, myapp.cli,
myapp.interfaces. I would *like* to group the test cases for each
package into a TestSuite (ie: myapp.data.AllDataTests) and then have
them all run from a central point. (ie: myapp.AllTests). The idea being
that once the structure is in place, all we would need to do is write
tests and classes, and not have to fiddle with the administration of
test cases.

Is there a neat way of recursively grouping suites of TestSuites (I
tried subclassing TestSuite, but with little success) or is JUnit
limited to a single TestSuite of TestCases? Have I missed something obvious?

Thanks for your enlightenment!

Steve
 
O

Oliver Nautsch

Stephen Riehm said:
Hi,

I'm an old-hat perl junkie that has grown very accustomed to setting up
a simple testing structure and then simply adding test cases whenever I
add functionality to my code. (ie: write test for new feature, write
feature, test, go back to step 1,...)

Now I'm branching into the world of Java and haven't quite 'got-it' when
using JUnit.

In the FAQ at http://junit.sourceforge.net/doc/faq/faq.htm#organize_2
Mike Clark presents a way of creating a TestSuite which contains a lot
of tests. So far so good. He does this by using a static method which is
called directly from the main method. Is it possible to wrap up a
TestSuite in a "higher" TestSuite?

In my case, I've split my application into several packages, just 5 so
far, but they'll be more. ie: myapp.data, myapp.gui, myapp.cli,
myapp.interfaces. I would *like* to group the test cases for each
package into a TestSuite (ie: myapp.data.AllDataTests) and then have
them all run from a central point. (ie: myapp.AllTests). The idea being
that once the structure is in place, all we would need to do is write
tests and classes, and not have to fiddle with the administration of
test cases.

Is there a neat way of recursively grouping suites of TestSuites (I
tried subclassing TestSuite, but with little success) or is JUnit
limited to a single TestSuite of TestCases? Have I missed something obvious?

Thanks for your enlightenment!

Steve

Hello Steve

yes it is possible.

Let's say you have the following suites. AllTests in root is the
top-level testsuite which should include suites in the subdirectories.

Then:

org.huhu.root.AllTests.java
org.huhu.root.subdir1.AllTests.java
org.huhu.root.subdir2.AllTests.java

In org.huhu.root.AllTests.java

....
public static Test suite() {
TestSuite suite = new TestSuite("Test for org.huhu.root");
suite.addTest(org.huhu.root.subdir1.AllTests.suite());
suite.addTest(org.huhu.root.subdir2.AllTests.suite());
return suite;
}
....

Regards
Oliver Nautsch
 
S

Stephen Riehm

yes it is possible.

Let's say you have the following suites. AllTests in root is the
top-level testsuite which should include suites in the subdirectories.

Then:

org.huhu.root.AllTests.java
org.huhu.root.subdir1.AllTests.java
org.huhu.root.subdir2.AllTests.java

In org.huhu.root.AllTests.java

...
public static Test suite() {
TestSuite suite = new TestSuite("Test for org.huhu.root");
suite.addTest(org.huhu.root.subdir1.AllTests.suite());
suite.addTest(org.huhu.root.subdir2.AllTests.suite());
return suite;
}
...

OK, so then I would only need to add the new classes to the AllTests in
their directory.
Thanks!

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,082
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top