Brian DeJong edited this page Aug 24, 2013 · 6 revisions
Clone this wiki locally

In the jargon, "autotesting" refers to a program that watches for changes to files. When it sees a file change, it reruns any tests that depend on the changed file. To be more exact: if namespace FACTS depends on namespace SOURCE, a change to SOURCE will cause FACTS to be reloaded. That will cause the facts in FACTS to be rechecked.

Rechecking happens even if the dependency is indirect. If FACTS depends on INTERMEDIATE, which depends on SOURCE, a change in SOURCE will cause FACT to be reloaded.

By default, Midje's autotesting tracks changes in a Leiningen project's :test-paths and :source paths. Those defaults can be changed to arbitrary directories. Take care when choosing to track only some directories, since untracked directories can mean that some dependent files won't be reloaded. Consider the picture below, where the shaded directories are being tracked:

File t_a.clj tests a.clj, and t_b.clj tests b.clj. It also happens that b.clj requires a.clj. Now consider what happens when a.clj changes. It's reloaded. t_a.clj is reloaded because it depends on a.clj. b.clj is not reloaded because we didn't ask autotest to watch it. Further, t_b.clj is also not reloaded, even though autotest is watching it, because autotest doesn't know of the indirect dependency through b.clj.

Autotest options

Midje supports autotesting via the repl's autotest command and lein midje. Here's how you track the default directories:

user=> (midje.repl/autotest)
% lein midje :autotest

If you're working on a source file and its fact file, track the containing directories:

user=> (midje.repl/autotest :dirs "test/subdir" "src/subdir") ; You can use `:dir` instead.
% lein midje :autotest test/subdir src/subdir

At the moment, you can't track particular files within a directory: it's all or nothing.

Autotest supports filtering with metadata:

user=> (midje.repl/autotest :filter :core (complement :slow))
% lein midje :autotest :filter core -slow

In both cases you can use :filters instead. The :filter and dirs options can be combined, and used in either order.

By default, autotesting checks for changes twice each second. That can't be changed from the commmand line. It can be changed in the repl:

user=> (midje.repl/autotest :interval 1000)

Since intervals are measured in milliseconds, that checks once per second.