It's not a secret anymore!*
The moose likes Testing and the fly likes Running batches of unit tests in a hierarchy with non-unit test tests Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Running batches of unit tests in a hierarchy with non-unit test tests" Watch "Running batches of unit tests in a hierarchy with non-unit test tests" New topic
Author

Running batches of unit tests in a hierarchy with non-unit test tests

Eric Asberry
Greenhorn

Joined: Mar 06, 2008
Posts: 3
After a bit of time away from Java, I recently joined a new team and am trying to promote the use of JUnit and unit testing. I created a separate test directory for containing the unit test source files, and got to the point where all the tests I had created were running great from ant using the junit task and batchtest to match all the source files that matched our naming convention for tests. So far, so good.

Now, the problem is that there already exist many manual test classes which are not really JUnit tests - they are Java classes with main's that are intended to be manually run. The problem is the lead dev wants to keep all these tests in the same directory as the JUnit tests, and unfortunately, they have the same naming convention as the unit tests, so using batchtest won't work any more, since it pulls in all these non JUnit tests and fails when it tries to run them.

I didn't think this was going to be a problem -- in my previous Java dev role (probably using JUnit 3.x) I had used an "AllTests" class that built a TestSuite by searching for appropriate classes in the class path. My thinking that was instead of using batchtest, I could do the same kind of thing to ignore all the classes that are just main's. So, I wrote an AllTests class, and so far, I have gotten VERY close to getting it to work. What really confuses me, is that it works when I "Run as JUnit" in Eclipse, but when I run it from ant, it is claiming there are "No tests found in a.b.c.FooTest". I'm using JUnit 4.4 both with Ant and Eclipse, so I'm mystified at the discrepancy. Any suggestions?

[Edit: I should also mention, the unit tests themselves are all using the JUnit 4 style, i.e. they don't extend TestCase, use the @Test annotation, etc.]

Here's what my TestSuite generator looks like:



[ July 30, 2008: Message edited by: Eric Asberry ]
[ July 30, 2008: Message edited by: Eric Asberry ]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30085
    
149

Eric,
Welcome to JavaRanch!


If the tests are JUnit 4 tests for a class, wouldn't the JUnit4TestAdapter throw an error that no tests are found?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Eric Asberry
Greenhorn

Joined: Mar 06, 2008
Posts: 3
Originally posted by Jeanne Boyarsky:
Eric,
Welcome to JavaRanch!


If the tests are JUnit 4 tests for a class, wouldn't the JUnit4TestAdapter throw an error that no tests are found?


Though it's not clear from the formatting in my post, that first line is actually commented out - it's what I had tried first. I had found the JUnit4TestAdapter wrapper doing a google search. And no, it doesn't throw an error, and actually does what I want it to do when I run it from Eclipse.

I ended up taking a different approach for now. I discovered the ability to specify an "includesfile" in a fileset in Ant, which I didn't know about before. This lets you specify a file to read the patterns from. So, I modified the class above to have a main, and I run it as a java program in the ant task, and it writes out the appropriate patterns to a file. Then I use the batchtest method that I was using originally, but pointing includesfile to the file I just wrote out instead of specifying a pattern in the actual Ant script.

I'm not really happy with it, and its probably going to get rather unwieldy as the number of tests grow, but at least in the short-term, its doing what I need it to.

I still don't understand why the original thing works in Eclipse and not in Ant. I would expect it to either work or not work consistently, so obviously I'm missing something.

BTW, it only works in Eclipse using the JUnit4TestAdapter wrapper ... if I don't use that, it doesn't work in Eclipse either.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Hmm. I'd give another shot at talking to the dev lead about the reasons for having just one directory for tests and for having more than one directory for tests.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Running batches of unit tests in a hierarchy with non-unit test tests
 
Similar Threads
Stuck with KMeans Clustering Algorithm
setUp and teardown not working
Add testSuites to other test suites?
Test Suites