views:

259

answers:

4

I want to run my unit tests automatically when I save my Eclipse project. The project is built automatically whenever I save a file, so I think this should be possible in some way.

How do I do it? Is the only option really to get an ant script and change the project build to use the ant script with targets build and compile?

Update I will try 2 different approaches now:

  1. Running an additional builder for my project that executes the ant target test (I have an ant script anyway)
  2. ct-eclipse, recommended by Thorbjørn
+2  A: 

Right click on your project > Properties > Builders > New, and there add your ant ant builder.

But, in my opinion, it is unwise to run the unit tests on each save.

Bozho
+2  A: 

See if Eclipse has a plugin for Infinitest.

I'd also consider TestNG as an alternative to JUnit. It has a lot of features that might be helpful in partitioning your unit test classes into shorter and longer running groups.

duffymo
It does indeed have a plugin that works really well. Tests run quickly and show up as if they ware copiel errors right in the IDE
Sebastian
+4  A: 

For sure it it unwise to run all tests, because we can have for example 20.000 tests whereas our change could affect only, let's say 50 of them, among which are tests for the class we have changed and tests for classes that collaborate with our class.

There is an unseful plugin called infinitetest http://improvingworks.com/products/infinitest/ which runs only some tests ( related to class we've changed ) just after we save changes. It also integrate quite nicely with editor ( using annotations ) and problem view - displaying not-passing tests like errors.

Luno
Agreed. Inifinitest is the best one I've found, and one of the only ones in active development.
awied
After thinking about it, I'd like to see a plugin execute the last tests I started manually. That would help a lot right now. Inifitiest looks nice, maybe I try it. But payware is not getting priority... ;-)
cringe
You could try http://www.junitmax.com/ for paid alternative.
fastcodejava
+1  A: 

I believe you are looking for http://ct-eclipse.tigris.org/

I've experimented with the concept earlier, and my personal conclusion was that in order for this to be useful you need a lot of tests which take time. Personally I save very frequently so this would happen frequently, and I didn't find it to be an advantage. It might be different for you.

Instead we bit the bullet and set up a "build server" which watches our CVS repository and builds projects as they change. If the compilation fails or the tests fail we are notified quickly so we can remedy it.

It is as always a matter of taste what works for you. This is what I've found.

Thorbjørn Ravn Andersen
Ah, sure. Hudson is doing integration, but I find it somewhat annoying to click around in Eclipse just to start the tests after changing. ;-) Maybe the plugin should be so clever to just run the testcases that test the class I changed. Running all tests on every change would be too annoying.
cringe
consider a save action.
Thorbjørn Ravn Andersen
Just a question: You do run your unit tests locally before checking in, right? Do you have rule of thumb when you run them? After you think your edit could break something? Or only once before commiting?
cringe
We do not have full test coverage (ah, the joy of legacy code). I run the tests if I think it's necessary. I _know_ that the build server will run the tests and notify me if they are broken, so I don't _have_ to.
Thorbjørn Ravn Andersen