runtime_custom.qbk 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 Custom command line arguments]
  8. It is possible to pass custom command line arguments to the test module. The general format for passing custom
  9. arguments is the following:
  10. ``
  11. <boost_test_module> [<boost_test_arg1>...] [-- [<custom_parameter1>...]
  12. ``
  13. This means that everything that is passed after "`--`" is considered as a custom parameter and will not be intercepted nor interpreted
  14. by the __UTF__. This avoids any troubleshooting between the __UTF__ parameters and the custom ones.
  15. There are several use cases for accessing the arguments passed on the command line:
  16. * instantiating an object used in test cases and which is dependant on parameters external to the test-module:
  17. the name of the graphic card, the credentials for a database connection, etc. The rest of the test module would check
  18. that the functions in test are not sensitive to this type of parametrization. One can also imagine running this same
  19. test module with different parameters (different graphic cards...) in a batched manner,
  20. * modifying the test tree by adding or parametrizing test cases: the arguments passed on the command line may contain
  21. for instance a set of parameters that define test cases.
  22. In the first scenario, [link ref_consuming_cmd_test_case test cases] or fixtures, including
  23. [link ref_consuming_cmd_global_fixture global fixtures], may be used. Since those are part of the test tree, they can benefit from the __UTF__ rich set of assertions
  24. and controlled execution environment.
  25. In the second scenario, the command line argument interact directly with the content of the test tree: by passing specific
  26. arguments, different set of tests are created. There are mainly two options for achieving this: using a dedicated
  27. [link ref_consuming_cmd_init_function initialization function] or using [link ref_consuming_cmd_dataset data driven] test cases.
  28. The error handling of the command line parameters needs however to be adapted.
  29. [#ref_consuming_cmd_test_case][h4 Consuming custom arguments from a test case]
  30. The [link boost_test.tests_organization.test_tree.master_test_suite master test suite] collects the custom arguments
  31. passed to the test module in the following way:
  32. * `argv[0]`, usually set by the operating system as the executable name, remains unchanged
  33. * any argument interpreted by the test module is removed from `argv`
  34. * the empty token `--` is removed as well
  35. * any additional argument passed after the empty token is reported in `argv` starting at index `1`
  36. [bt_example runtime-configuration_1..Basic custom command line..run-fail]
  37. [#ref_consuming_cmd_global_fixture][h4 Consuming custom arguments from a global fixture]
  38. Another possibility for consuming the custom command line arguments would be from within a
  39. [link boost_test.tests_organization.fixtures.global global fixture]. This is especially useful
  40. when external parameters are needed for instantiating global objects used in the test module.
  41. The usage is the same as for test cases. The following example runs the test module twice with
  42. different arguments, and illustrate the feature.
  43. [tip The global fixture can check for the correctness of the custom arguments and may abort the full run
  44. of the test module.]
  45. [bt_example runtime-configuration_2..Command line arguments interpreted in a global fixtures..run-fail]
  46. The above example instantiates a specific device through the `DeviceInterface::factory` member function. The
  47. name of the device to instantiate is passed via the command line argument `--device-name`, and the instantiated
  48. device is available through the global object `CommandLineDeviceInit::device`.
  49. The module requires `3` arguments on the command line:
  50. * `framework::master_test_suite().argv[0]` is the test module name as explained in the previous paragraph
  51. * `framework::master_test_suite().argv[1]` should be equal to `--device-name`
  52. * `framework::master_test_suite().argv[2]` should be the name of the device to instantiate
  53. As it can be seen in the shell outputs, any command line argument consumed by the __UTF__ is removed from
  54. `argc` / `argv`. Since global fixtures are running in the __UTF__ controlled environment, any fatal error reported
  55. by the fixture (through the __BOOST_TEST_REQUIRE__ assertion) aborts the test execution. Non fatal errors
  56. on the other hand do not abort the test-module and are reported as assertion failure, and would not prevent the execution
  57. of the test case `check_device_has_meaningful_name`.
  58. [note It is possible to have several global fixtures in a test module, spread over several compilation units.
  59. Each of those fixture may in turn be accessing a specific part of the command line.]
  60. [#ref_consuming_cmd_init_function][h4 Parametrizing the test tree from command line in the initialization function]
  61. The initialization function are described in details in this [link boost_test.adv_scenarios.test_module_init_overview section].
  62. The initialization function is called before any other test or fixture, and before entering the master test suite. The initialization
  63. function is not considered as a test-case, although it is called under the controlled execution
  64. environment of the __UTF__. This means that:
  65. * the errors will be properly handled,
  66. * loggers are not fully operational,
  67. * it is not possible to use the __UTF__ assertion macros like __BOOST_TEST__ as it is not a test-case.
  68. The following example shows how to use the command line arguments parsing described above to create/add new test cases
  69. to the test tree. It also shows very limited support to messages (does not work for all loggers), and error handling.
  70. [bt_example runtime-configuration_3..Init function parametrized from the command line..run-fail]
  71. As seen in this example, the error handling is quite different than a regular test-case:
  72. * For the /alternative/ initialization API (see
  73. __BOOST_TEST_ALTERNATIVE_INIT_API__), the easiest way to indicate an error would be to return `false`
  74. in case of failure.
  75. * For the /obsolete/ and /alternative/, raising an exception such as `std::runtime_error` or
  76. [classref boost::unit_test::framework::setup_error] as above works as well.
  77. [#ref_consuming_cmd_dataset][h4 Data-driven test cases parametrized from the command line]
  78. It is possible to use the command line arguments to manipulate the dataset generated by a data-drive test case.
  79. By default, datasets are created before entering the `main` of the test module, and try to be efficient in the number
  80. of copies of their arguments. It is however possible
  81. to indicate a delay for the evaluation of the dataset by constructing the dataset with the `make_delayed` function.
  82. With the `make_delayed`, the construction of the dataset will happen at the same time as the construction of the
  83. test tree during the test module initialization, and not before. It is this way possible to access the
  84. [link boost_test.tests_organization.test_tree.master_test_suite master test suite] and its command line arguments.
  85. The example below shows a complex dataset generation from the content of an external file. The data contained
  86. in the file participates to the definition of the test case.
  87. [bt_example runtime-configuration_4..Dataset test case parametrized from the command line..run-fail]
  88. * Using `make_delayed`, the tests generated from a dataset are instantiated during the framework setup. This
  89. let the dataset generator access the `argc` and `argv` of the master test suite.
  90. * The generation of the test-cases out of this dataset happens before the global fixture are reached (and before
  91. any test cases), and after the initialization function.
  92. * The generator of the dataset is [*not] considered being a test case and the __UTF__ assertions are not accessible.
  93. However, the __UTF__ will catch the exceptions raised during the generation of the test-cases by the dataset.
  94. To report an error, a `std::logic_error` or [classref boost::unit_test::framework::setup_error] can be raised
  95. and will be reported by the __UTF__.
  96. [/
  97. [h4 Handling errors]
  98. The handling of errors that happen during the command line parsing has been discussed through the examples above.
  99. Some additional notes:
  100. * an exception occurring in a global fixture or the initialization function will be caught by the framework and will abort
  101. the test module
  102. * a global fixture is attached to the [link boost_test.tests_organization.test_tree.master_test_suite master test suite], and
  103. any failure there will be reported by the loggers properly.
  104. * A global fixture cannot manipulate the test tree, while the data-driven tests or custom initialization functions can.
  105. ]
  106. [endsect] [/ Custom runtime parameters]