Mercurial contains a simple regression test framework that allows both Python unit tests and shell-script driven regression tests.

Running the test suite

To run the tests, do:

$ make tests
cd tests && ./run-tests
Ran 44 tests, 0 failed.

This finds all scripts in the tests/ directory named test-* and executes them. The scripts can be either shell scripts or Python. Each test is run in a temporary directory that is removed when the test is complete.

You can also run tests individually:

$ cd tests/
$ ./run-tests test-pull test-undo
Ran 2 tests, 0 failed.

A test-<x> succeeds if the script returns success and its output matches test-<x>.out. If the new output doesn't match, it is stored in test-<x>.err.

Writing a shell script test

Creating a regression test is easy. Simply create a shell script that executes the necessary commands to exercise Mercurial.

Here's an example:

hg init
touch a
hg add a
hg commit -m "Added a" -d "0 0"

touch main
hg add main
hg commit -m "Added main" -d "0 0"
hg checkout 0

echo Main should be gone

Then run your test:

$ ./run-tests test-example
test-example generated unexpected output:
Main should be gone

Ran 1 tests, 1 failed.

Double-check your script's output, then save the output so that future runs can check for the expected output:

$ mv test-example.err test-example.out
$ ./run-tests test-example
Ran 1 tests, 0 failed.

There are some tricky points here that you should be aware of when writing tests:

cat <<'EOF' > merge
echo merging for `basename $1`
chmod +x merge

env HGMERGE=./merge hg update -m 1

hg commit -m "test" -u test -d "0 0"

hg diff | sed "s/\(\(---\|+++\) [a-zA-Z0-9_/.-]*\).*/\1/"