Visual Studio integration

From CxxTest wiki

Jump to: navigation, search

These instructions work for Visual Studio 2005. If you have to do anything different for other versions of Visual Studio, please add that information.

Setting up a test project

  1. Create a new project in your solution. Use the Win32 Console Application template. Name it something like "Test suite". In the Settings dialog, turn off precompiled header and turn on empty project. Leave the type as console application and leave the ATL and MFC header files checkboxes unchecked.
  2. Open the Project Dependencies dialog and make this project dependent on any other projects that you want to test.
  3. Add a new C++ file to the project as a placeholder. This makes the configuration dialog show the C/C++ pane.
  4. Open the project properties dialog. Make sure to set the configuration to All Configurations or this will cause headaches later when things don't work as expected. Go to C/C++ | General, find Additional Include Directories, and enter the path to the cxxtest framework and any other includes you may need, say the headers for the system under test. You may also have to define some preprocessor definitions, so you might as well put them in now. Save these settings and close the dialog.
  5. In the Solution Explorer, delete the existing "filters" and create new ones. Call them something like "Main", "Test code", and "Generated source (do not edit!)".
  6. Add a new header file called Main.h and put a dummy test in it to force the script to generate output. To test your setup, have your dummy test call TS_FAIL(). Open its properties dialog, set the configuration to All Configurations, and enter the following under Custom Build Step:
    • (Visual Studio 2010 only) Under Configuration Settings -> General, set the Item Type to Custom Build Tool. Close and re-open the Properties window (don't forget to set the configuration to All Configurations again.)
    • For Command Line, enter python <path to cxxtest>/cxxtestgen.py --runner=ParenPrinter -o $(InputName).cpp "$(InputFileName)".
      • (Visual Studio 2010 only) Instead, use python <path to cxxtest>/cxxtestgen.py --runner=ParenPrinter -o %(Filename).cpp "%(FullPath)".
    • For Description, enter "Generating main code for test suite.".
    • For Outputs, enter Main.cpp.
  1. Build this project. This generates a Main.cpp, which contains main() and some other single instance code for the test suite. You should see
    Generating main code for test suite.
    in the build output. If you get any errors, double-check the script and includes path in the file's properties.
  2. Add an existing file and choose Main.cpp, adding it to your Main folder.
  3. Open the project properties dialog again, set the configuration to All Configurations, and enter the following under Custom Build Step:
    • For Command Line, enter "$(OutDir)/$(ProjectName).exe". Note that you can change the name of your test executable here and in the Linker settings if you so choose, so long as the two match.
    • For Description, enter "Running test suite.".
    • For Outputs, enter none.
  4. Build the project again. This will compile your new Main.cpp and run the resulting binary. If you put the test TS_FAIL() in your header, you should get an error. Once you've confirmed that it works, you can delete or comment out that TS_FAIL(). Check that everything works by building the project again and you should see
    Running 1 test.OK!
    in the build output, with no errors.

Setup complete! You can now start adding test files to your suite.

Adding a new test file

  1. Add a new header file. Strictly speaking, you can name it anything you like, but for sanity's sake name it something like "Test<system>.h". Put a dummy test in it for now, and for setup testing purposes, have your it call TS_FAIL().
  2. Open the header's properties dialog. Make sure to set the configuration to All Configurations or this will cause headaches later when things don't work as expected. Enter the following under Custom Build Step:
    • (Visual Studio 2010 only) Under Configuration Settings -> General, set the Item Type to Custom Build Tool. Close and re-open the Properties window (don't forget to set the configuration to All Configurations again.)
    • For Command Line, enter python <path to cxxtest>/cxxtestgen.py --part -o "$(InputName).cpp" "$(InputFileName)".
      • (Visual Studio 2010 only) Instead, use python <path to cxxtest>/cxxtestgen.py --part -o "%(Filename).cpp" "%(FullPath)".
    • For Description, enter "Generating test code for $(InputName).".
      • (Visual Studio 2010 only) Instead, use "Generating test code for %(Filename).".
    • For Outputs, enter $(InputName).cpp.
      • (Visual Studio 2010 only) Instead, enter %(Filename).cpp.
  3. Build the project. This generates a $(InputName).cpp file, which contains just the code for the tests in this module, without main() or any other single-instance code. You should see
    Generating test code for Test<system>.
    in the build output. If you get any errors, double-check the script path in the file's properties.
  4. Add an existing file and choose $(InputName).cpp, adding it to your "Generated source (do not edit!)" folder.
  5. Build the project again. This will compile your new Main.cpp and run the resulting binary. You should see
    Running test suite.
    in the build output. If you put the test TS_FAIL() in your header, you should also see an error. Once you've confirmed that it works, you can replace that TS_FAIL() with real test code.

And you're done. You can start adding real tests to this test file now.

Personal tools