Configure your IDE to run your tests automatically

May 7th, 2009 · 19 Comments ·

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…

Tags: Uncategorized

19 responses so far ↓

Leave a Comment