testing_output_streams.qbk 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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:output_stream_testing Output streams testing tool]
  8. How would you perform correctness test for ``operator<< ( std::ostream &, ... )`` operations?
  9. You can print into the standard output stream and manually check that it is matching your expectations.
  10. Unfortunately, this is not really acceptable for the regression testing and doesn't serve a long term purpose of a
  11. unit test.
  12. You can use `std::stringstream` and compare resulting output buffer with the
  13. expected pattern string, but you are required to perform several additional operations with every check you do. So it
  14. becomes tedious very fast.
  15. The class [classref boost::test_tools::output_test_stream] is designed to automate these tasks for you. This is a simple,
  16. but powerful tool for testing standard `std::ostream` based output operation. The class `output_test_stream`
  17. complies to `std::ostream` interface so it can be used in place of any
  18. `std::ostream` parameter. It provides several test methods to validate output content,
  19. including test for match to expected output content or test for expected output length. Flushing, synchronizing,
  20. string comparison and error message generation is automated by the tool implementation.
  21. All `output_test_stream` validation member functions by default flush the stream once the check is performed.
  22. If you want to perform several checks with the same output, specify parameter `flush_stream`
  23. with value `false` [footnote This parameter is supported on all comparison methods, see the class
  24. [classref boost::test_tools::output_test_stream documentation.] ].
  25. In some cases manual generation of expected output is either too time consuming or is impossible at all because
  26. of sheer volume. A possible way to address that issue is to split the test in two steps:
  27. # first by checking the expected output manually
  28. # second to save this output to ensure that future checks produce the same output
  29. The class `output_test_stream` allows both the matching of the output content versus a /pattern file/ and generation
  30. of this pattern file. The command line parameter [link boost_test.utf_reference.rt_param_reference.save_pattern `save_pattern`]
  31. may be used to either generate a new pattern file, or to check against an existing pattern.
  32. [h3:usages Usage]
  33. There are two ways to employ the class `output_test_stream`:
  34. # explicit output checks and
  35. # pattern file matching
  36. [h4 Explicit output checks]
  37. Use the instance of class `output_test_stream` as an output stream and check output content using tool's methods.
  38. [bt_example example28..Explicit output checks with `output_test_stream`..run-fail]
  39. [note Use of `false` to prevent output flushing in first two invocation of check functions. Unless
  40. you want to perform several different checks for the same output you wouldn't need to use it though. Your
  41. test will look like a sequence of output operators followed by one check.]
  42. [tip Try to perform checks as
  43. frequently as possible. It not only simplifies patterns you compare with, but also allows you to more closely
  44. identify possible source of failure.]
  45. [h4 Pattern file matching]
  46. The ['pattern file] is a companion file containing the patterns that the stream should match. Your testing will look
  47. like a series of output operators followed by match pattern checks repeated several times.
  48. In the example below, the file `pattern_file` contains the patterns that should match.
  49. [pre
  50. i=2
  51. File: test.cpp Line:XXX
  52. ]
  53. [bt_example example29..Pattern file matching with `output_test_stream`..run-fail]
  54. [tip Try to perform checks as frequently as possible, because it allows you to more closely identify possible source
  55. of failure
  56. ]
  57. [endsect] [/ output stream testing]