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
- 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.
- Create a JUnit Text runner as shown here: here
- 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.
- Tell Eclipse to run your ant target after every compilation cycle. This is known as Builder in Eclipse.
- Open project properties: Right click project -> Properties
- Create new ant builder step after the Java Builder in eclipse: Builders -> new -> Ant Builder
- 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…
19 responses so far ↓
Great post! I’ve been looking for something like this for a while now for home and at work. Have you thought about just having your ANT build run all tests for you instead of managing a TestSuite? http://junit.sourceforge.net/doc/faq/faq.htm#running_15Thanks again.
Corrected link:http://junit.sourceforge.net/doc/faq/faq.htm#running_15
Also here is a reference to making JUnit 4 TestSuites http://stackoverflow.com/questions/457276/junit4-test-suites
There is also a think called infinitest that does this. http://code.google.com/p/infinitest/
Another approach is to use a continuous testing tool like Infinitest. CT tools make this process a little easier because they prioritize tests (running failing tests, and test likely to fail, first). Infinitest also selects a subset of tests to run depending on what changes you make.Check out eclipse.infinitest.org for more about the Eclipse plugin.
Nice tip, thanks! An alternative would be the Infinitest plugin (for Eclipse and IntelliJ) at http://www.infinitest.org.There is also Kent Beck’s JUnitMax (commercial)./Vlad
Better options are infinitest and JUnitMax. http://www.infinitest.org/
If you’re a Vi or command-line person, with access to a UNIX shell, you can do the same thing with the command ‘watch’, which will run a given command every N seconds, displaying the output, and optionally display the differences from one run to the next in inverse.
I created a small Python script last week to try to make a version of ‘watch’ that could be run on Windows too, but it’s currently languishing under a couple of dealbreaker bugs:
http://code.google.com/p/testwatcher/
If anyone who knows about good ways to manipulate the Windows text terminal that cmd runs in then I’d love to hear from you (eg. how to find out the size of the terminal your command-line program is running in, or control codes to move the print output position or at least reset it to (0,0))
Hello,Nice idea : you may have a look at the new project of Kent Beck: JUnit Max. It’s exactly what you present here.Coderfriendly
I forgot to give you this link: it is a short post that I’ve written on JUnit Max: JUnit Max : Continuous integration goes further
great article. It would be nice to have something that looks for all
test classes. Everytime a new test case is added, it needs to be put
into the TestSuite suite Class for AllTests
Hi Misko,first of all I would like to thank you for the inspiring talk at GeeCON yesterday. I hope that your voice emission is back to normal
Regarding “after-save automated testing” there is also a plugin for eclipse/idea, maybe you’ll find it useful http://code.google.com/p/infinitest/
WOW thanks to all which pointed me to http://www.infinitest.org. It is perfect and a way better than my quick hack!
For command line use on Linux: the inotify syscall allows one to monitor file system events.Write your own little C-program or use one of the available wrappers: http://inotify-tools.sourceforge.net/ for shells, http://pyinotify.sourceforge.net/ for Python, or http://search.cpan.org/~mlehmann/Linux-Inotify2-1.2/ for Perl. (I’ve only tried the Python wrapper.)
Software Testing Categorization // Jul 14, 2009 at 12:28 pm
[...] is that you want to run ALL (thousands) of your unit-tests every time you modify anything, preferably on each save. My patience is two seconds max. In two seconds I want to make sure that all of my unit tests ran [...]
James Carr » Blog Archive » links for 2009-08-15 // Aug 15, 2009 at 6:02 am
[...] Configure your IDE to run your tests automatically Share and Enjoy: [...]
JUnit4 continuous testing builder « A Place In The Queue // Oct 1, 2009 at 1:27 pm
[...] da acerisara il 1 Ottobre 2009 Just a little upgrade to what M. Hevery has explained in his blog. If you want to use JUnit4, change your tests suite like [...]
I shared my experience of integration JsTestDriver in Visual Studio here: http://slmoloch.blogspot.com/2009/08/how-to-run-jstestdriver-with-visual_02.html
Can you help me out in setting up PhantomJS wih eclipse.
Leave a Comment