C++ Unit Testing Framework: A Boost Test Tutorial——part2:Using Boost Test

來源:互聯網
上載者:User

原文地址:http://www.beroux.com/english/articles/boost_unit_testing/?part=2

Following the crash-course introduction, let's see how to install and use Boost, plus some references.

Installing Boost

Now that you have an idea about the benefit we'll briefly see how to install Boost.

On Windows for Visual C++ the simplest way (and also the way promoted by Boost) is to use Boost from BoostPro Computing.

Here are the full installation step. If the installation setup fails, restart it.

  1. Download the latest Boost installer from BoostPro Computing: http://www.boostpro.com/products/free
  2. Install the setup:
    1. Select whatever mirror you like.
    2. It is advised to checked those checked with a cross below:
      [ ] Visual C++ 8.0 (Visual Studio 2005)



      [X] Visual C++ 9.0 (Visual Studio 2008)







      [X] Mulithread Debug, DLL --DLL Debug, required when BOOST_DYN_LINK defined.



      [X] Mulithread, DLL --DLL Release, required when BOOST_DYN_LINK defined.



      [X] Mulithread --LIB (for CRT DLL) Release, default in Release.



      [X] Mulithread Debug --LIB (for CRT DLL) Debug, default in Debug.



      [X] Mulithread, static runtime --LIB (for CRT LIB) Release.



      [ ] Mulithread Debug, static runtime --LIB (for CRT LIB) Debug.



      [ ] Single thread, static runtime



      [ ] Single thread Debug, static runtime
    3. Keep the default options on the next screen.
    4. Wait for the download and installation to complete. In case of error, restart.
    5. Say YES to add Boost to the path at the end.
  3. Add Boost to the include and library folders of Visual Studio:
    1. Open Visual Studio
    2. Go to the menu Tools > Options...
    3. Go to Project and Solutions > VC++ Directories
    4. Add to your Include files: C:"Program Files"boost"boost_X_XX_X
    5. Add to your Include files: C:"Program Files"boost"boost_X_XX_X"boost"tr1
    6. Add to your Library files: C:"Program Files"boost"boost_X_XX_X"lib
  4. (optional) Path inclusion: If you use Boost DLLs (rare) you may add C:"Program Files"boost"boost_X_XX_X"lib to your system PATH.
  5. (optional) Documentation shortcut: Create a shortcut to C:"Program Files"boost"boost_X_XX_X"more"index.htm
  6. Done.
Creating a unit test project in Visual C++

You know how to write C++ code using Boost Test and you have Boost installed. Let's see how to create a nice project for unit testing in Visual Studio.

  1. Open your existing project to test.
  2. Go to menu File > Add > New Project...
  3. Create a Visual C++ empty project.
  4. Either add the classes to test so they get compiled, or make a library and add a reference to your other project (better). You probably also want to have the same StdAfx.h.
  5. In the project settings Build Events > Post-Build Eventdefine:
    Command Line: "$(TargetPath)" --result_code=no --report_level=no --catch_system_error=yes
  6. Include your runner and other test cases.

If you generated a library from your code, you probably have at least third project that will use this library to make the application. You can mark the unit test to be built first, this way you'll always have unit tests checks done each build.

Official documentation

You can find the official documentation here. The documentation may be hard to read, so here are some tips to find your things in it:

  • The BOOST_CHECK and other commands: Boost Test Library > The Unit Test Framework > Testing tools
  • Runner command-line options: Boost Test Library > The Unit Test Framework > Runtime configuration > Parameters reference
Conclusion

Boost Test is now simple to use (once you found your way through their documentation or after reading this article) and provides from minimal tests to complex scenarios. You can find some more advanced features like function base test cases. I hope Boost has now been demystified for you; it's now my favorite C++ testing framework.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.