Debugging Unit Tests

From MantidProject

(Redirected from DebuggingUnitTests)
Jump to: navigation, search

Eclipse

Here is a method I used in order to get debugging working in Eclipse under Linux. I am working with the idea that you will want to debug your test runner.

  1. Build and run your test using the ./runTests.sh script in the test directory.
    1. You can use ./runTests.sh thenameofthetestyouwanttorun.h to do only one of the tests
  2. For the project you are working on (you will have to do this for each project):
    1. Go to Run->Debug Configurations
    2. Create a new Debug Configuration. I called mine "Nexus Test Debug"
    3. For me, it worked best using the "Standard Create Process Launcher" (bottom option on the "Main" tab)
    4. Set the C/C++ application to the path to the "runner.exe" file made by ./runTests.sh
    5. Set the Working Directory (under "Arguments" tab) to "${workspace_loc:Nexus}/test" (replace Nexus with your project name)
    6. Under environment, add a "LD_LIBRARY_PATH" variable with the value "${workspace_loc}/debug" (this gives the path to the compiled libraries)
    7. Under "Common" you can put the debug config in your "favorites" menu.

You can then run the debugger on runner.exe from the Run menu or the toolbar.

Test Monitor

The utility script monitorTests.py, under the Code/Mantid/ folder, is handy to automate testing while you work on a program.

  • Run this from the ProjectName/test/ subfolder!
  • You can also specify command line arguments. These will be fed to the ./runTests.sh script that is being called.
  • For example, in Geometry/test, to only do the QuatTest file:
    • $../../monitorTests.py QuatTest.h
    • This will run continually (until you stop with Ctrl+C).
    • If any of the library files change (in ../../debug/*.so) or any of the test files change (*.h in current directory), the tests are rerun automatically.
  • The way it is written, it will only run under linux, but could be modified for Windows use too.

Visual Studio

The process here is not as straight forward as it is in eclipse. It involves a few steps, but it does work!

1. Edit the test to add a pause at the start of the test you are interested in. This will make the test wait for keyboard input, all you need to do is hit enter, but you can use this delay to attach to the debugger to running process.

   std::string s;
   std::getline(std::cin,s);

2. Edit the runTests.bat file and add REM to the following lines at the end of the file. This ensure that the required file are not deleted.

REM del *.pdb
REM del runner.exe

3. Run the runTests.bat file. This builds and runs the tests. Do not attempt to attach the debugger this time. As the test are being hosted in a cmd.exe process you will not be able to load the debugging symbols.
4. Now using Windows Explorer run the runner.exe file directly by clicking on it.
5. When the test pauses for input within visual studio select Debug\Attach to Process... Image:AttachToProcess.PNG
6. Select runner.exe from the list and click attach. Visual studio will change to debug mode.
7. Set any breakpoints you want, go back to the runner.exe window and hit enter to stop the pause. This should then ctach on any breakpoints you have set.
8. VERY IMPORTANT - Clean up when you have done, and do not check in with any paused tests!

Personal tools
Create a book