Size: 2239
Comment:
|
Size: 3227
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 103: | Line 103: |
dates | |
Line 105: | Line 104: |
== Writing a Python unit test == A unit test operates much like a regression test, but is written in Python. Here's an example: {{{ #!python #!/usr/bin/env python import sys from mercurial import bdiff, mpatch def test1(a, b): d = bdiff.bdiff(a, b) c = a if d: c = mpatch.patches(a, [d]) if c != b: print "***", `a`, `b` print "bad:" print `c`[:200] print `d` def test(a, b): print "***", `a`, `b` test1(a, b) test1(b, a) test("a\nc\n\n\n\n", "a\nb\n\n\n") test("a\nb\nc\n", "a\nc\n") test("", "") test("a\nb\nc", "a\nb\nc") test("a\nb\nc\nd\n", "a\nd\n") test("a\nb\nc\nd\n", "a\nc\ne\n") test("a\nb\nc\n", "a\nc\n") test("a\n", "c\na\nb\n") test("a\n", "") test("a\n", "b\nc\n") test("a\n", "c\na\n") test("", "adjfkjdjksdhfksj") test("", "ab") test("", "abc") test("a", "a") test("ab", "ab") test("abc", "abc") test("a\n", "a\n") test("a\nb", "a\nb") print "done" }}} |
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 ls
Then run your test:
$ ./run-tests test-example . test-example generated unexpected output: Main should be gone a 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:
- hg commit wants user interaction - use -m "text"
- hg up -m wants user interaction, set HGMERGE to something noninteractive:
cat <<'EOF' > merge #!/bin/sh echo merging for `basename $1` EOF chmod +x merge env HGMERGE=./merge hg update -m 1
- changeset hashes will change based on user and date which make
- things like hg history output change - use -d:
hg commit -m "test" -u test -d "0 0"
- diff will show the current time - strip with sed:
hg diff | sed "s/\(\(---\|+++\) [a-zA-Z0-9_/.-]*\).*/\1/"
Writing a Python unit test
A unit test operates much like a regression test, but is written in Python. Here's an example:
1 #!/usr/bin/env python
2
3 import sys
4 from mercurial import bdiff, mpatch
5
6 def test1(a, b):
7 d = bdiff.bdiff(a, b)
8 c = a
9 if d:
10 c = mpatch.patches(a, [d])
11 if c != b:
12 print "***", `a`, `b`
13 print "bad:"
14 print `c`[:200]
15 print `d`
16
17 def test(a, b):
18 print "***", `a`, `b`
19 test1(a, b)
20 test1(b, a)
21
22 test("a\nc\n\n\n\n", "a\nb\n\n\n")
23 test("a\nb\nc\n", "a\nc\n")
24 test("", "")
25 test("a\nb\nc", "a\nb\nc")
26 test("a\nb\nc\nd\n", "a\nd\n")
27 test("a\nb\nc\nd\n", "a\nc\ne\n")
28 test("a\nb\nc\n", "a\nc\n")
29 test("a\n", "c\na\nb\n")
30 test("a\n", "")
31 test("a\n", "b\nc\n")
32 test("a\n", "c\na\n")
33 test("", "adjfkjdjksdhfksj")
34 test("", "ab")
35 test("", "abc")
36 test("a", "a")
37 test("ab", "ab")
38 test("abc", "abc")
39 test("a\n", "a\n")
40 test("a\nb", "a\nb")
41
42 print "done"