Home > The Unit Test Framework > Testing tools > Output testing tool
PrevNext

Output testing tool

How do you perform correctness test for operator<<( std::ostream&, ... ) operations? You can print into the standard output stream and manually check that it is matching your expectations. Unfortunately, this is not really acceptable for the regression testing and doesn't serve a ling term purpose of a unit test. You can use std::stringstream and compare resulting output buffer with the expected pattern string, but you are required to perform several additional operations with every check you do. So it becomes tedious very fast. The class output_test_stream is designed to automate these tasks for you. This is a simple, but powerful tool for testing standard std::ostream based output operation. The class output_test_stream complies to std::ostream interface so it can be used in place of any std::ostream parameter. It provides several test methods to validate output content, including test for match to expected output content or test for expected output length. Flushing, synchronizing, string comparison and error message generation is automated by the tool implementation.
如何為 operator<<(std::ostream&, ... ) 操作執行正確性檢查? 你可以打印到標準輸出流中,手動檢查是否符合你的期望。 不幸的是,這在回歸測試中不可接受,也不是單元測試的長期目的。 你可以使用 std::stringstream 並將結果輸出緩衝區和期望模式字符串進行比較, 但在每個檢查中你都需要執行額外幾步操作。這很快就會讓人覺得厭煩。 output_test_stream 類就是用來自動化完成這些任務的。 這是一個簡單但強大的工具用於測試基於 std::stream 的操作。 output_test_stream 類包含了 std::ostream 的接口,可以應用在任何使用 std::ostream 作為參數的地方。 它提供了一些測試方法用來驗證輸出內容,包括測試是否匹配期望輸出內容,是否符合期望輸出長度。 刷新,同步,字符串比較和錯誤信息生成都是由工具自動實現的。

In some cases it still may not be good enough. It becomes even more obvious when it's difficult to generate the expected pattern. What we need in cases like this is to be able to check once manually that the output is as expected and to be able in a future check that it stays the same. To help manage this logic the class output_test_stream allows matching output content versus specified pattern file and produce pattern file based on successful test run.
在某些情況下,它仍不夠好。尤其是它很難生成期望模式。 像這種情況,我們需要的就是能夠手動檢查一次,並能夠檢查將來的輸出是否仍然一致。 為了管理這種邏輯,類 output_test_stream 允許將輸出內容與特定的模式文件進行匹配, 並且基於成功的測試產生模式文件。

Detailed specification of class output_test_stream is covered in reference section.
output_test_stream 的詳細定義見參考部分。

Usage

There are two ways to employ the class output_test_stream: explicit output checks and pattern file matching.
有兩種方法使用類 output_test_stream:顯式輸出檢查和模式文件匹配。

Example 34. output_test_stream usage with explicit output checks

Use the instance of class output_test_stream as an output stream and check output content using tool's methods. Note use of false to prevent output flushing in first two invocation of check functions. Unless you want to perform several different checks for the same output you wouldn't need to use it though. Your test will look like a serious of output operators followed by one check. And so on again. Try to perform checks as frequently as possible. It not only simplifies patterns you compare with, but also allows you to more closely identify possible source of failure.
使用類 output_test_stream 的實例作為輸出流並使用工具方法檢查輸出內容。 注意,使用 false 阻止輸出在前兩次檢查函數的調用中刷新。 除非你想要為相同的輸出執行幾個不同的檢查,否則你不需要它。 你的測試看上去像是一個輸出操作,跟著一個檢查。盡可能頻繁地執行檢查。 它不僅簡化你要比較的模式,同時讓你更新接近發生錯誤的可能源。

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>
#include <boost/test/output_test_stream.hpp> 
using boost::test_tools::output_test_stream;

//____________________________________________________________________________//

BOOST_AUTO_TEST_CASE( test )
{
    output_test_stream output;
    int i=2;
    output << "i=" << i;
    BOOST_CHECK( !output.is_empty( false ) );
    BOOST_CHECK( output.check_length( 3, false ) );
    BOOST_CHECK( output.is_equal( "i=3" ) );
}

//____________________________________________________________________________//
Source code | Show output
> example
Running 1 test case...
test.cpp(15): error in "test": check output.is_equal( "i=3" ) failed. Output content: "i=2"

*** 1 failure detected in test suite "example"

Example 35. output_test_stream usage for pattern file matching

Even simpler: no need to generate expected patterns. Though you need to keep the pattern file all the time somewhere around. Your testing will look like a serious of output operators followed by match pattern checks repeated several times. Try to perform checks as frequently as possible, because it allows you to more closely identify possible source of failure. Content of the pattern file is:
更簡單的:不需要生成期望模式。雖然你需要一直保存你的模式文件。 你的測試看起來就是一些輸出操作,跟著若干個重複的匹配模式檢查。 盡可能頻繁地執行檢查,因為這讓你更接近於錯誤的源。模式文件內容是:

i=2
File: test.cpp Line: 14

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>
#include <boost/test/output_test_stream.hpp> 
using boost::test_tools::output_test_stream;

//____________________________________________________________________________//

BOOST_AUTO_TEST_CASE( test )
{
    output_test_stream output( "pattern_file", true );
    int i=2;
    output << "i=" << i;
    BOOST_CHECK( output.match_pattern() );

    output << "\nFile: " << __FILE__ << " Line: " << __LINE__;
    BOOST_CHECK( output.match_pattern() );
}

//____________________________________________________________________________//
Source code | Show output
> example
Running 1 test case...
test.cpp(16): error in "test": check output.match_pattern() failed. Mismatch at position 23
...5...
...4...

*** 1 failure detected in test suite "example"


PrevUpHomeNext