When I program, I first write tests. Frequent manual testing can turn into a tedious task. ')
I will describe the usual code development script:
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 of them broke the program. The solution was to run the tests more frequently (after each change), but you forgot to do it.
What I want is to be able to let 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 also runs the tests ten times. And then at that moment when you do something, as you think, simple, but which in reality breaks the program, the IDE will point you to your mistakes. You can simply press Ctrl + z (cancel) and get the working code back.
What I need is for my IDE to run tests every time I save the code. For this, tests should be quick, because 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 on Ctrl + s.
I know how to do this in Eclipse, but perhaps those of you who use other IDEs can do this kind of thing. I have already prepared and created a project that you can download. That was the sequence of actions.
Create a project . Eclipse has such a handy thing as running a compilation in the background with each save.
Generate junit code 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);
}
}
Create an ant file that will run your test from the folder for compiled eclipse files. In this file there will be no instructions for compilation - eclipse will do it for you: build.xml
Specify eclipse? run ant target after each compilation. To do this, you need to create a new builder.
Open project properties by selecting the menu item Project -> Properties.
Add a new “Ant Builder” collector after Java Builder: Builders -> New ... -> Ant Builder.
Give your picker a beautiful name. And do the following on each tab (I believe that eclipse contains a bug, and you have to click Apply every time each 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 performed after each compilation).
Build Options : Set the "Specify working set" flag and select all your source folders. (This determines the changes in which files this collector will call — in In our case, these are changes to any file.)
Creating an Ant Builder :
Main tab:
Targets tab: Startup example: (not very good quality, but you can disassemble)
Or just download ( zip ) their project from my repository, import it into eclipse and try.
... works at Google as an Instructor for Agile Coach Development Methodology (Agile Coach) and teaches Googlers how to auto test culture. Its activity helps Google to release frequent releases of web applications with consistently high quality. Prior to that, Misco worked at 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 participates in the Open Source community a lot and is the author of several open source projects. His recent interests in Test-Driven Development have resulted in the Testability Explorer implementation and in JsTestDriver , with which he hopes to change the test culture in the open source community.
Upd 1: There are good plugins for Eclipse and IntelliJ that help automatically run tests - Infinitest Upd 2:Translated an article that will give an explanation of the concept of a unit test and eliminate some questions in the comments.