decorators.qbk 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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:decorators Decorators]
  8. "Decorator" is a uniform mechanism for updating various attributes of the automatically registered test units. These
  9. attributes affect how the test tree is processed during the execution of the test module and include test unit
  10. description, floating-point tolerance and the number of expected failures among others. They are listed in detail in
  11. the following sections.
  12. [h4 Test case decorators]
  13. You can apply more than one decorator to the same test unit. A list of decorators is applied to a test case by specifying
  14. it as the second argument to macro __BOOST_AUTO_TEST_CASE__ or the third argument to macro __BOOST_FIXTURE_TEST_CASE__.
  15. [bt_example decorator_01..Test unit decorators..run]
  16. Each decorator in the list is preceded by an asterisk (`*`); the subsequent syntax resembles a function call and is
  17. specified in detail for each decorator. If there is more than one decorator in the list, they are concatenated with no
  18. additional separator; each asterisk indicates the beginning of a decorator. In the above example, test case `test_case1`
  19. has one associated ['decorator:] __decorator_label__. This means that when test units are filtered based on label, this
  20. test case will match to label `"trivial"`. Test case `test_case2` has three associated decorators: two of type `label`
  21. and one of type `description`.
  22. [/ ############################]
  23. [section Suite-level decorators]
  24. Similarly to test case it is possible to apply list of decorators to test suite. It is done by specifying a list of
  25. decorators as the second argument to the macro __BOOST_AUTO_TEST_SUITE__ or the third argument to the macro
  26. __BOOST_FIXTURE_TEST_SUITE__.
  27. [bt_example decorator_02..Test suite decorators..run]
  28. How a test suite decorator affects the processing of the test units inside of it varies with the decorator
  29. and is described for each decorator in subsequent sections. For instance, the function of the decorator in the above
  30. example is that when tests are filtered by label `"trivial"`, every test unit in suite `suite1` will be run.
  31. Similar to C++ namespace test suite can be closed and reopened within the same test file or span more than one file
  32. and you are allowed to apply different decorators in each point, where test suite is opened. If this is the case,
  33. the list of decorators applied to the test suite is the union of decorators specified in each place. Here an example.
  34. [bt_example decorator_03..Decorators on multiple suite openings..run]
  35. In the above example, the scope of test suite `suite1` is opened three times. This results in a test suite containing
  36. three test cases and associated with two __decorator_label__ decorators. Therefore running tests by label `"trivial"` as
  37. well as by label `"simple"` both result in executing all three test cases from the suite.
  38. [caution The above syntax for decorators requires that the compiler supports variadic macros (added in C++11). If you
  39. intend for your test program to also work for compilers without variadic macros, use explicit decorator syntax,
  40. described below.]
  41. [endsect]
  42. [/ ############################]
  43. [section Explicit decorator declaration]
  44. There is another way of associating a decorator set with test units. Macro __BOOST_TEST_DECORATOR__ indicates that its set
  45. of decorators is to be applied to the test unit or /test case sequence/ that immediately follows the declaration.
  46. [bt_example decorator_00..explicit decorator declaration..run]
  47. In the above example a decorator is applied to a [link boost_test.tests_organization.test_cases.test_case_generation
  48. data-driven test case]. Macro __BOOST_DATA_TEST_CASE__ cannot take the decorator set as one of its arguments, therefore
  49. the explicit decorator declaration is used. Macro __BOOST_DATA_TEST_CASE__ generates a sequence of 4 test cases. The
  50. decorator set is applied to each of them.
  51. Another use case for the explicit decorator declaration is when you intend for your test program to compile also on
  52. compilers without variadic macros. In this case it is recommended that you use the more verbose syntax. It is summarized
  53. in the following table:
  54. [table
  55. [[Test unit to register][Concise syntax][Universal syntax]]
  56. [[test case][```
  57. BOOST_AUTO_TEST_CASE(test_case, *decor1() *decor2())
  58. {
  59. // assertions
  60. }
  61. ```][```
  62. BOOST_TEST_DECORATOR(*decor1() *decor2())
  63. BOOST_AUTO_TEST_CASE(test_case)
  64. {
  65. // assertions
  66. }
  67. ```]]
  68. [[test case with fixture][```
  69. BOOST_FIXTURE_TEST_CASE(test_case, Fx, *decor1() *decor2())
  70. {
  71. // assertions
  72. }
  73. ```][```
  74. BOOST_TEST_DECORATOR(*decor1() *decor2())
  75. BOOST_FIXTURE_TEST_CASE(test_case, Fx)
  76. {
  77. // assertions
  78. }
  79. ```]]
  80. [[test suite][```
  81. BOOST_AUTO_TEST_SUITE(test_suite, *decor1() *decor2())
  82. // test units
  83. BOOST_AUTO_TEST_SUITE_END()
  84. ```][```
  85. BOOST_TEST_DECORATOR(*decor1() *decor2())
  86. BOOST_AUTO_TEST_SUITE(test_suite)
  87. // test units
  88. BOOST_AUTO_TEST_SUITE_END()
  89. ```]]
  90. [[test suite with fixture][```
  91. BOOST_FIXTURE_TEST_SUITE(test_suite, Fx, *decor1() *decor2())
  92. // test units
  93. BOOST_AUTO_TEST_SUITE_END()
  94. ```][```
  95. BOOST_TEST_DECORATOR(*decor1() *decor2())
  96. BOOST_FIXTURE_TEST_SUITE(test_suite, Fx)
  97. // test units
  98. BOOST_AUTO_TEST_SUITE_END()
  99. ```]]
  100. [[data-driven test case][```
  101. // not doable
  102. ```][```
  103. BOOST_TEST_DECORATOR(*decor1() *decor2())
  104. BOOST_DATA_TEST_CASE(test_case, data, var)
  105. {
  106. // assertions
  107. }
  108. ```]]
  109. [[test case template][```
  110. // not doable
  111. ```][```
  112. BOOST_TEST_DECORATOR(*decor1() *decor2())
  113. BOOST_AUTO_TEST_CASE_TEMPLATE(test_case, T, type_list)
  114. {
  115. // assertions
  116. }
  117. ```]]
  118. [[test case template with fixture][```
  119. // not doable
  120. ```][```
  121. BOOST_TEST_DECORATOR(*decor1() *decor2())
  122. BOOST_FIXTURE_TEST_CASE_TEMPLATE(test_case, T, type_list, Fx)
  123. {
  124. // assertions
  125. }
  126. ```]]
  127. ]
  128. Throughout the reminder of this documentation we use only the concise syntax.
  129. [endsect]
  130. [endsect] [/ section decorators]
  131. [/EOF]