Skip to content

Latest commit

 

History

History
57 lines (46 loc) · 2.72 KB

eunit_auto_test_runner.asciidoc

File metadata and controls

57 lines (46 loc) · 2.72 KB

Automatically Running Tests

What if every time a source file changed the system would notice and run your tests again? One of the best practices for unit tests is that they should provide a very short feedback loop for possible bugs. Ideally within a second or two of saving a file the system could run the test suite and provide some form of visual feedback.

This will require a reasonably powerful computer, so it probably should not be run on a netbook with an Atom processor, but on a more powerful machine there are probably cycles to spare so that is ok. Of course you can also do something like have this run on a faster machine on the network.

Monitoring files for changes

In order to do this we need a few things. First we need a way to monitor a set of files for changes. Then when a change has been noticed we should compile any changed files and run the tests. However it is not enough to monitor the erlang source files .erl the monitor should also monitor any header .hrl files that are included. Ideally the system would also be able to show you what has changed from when the last time the tests passed.

There are a few ways we can monitor the files for changes. If the user is using Emacs we can attach a function to a save hook that will use Distel to call our testing server and run the tests. But that only works if the user is running Emacs. If members of the team are using VIM or Eclipse we need something else.

Linux provides a set of events called inotify to show you when files have changed. There is a module for Elang that can be found on github at https://github.com/massemanet/inotify that will let you monitor the file system. But again this is not portable to the Mac or Windows so we will skip it.

The final option is to just poll the file system. If we keep a last modified date or a checksum for each file we can check every N seconds for changes and run our tests. This is rather brute force and lacks some level of elegance, but is reasonably simple and will work independent of the user’s tool chain and platform.

One way of doing this would be to use a Makefile

The User Interface

In addition the system needs a way to alert the developer that a test has broken. This can be done in several ways. It would of course be possible to have the test runner print a message to the Erlang console, this has the advantage of being very flexible and not requiring any other software. If you are developing with Emacs it could alert the user via a plugin and Distel <ulink url="https://github.com/massemanet/distel"/>. We could also create a desktop widget with something like etk or wx-erlang that will display an error. Finally it could report to a web browser over a web socket.