As an Agile Coach, Miško is responsible for teaching his co-workers to maintain the highest level of automated testing culture, allowing frequent releases of applications with high quality. He is very involved in Open Source community and an author of several open source projects. Recently his interest in Test Driven Developement turned into http://TestabilityExplorer.org with which he hopes will change the testing culture of the open source community. Misko is a DZone MVB and is not an employee of DZone and has posted 38 posts at DZone. You can read more from them at their website. View Full User Profile

Configure your IDE to run your tests automatically

05.08.2009
| 11165 views |
  • submit to reddit

 

When I develop code I write test first and always run my tests. But it can get rather tedious to run the tests manually every so often. Here is a common scenario. Your tests are green and you start doing whole bunch of refactorings which you think are trivial and safe. When you are done you run the tests and it is broken. The problem is that you did ten little things and you don’t know which of the ten things you did broke the code. The solution is to run the test more often, but we just forget.

What I want is to have my IDE run my tests every time I do a change without me doing anything. This way even though I am doing ten little changes the IDE runs the tests ten times. The moment you do something which you thing is safe but it turns out that it makes your tests fail your IDE should tell you. You than simply hit Cntl-Z (undo) and you are back in business.

What I want is for my IDE to run my tests every time I save the code. To do this my tests need to be fast, because my patience after hitting Cntl-S is about two seconds. Anything longer than that and I will get annoyed. If you start running your tests after every save from test zero you will automatically make sure that your test will never become slow, since as soon as your tests start to run slow you will be forced to refactor your tests to make them faster.

Obviously this technique is only useful for true unit-tests since they are fast. I do not run my scenario tests as part of Cntl-S.

I only know how to set this up on Eclipse, so perhaps people with IDE or NetBeans can chime in. I have already set up the project for you here so all you need to do is download and try it out. But here are the steps

  1. Create a Project: Eclipse has this cool feature which compiles your code continuously on every save. This is great, as what it really means is that your code is always compiled and you never have to wait for  a compilation step in eclipse.
  2. Create a JUnit Text runner as shown here: here
  3. Create an ANT build file which will run your text runner from the eclipse compiled folder. This way the ant does not have to continuously compile your code.
  4. Tell Eclipse to run your ant target after every compilation cycle. This is known as Builder in Eclipse.
    1. Open project properties: Right click project -> Properties
    2. Create new ant builder step after the Java Builder in eclipse: Builders -> new -> Ant Builder
    3. Give your builder a nice name. Here is what to do on each tab. (I thing eclipse has a bug and you need to hit Apply every time you change tabs)
      • Main: Select the build file; base directory (This specifies which ant filet to use)
      • Targets: Select your ANT target for: “After Clean”, “Manual Build”, and “Auto Build” (most important) (This specifies that after ever build it should also run your ANT target.)
      • Build Options: Check “Specify working set” and select all of your source folders. (This specifies which file changes will trigger this builder, in our case any file change)

Or just grab my repository form here and import the already made project 00_AutoTest and try it out.

Happy test running…

Published at DZone with permission of Misko Hevery, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Rod Coffin replied on Fri, 2009/05/08 - 10:06am

Hi Misko, Have you tried out Infinitest (http://www.infinitest.org/) yet? It does exactly this for you automatically and is more intelligent about which test need to be run and which don't. Regards, Rod

Daniele Dellafiore replied on Fri, 2009/05/08 - 12:29pm

Kent Beck himself is developing a continuous testing plugin for eclipse colled JUnit Max: http://www.threeriversinstitute.org/junitmax/subscribe.html

Maarten Meijer replied on Sat, 2009/05/09 - 3:26pm

How about task context aware junit testst using Mylyn? Only run the tests that are in context...

Maarten Meijer replied on Sat, 2009/05/09 - 3:26pm

How about task context aware junit testst using Mylyn? Only run the tests that are in context...

Walter Bogaardt replied on Tue, 2009/06/30 - 9:34am

I assume you use unit test groups to identify your unit tests from your other suites of test.This the thing I like about JUnit with the annotations.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.