2
0
mirror of https://github.com/boostorg/test.git synced 2026-01-26 19:12:10 +00:00
Files
test/doc/components/test_tools/output_test_stream.html
Gennadiy Rozental 8ee41f3ba4 New Version
[SVN r18711]
2003-06-09 08:07:03 +00:00

96 lines
6.4 KiB
HTML

<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> > <A href="../index.html">Components</A>
> <A href="index.html">The Test Tools</A> > <SPAN class="current_article">Output operations testing</SPAN>
</DIV>
<DIV class="body"> <IMG src='../../btl.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 use 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_toolbox::output_test_stream;
<SPAN class="cpp-type">int</SPAN> test_main( <SPAN class="cpp-type">int</SPAN>, <SPAN class="cpp-type">char</SPAN>* [] ) {
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> ) );
<SPAN class="reserv-word">return</SPAN> <SPAN class="literal">0</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_toolbox::output_test_stream;
<SPAN class="cpp-type">int</SPAN> test_main( <SPAN class="cpp-type">int</SPAN> argc, <SPAN class="cpp-type">char</SPAN>* [] ) {
output_test_stream output( <SPAN class="literal">&quot;pattern_file&quot;, </SPAN>argc == <SPAN class="literal">1</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() );
...
<SPAN class="reserv-word">return</SPAN> <SPAN class="literal">0</SPAN>;
}</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</A> programs</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>Copyright &copy <A href='mailto:rogeeff@emailaccount.com'>Gennadiy Rozental</A> 2001-2003.<BR>
Permission to copy, use, modify, sell and distribute this document is granted provided this copyright
notice appears in all copies. This document is provided "as is" without express or implied warranty
and with no claim as to its suitability for any purpose.</P>
<P>Revised: 9 June, 2003</P>
</DIV>
</DIV>
</BODY>
</HTML>