views:

155

answers:

7

Is there a way I can get ant to run the unit tests just for the java classes it builds? For example, if MyClass.java is out of date, ant will build MyClass.class. After that I want it to also run MyClassTest and MyClassTestSuite if they exist. It doesn't have to be based on a naming convention. I'd be fine with using annotations or any other method that works.

EDIT: Several people have chimed in saying this is a bad idea. It would be, if I planned on checking in without running all of our unit tests. My main project has over 16k unit tests which take about 20 minutes to run. I'll run them all before check-in, but it's completely impractical to run the whole suite every time I change a file. Sorry, I should have given more context.

+4  A: 

It's not that easy...

Take a look at Atlassian Clover. It has the feature you dream about, but it's not free.

Grzegorz Oledzki
+6  A: 

I would not recommend this approach, There is a good chance of missing errors introduced by side effects.

Ramon
+1  A: 

Take a look at Infinitest, it's a plugin for Eclipse (and Intellij) that runs tests based on the source files you have just changed:

Infinitest Website

For an overview see the screencast here:

Screencast Overview

Jon
Infinitest is awesome. I wouldn't code without it.It's the one reason I'm not switching to NetBeans
Sebastian
+4  A: 

That seems like a bad idea to me. Sure the changed source files need testing, but also any files that depend on those files, and any that depend on those, etc. You would have to track which file depend on what other files, and thats a rather big problem to solve.

In my opinion, if the project is big enough that there is a significant gain in trying to resolve dependency trees of testing, maybe the project is too big? Maybe it is time to split it up into separate projects that work together, which could each be tested apart from each other.

Mike Cooper
A: 

I'd like to chime in with the other folks that have said that this is something you want to avoid. Running only the test cases for the specific classes which have changed introduces a risk that you will make a change which breaks a test for a class which wasn't changed but has a dependency on some of the code that has been changed.

This is a risk even if you're aggressive about programming to an interface and mocking dependencies in unit tests. For example, static utility classes are likely to have a lot of classes using them directly. Here, it would be easy to imagine a case where you could make a change to a static utility method, ensure that the tests for that method worked, and break a ton of code that called the method if you didn't run all of your tests.

Paul Morie
+2  A: 

There is no way I know of to do this with ANT, but you can do something like this in Eclipse.

Kent Beck has written an Eclipse plugin called JUnit Max that runs your unit tests after you save your code. It runs the tests "most likely to fail" first.

JUnit Max

Jon Strayer
MaxCore prefers new tests to old tests, fasttests to slow tests, and recently failing tests to tests that lastfailed long ago.
Jon
A: 

Well, you could probably do it using variables, if you follow a naming convention. Get a list of changed files, and run the corresponding tests. But I would recommend against it, like the other answers explain.

sleske