Differences between revisions 2 and 3
Revision 2 as of 2008-06-13 07:46:11
Size: 4009
Editor: abuehl
Comment: typo
Revision 3 as of 2008-06-13 07:51:40
Size: 4013
Editor: abuehl
Comment: using RFC 2606, Section 3, reserved example documentation domain
Deletions are marked like this. Additions are marked like this.
Line 29: Line 29:
echo "username = John Doe <john@doe.org>" >>$HGRCPATH echo "username = John Doe <john@example.com>" >>$HGRCPATH

Debugging Tests

It's quite handy to be able to develop test scripts in parallel with the code (test-driven development). Hg's default tests/run-tests.py script described in WritingTests is a little slow for running a single test repeatedly while refining your test and code. And it runs the test in a temporary dir with an ever changing name. This is not useful for manually inspecting the state left by the test, or to run hg from within a debugger on an interim state.

Debugging Shell Tests Scripts

The following script, if placed in hg's tests folder, will run a single, simple shell test script much more quickly and in a predictable location, namely tests/tmp. It does not support test scripts involving servers, however. It assumes that it is run from within tests. Example:

~/dev/hg/tests$ ./debug-test test-guessrenames

And here is the debug-test script:

cd ..; HGDIR=`pwd`; cd tests # want an absolute path
export PATH
rm -rf tmp; mkdir tmp; cd tmp
echo "[ui]" >$HGRCPATH
echo "username = John Doe <john@example.com>" >>$HGRCPATH
. ../$1

Hg provides a number of DebuggingFeatures you can use here. For example, you might want to change a particular call from hg to hg --debugger in your testscript.

You can also add echo DEBUG; return instructions anywhere in your test script to stop and inspect the state manually or to run a particular hg command under control of an external debugger (see below). The echo DEBUG part is just a reminder so you don't forget to remove the return again. The hg --cwd option is particularly handy for switching the debuggee to tests/tmp/mytestrepo or wherever.

Using Eclipse Pydev

With the open-source [http://pydev.sourceforge.net/ Eclipse Pydev plugin], you can only launch hg directly from within [http://eclipse.org/ Eclipse]. So you need to stop the script before the command you want to debug (as described above) and setup a corresponding launch configuration in Eclipse.

Main module:


Arguments (... is the command in question):

--cwd ~/dev/hg/tests/tmp/mytestrepo

Working dir (I had to switch from the default settings to this to make the debugger work):



HGRCPATH = ~/dev/hg/tests/tmp/hgrc

Using Eclipse Pydev Extensions

With the commercially licensed [http://www.fabioz.com/pydev/ Pydev Extensions], you can attach to a running instance of hg. This relies on a Pydev supplied module, whose path you need to set up. Amend the Python path in debug-test as follows:


Then you can proceed as described in [http://www.fabioz.com/pydev/manual_adv_remote_debugger.html Pydev's remote debugging instructions]. Beware, though, that it did not work for me when placing pydevd.settrace() top-level in dispatch.py. It did work within actual commands in commands.py and also deeper within the code. Don't despair if Pydev tries to locate demandimport.py and cannot find it. Simply set a breakpoint at your desired location and continue the debugger.

If your test script contains something like:

hg stat
hg stat

and you want to break on the second instance of hg stat only, you could use a flag file to signal this. I have the following in my debug-test script:

rm /tmp/enable-hg-debugger
DBG() {
    touch /tmp/enable-hg-debugger

In the code I then do:

   1 if os.path.exists('/tmp/enable-hg-debugger'):
   2     import pydevd; pydevd.settrace()

and in the script I do:

hg stat
DBG; hg stat

CategoryContributing CategoryTesting CategoryHowTo

DebuggingTests (last edited 2011-05-03 21:59:12 by DaleWijnand)