Configure IDE to run tests automatically

Original author: Miško Hevery
  • Transfer
Source When I program, I first write tests. Frequent manual test runs can turn into a tedious task. I will describe the usual scenario of code development: your tests are “green” and you have started refactoring, which in your opinion is simple and safe. After that, you ran the tests and saw that something was broken. But before that, you have already made ten small changes and do not know which one broke the program. The solution was to run the tests more often (after each change), but you forgot to do it.

Google Testing Blog








What I want is to be able to have my development environment (IDE) automatically run tests every time I make changes to the code. This method implies that when I make ten small changes, the IDE runs the tests ten times. And then at the moment when you are doing something that you think is simple, but which actually breaks the program, the IDE will tell you your mistakes. You can just press Ctrl + z (cancel) and get back the working code.

What I need is for my IDE to run tests every time I save the code. To do this, the tests should be quick, since my patience after pressing Ctrl + s is only 2 seconds. Anything lasting will annoy me more. If your test suite runs slowly, then refactor it.

This technique is only suitable for quick unit tests . I do not run my script tests as an event by Ctrl + s.


I know how to do this in Eclipse, but maybe those of you who use other IDEs can do a similar thing. I have already prepared and created a project that you can download. Here is the sequence of actions.
Eclipse IDE

  1. Create a project . Eclipse has such a convenient thing as running a compilation in the background every time it is saved.
  2. Create code for junit to run your tests, as shown here: AllTests.java
    import junit.framework.TestSuite;
    import junit.textui.TestRunner;
    public class AllTests {
       public static void main (String [] args) {
          TestSuite suite = new TestSuite ();
          suite.addTestSuite (GreeterTest.class);
          TestRunner.run (suite);
       }
    }
    

  3. Create an ant file that will run your test from the folder for the compiled eclipse files. There will be no compilation instructions in this file - eclipse will do it for you:
    build.xml

  4. Specify eclipse? u run the ant target after each compilation. To do this, create a new builder.
    1. Open the project properties by selecting the menu item Project -> Properties.
    2. Add a new “Ant Builder” builder after Java Builder: Builders -> New ... -> Ant Builder.
    3. Give your collector a pretty name. And do the following on each tab (I believe that eclipse contains a bug, and you have to click Apply every time you change the data in the tab):
      • Main : Select the build file and the base directory (this sets which ant file to use).
      • Targets : Select your ant target for: After Clean, Manual Build, and Auto Build (most important) (This indicates that an ant target should be executed after each compilation).
      • Build Options : Set the “Specify working set” flag and select all of your source folders. (This determines the changes in which files this collector will cause - in
        our case, these are changes to any file.)

Creating a builder (Ant Builder) :


Main tab:


Targets tab:

Launch example: (not very good quality, but you can parse it)



Or just download ( zip ) their project from my repository, import it into eclipse and try it.

Successful testing ...

About the author: Mishko Hevery (Miško Hevery)


... works in Google instructor on  agile development (Agile Coach) and teaches Googlers culture automated testing. His work helps Google release frequent web application releases with consistently high quality. Prior to this, Misco worked for Adobe, Sun Microsystems, Intel and Xerox (and this is not a complete list), where he became an expert in developing web applications in Java, JavaScript, Flex and ActionScript. He has been involved in the Open Source community a lot and has authored several open source projects. Recently, his interest in test-driven development (Test-Driven-Development) resulted in the implementation of the Observer testability (Testability Explorer) and  JsTestDriverwith whom he hopes to change the testing culture in the open source community.

Open Source Projects:


Upd 1: There are good plugins for Eclipse and IntelliJ that help you run tests automatically - Infinitest
Upd 2: I translated an article that gives an explanation of the concept of a unit test and eliminates some of the questions in the comments.

Also popular now: