File: output_test_stream.html

package info (click to toggle)
boost 1.34.1-14
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 116,412 kB
  • ctags: 259,566
  • sloc: cpp: 642,395; xml: 56,450; python: 17,612; ansic: 14,520; sh: 2,265; yacc: 858; perl: 481; makefile: 478; lex: 94; sql: 74; csh: 6
file content (92 lines) | stat: -rw-r--r-- 6,441 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE>The Test Tools</TITLE>
<LINK rel="stylesheet" type="text/css" href="../../style/btl.css" media="screen">
<LINK rel="stylesheet" type="text/css" href="../../style/btl-print.css" media="print">
<META http-equiv="Content-Language" content="en-us">
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</HEAD>
<BODY>
<DIV class="header"> <A href="../../index.html">Boost.Test</A> &gt; <A href="../index.html">Components</A> 
  &gt; <A href="index.html">The Test Tools</A> &gt; <SPAN class="current_article">Output operations testing</SPAN> 
</DIV>
<DIV class="body"> <IMG src="../../btl1.gif" width="252" height="43" alt="Boost Test logo"> 
  <H1 class="subtitle">Output operations testing</H1>
  <P class="page-toc"> <A href="#Introduction">Introduction</A><BR>
    <A href="#Usage">Usage<BR>
    </A> <A href="#Tests">Example and test programs</A></P>
  <H2><A name="Introduction">Introduction</A></H2>
  <P class="first-line-indented">How do you perform correctness test for operator&lt;&lt;( std::ostream&amp;, 
    ... ) operations. You may print into the standard output stream and manually check that everything 
    is as you expected. But this is not really fit for regression testing. You may use stringstream and 
    compare resulting output buffer with the expected pattern string. But you are required to perform 
    several extra operations with every check you do, so you it becomes tedious really soon. The class 
    <SPAN class="new-term">output_test_stream</SPAN> automate this task for you. This is a simple, but 
    powerful tool for testing standard std::ostream based output operation. The class output_test_stream 
    comply to std::ostream interface so it can be used in place of any std::ostream parameter. It provides 
    several methods to validate output content - you may compare with expected pattern string or perform 
    only length check. Flushing, synchronizing, string comparison is automated for you by this tool</P>
  <P class="first-line-indented">In some cases it may not be good enough. It becomes even more obvious 
    when it's difficult to generate the expected pattern. What we need is to be able to check once manually 
    that the output is as expected and to be able in a future check that is stays the same. To help manage 
    this logic the class output_test_stream also allows to match/save output content versus/into specified 
    file.</P>
  <P class="first-line-indented">For detailed description of the facilities provided by the output_test_stream 
    see it's <A href="output_test_stream_spec.html">specification</A>.</P>
  <H2><A name="Usage">Usage</A></H2>
  <P class="first-line-indented">There are two ways to employ the output_test_stream tool: explicit output 
    checks and pattern file matching.</P>
  <H3 class="first-line-indented">Explicit output checks</H3>
  <PRE class="code"><SPAN class="reserv-word">using</SPAN> boost::test_tools::output_test_stream;
    
BOOST_AUTO_TEST_CASE( test )
{
    output_test_stream output;
    <SPAN class="cpp-type">int</SPAN> i=<SPAN class="literal">2</SPAN>;
    output &lt;&lt; <SPAN class="literal">&quot;i=&quot;</SPAN> &lt;&lt; i;
    BOOST_CHECK( !output.is_empty(<SPAN class="reserv-word"> false</SPAN> ) );
    BOOST_CHECK( output.check_length(<SPAN class="reserv-word"> </SPAN><SPAN class="literal">3</SPAN>, <SPAN class="reserv-word">false</SPAN> ) );
    BOOST_CHECK( output.is_equal( <SPAN class="literal">&quot;i=2&quot;</SPAN> ) );
}</PRE>
  <P class="first-line-indented">As simple as that: just use instance of output_test_stream as an output 
    stream and then check output content using supplied tool's methods. Note use of <I>false</I> to prevent 
    output flashing 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 testing will look like a serious 
    of output operators followed by check one. And so on again. Try to perform checks as frequently as 
    possible. It's not only simplify patters you compare with, but also allows you to more closely identify 
    possible source of failure.</P>
  <H3 class="first-line-indented">Pattern file matching</H3>
  <PRE class="code"><SPAN class="reserv-word">using</SPAN> boost::test_tools::output_test_stream;
    
BOOST_AUTO_TEST_CASE( test )
{
    output_test_stream output( <SPAN class="literal">&quot;pattern_file&quot;</SPAN>, <SPAN class="reserv-word">true</SPAN> );
    <SPAN class="cpp-type">int</SPAN> i=<SPAN class="literal">2</SPAN>;
    output &lt;&lt; <SPAN class="literal">&quot;i=&quot;</SPAN> &lt;&lt; i;
    BOOST_CHECK( output.match_pattern() );

    output &lt;&lt; <SPAN class="literal">&quot;File: &quot;</SPAN> &lt;&lt; __FILE__ &lt;&lt; <SPAN class="literal">&quot; Line: &quot;</SPAN> &lt;&lt; __LINE__;
    BOOST_CHECK( output.match_pattern() );
}</PRE>
  <P class="first-line-indented">Even more 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. And so on again. Try to perform checks as frequently 
    as possible,cause it allows you to more closely identify possible source of failure.</P>    
  <H2><A name="Tests">Example and test programs</A></H2>
  <P class="indented"> <A href="../../tests/output_test_stream_test.html">output_test_stream_test</A><BR>
    <A href="../../tests/result_report_test.html">result_report_test</A><BR>
    <A href="../../tests/result_report_test.html">test_tools_test</A><BR>
    <A href="../../tests/errors_handling_test.html">errors_handling_test</A></P>
</DIV>
<DIV class="footer"> 
  <DIV class="footer-body"> 
    <P> &copy; <A name="Copyright">Copyright</A> <A href="mailto:boost-test%20at%20emailaccount%20dot%20com%20%28please%20unobscure%29">Gennadiy Rozental</A> 2001-2006. <BR>
      Distributed under the Boost Software License, Version 1.0.
      (See accompanying file <A href="../../../../../LICENSE_1_0.txt">LICENSE_1_0.txt</A> or copy at 
      <A href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</A>)</P>
        <P>Revised:        <!-- #BeginDate format:Sw1 -->18 February, 2006<!-- #EndDate -->     </P>
  </DIV>
</DIV>
</BODY>
</HTML>