introduction.qbk 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. [/
  2. / Copyright (c) 2003 Boost.Test contributors
  3. /
  4. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. /]
  7. [section:intro Introduction]
  8. [role epigraph Test everything that could possibly break]
  9. [role epigraph --XP maxim]
  10. [role epigraph
  11. The acceptance test makes the customer satisfied
  12. that the software provides the business value that
  13. makes them willing to pay for it. The unit test makes
  14. the programmer satisfied that the software does what
  15. the programmer thinks it does
  16. ]
  17. [role epigraph --XP maxim]
  18. What is the first thing you need to do when you start working on new library/class/program? That's right -
  19. you need to start with the unit test module (hopefully you all gave this answer!). Occasionally, you may get
  20. away with simple test implemented using `assert`s, but any professional developer soon finds this approach
  21. lacking. It becomes clear that it's too time-consuming and tedious for simple, but repetitive unit testing
  22. tasks and it's too inflexible for most non-trivial ones.
  23. The Boost.Test library provides both an easy to use and flexible set of interfaces for writing test
  24. programs, organizing tests into simple test cases and test suites, and controlling their runtime execution.
  25. Some of Boost.Test's interfaces are also useful in production (non-test) environments.
  26. [/ ##################################################################### ]
  27. [h4 Starter example]
  28. This is how a minimal single-file test program looks like:
  29. ``
  30. #define __BOOST_TEST_MODULE__ My Test /*< Macro __BOOST_TEST_MODULE__ defines the name of our program, which will be used in messages. >*/
  31. #include <boost/test/included/unit_test.hpp> /*< This includes all the __UTF__ in a "header-only" mode; it even defines function `main`, which will call the subsequently defined test cases. >*/
  32. __BOOST_AUTO_TEST_CASE__(first_test) /*< Macro __BOOST_AUTO_TEST_CASE__ declares a ['test case] named `first_test`, which in turn will run the content of `first_test` inside the
  33. controlled testing environment.>*/
  34. {
  35. int i = 1;
  36. __BOOST_TEST__(i); /*< This test checks if `i` is non-zero. >*/
  37. __BOOST_TEST__(i == 2); /*< This test checks if `i` has value `2` (something more than just evaluating the equality operator). >*/
  38. }
  39. ``
  40. When run, it produces the following output:
  41. [pre
  42. Running 1 test case...
  43. test_file.cpp(8): error: in "first_test": check i == 2 has failed [1 != 2]
  44. *** 1 failure is detected in the test module "My Test"
  45. ]
  46. [/ ##################################################################### ]
  47. [include overview.qbk]
  48. [endsect]
  49. [/ EOF]