testdev/ite/src/com.nokia.testfw.sut.help/html/references/write_a_unit_test_using_sut.htm
author Johnson Ma <johnson.ma@nokia.com>
Tue, 30 Mar 2010 14:39:29 +0800
changeset 1 96906a986c3b
permissions -rw-r--r--
contribute ITE to symbian foundation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
     1
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
     2
<html>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
     3
<head>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
     4
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
     5
<meta http-equiv="Content-Style-Type" content="text/css" />
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
     6
<title>Write a unit test using SUT</title>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
     7
<link rel="StyleSheet" href="../../book.css" type="text/css"/>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
     8
</head>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
     9
<body>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    10
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    11
<h2>Write a unit test using SUT</h2>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    12
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    13
<p>The following code snippet is from the example Racecar, we can download it from<br />
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    14
<a href="https://developer.symbian.org/sfl/MCL/sftools/ana/testfw/testexecfw/symbianunittestfw/sutfw/sutfwexamples">https://developer.symbian.org/sfl/MCL/sftools/ana/testfw/testexecfw/symbianunittestfw/sutfw/sutfwexamples</a>.</p>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    15
<div><span>1.<span>&nbsp;</span></span>Select Class and methods to be tested</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    16
<div>For instance, we want to test the class CRaceCar in the example. Two of the functions we want to test are AddFuel() and SpeedL().</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    17
<div>&nbsp;</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    18
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    19
<div><code>#ifndef CRACECAR_H<br />
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    20
#define CRACECAR_H<br />
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    21
<br />
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    22
class CRaceCar : public CBase <br />
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    23
&nbsp;&nbsp;&nbsp; {<br />
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    24
<span>&nbsp;&nbsp;&nbsp; // some constructors and destructor&hellip;<br />
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    25
<br />
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    26
&nbsp;&nbsp;&nbsp; public: // New functions<br />
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    27
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TUint AddFuel( TUint aFuel );<br />
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    28
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TUint SpeedL();<br />
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    29
<br />
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    30
&nbsp;&nbsp;&nbsp; // some other methods and attributes&hellip;<br />
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    31
&nbsp;&nbsp;&nbsp; };<br />
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    32
<br />
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    33
#endif // CRACECAR_H</span></code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    34
<br />
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    35
<div>In order to get enough privillage to access private members in CRaceCar when creating unit test case, we need to define the test class UT_CRaceCar as friend class</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    36
<div><code>class CRaceCar : public CBase</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    37
<div>&nbsp;&nbsp;&nbsp; <code>{</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    38
<div>&nbsp;&nbsp;&nbsp; <code>// ...</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    39
<div>&nbsp;&nbsp;&nbsp;</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    40
<div>&nbsp;&nbsp;&nbsp; <code>private: // Test class</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    41
<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <code>#ifdef SYMBIAN_UNIT_TEST</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    42
<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <code>friend UT_CRaceCar;</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    43
<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <code>#endif&nbsp;// SYMBIAN_UNIT_TEST</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    44
<div>&nbsp;&nbsp;&nbsp; <code>};</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    45
<div>&nbsp;&nbsp;&nbsp;</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    46
<div>UT_CRaceCar will be introduced to CRaceCar as friend class if the macro SYMBIAN_UNIT_TEST has been defined in test project.</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    47
<div>&nbsp;&nbsp;&nbsp;</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    48
<div><span>2.<span>&nbsp;</span></span>Define Test Class</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    49
<div>Define a test class UT_CRaceCar, it will derive from CSymbianUnitTest,</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    50
<div>&nbsp;</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    51
<div><code>#ifndef UT_RACECAR_H</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    52
<div><code>#define UT_RACECAR_H</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    53
<div><code>&nbsp;</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    54
<div><code>// INCLUDES</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    55
<div><code>#include &lt;symbianunittest.h&gt;</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    56
<div><code>&nbsp;</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    57
<div><code>// FORWARD DECLARATIONS</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    58
<div><code>class CRaceCar;</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    59
<div><code>&nbsp;</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    60
<div><code>// CLASS DECLARATION</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    61
<div><code>class UT_CRaceCar : public CSymbianUnitTest</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    62
<div><code>&nbsp;&nbsp;&nbsp; {</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    63
<div><code>&nbsp;&nbsp;&nbsp; // Constructors and destructor&hellip;</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    64
<div><code>&nbsp;&nbsp;&nbsp;&nbsp;</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    65
<div><code>&nbsp;&nbsp;&nbsp; protected: // From CSymbianUnitTest</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    66
<div><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void SetupL();&nbsp;<br />
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    67
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void Teardown();&nbsp;</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    68
<div><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    69
<div><code>&nbsp;&nbsp;&nbsp; protected: // Test functions, others are the same as this one.</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    70
<div><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void UT_AddFuel();&nbsp;</code><br />
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    71
<span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void UT_SpeedL();</code> </span></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    72
<div><code>&nbsp;</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    73
<div><code>&nbsp;&nbsp;&nbsp; private: // Data</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    74
<div><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // The object to be tested as a member variable:</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    75
<div><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;CRaceCar *iRaceCar;&nbsp;</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    76
<div><code>&nbsp;&nbsp;&nbsp; };</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    77
<div><code>&nbsp;</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    78
<div><code>#endif // UT_RACECAR_H</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    79
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    80
<div>&nbsp;</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    81
<div>Please notice some Symbian C++ essential functions such as NewL() and ConstructL() are ignored in this document.</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    82
<div>Users can provide optional SetupL() and Teardown() functions. They will be called before and after test case execution. SetupL() usually initializes some of the resources, allocates memory, Teardown() usually in charge of some resource destruction so that the test object can reset its state for the next test case execution.</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    83
<div>UT_AddFuel() and UT_SpeedL() are the unit test&nbsp;code to test the corresponding functions defined in the class&nbsp;CRaceCar.</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    84
<div>The iRaceCar is the instance of class under test.</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    85
<div>&nbsp;</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    86
<div><span>3.<span>&nbsp;</span></span>Create Test DLL entry.</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    87
<div>The next file that we should take into consideration is dllentry.cpp which you can find in the example test project dllentry.cpp.</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    88
<div>It only has one global function with the signature EXPORT_C MSymbianUnitTestInterface* CreateTestL(); in this function, it creates and return the test suites, so the SymbianUnitTest framework can load and run test cases.&nbsp;</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    89
<div>&nbsp;</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    90
<div>The example code snippet is shown below:</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    91
<div><code>EXPORT_C MSymbianUnitTestInterface* CreateTestL()</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    92
<div><code>&nbsp;&nbsp;&nbsp; {</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    93
<div><code>&nbsp;&nbsp;&nbsp; CSymbianUnitTestSuite* testSuite =</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    94
<div><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;CSymbianUnitTestSuite::NewLC( _L(&quot;ut_racecar&quot;) );</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    95
<div><code>&nbsp;</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    96
<div><code>&nbsp;&nbsp;&nbsp; testSuite-&gt;AddL( UT_CRaceCar::NewLC() );</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    97
<div><code>&nbsp;&nbsp;&nbsp; CleanupStack::Pop();</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    98
<div><code>&nbsp;</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
    99
<div><code>&nbsp;&nbsp;&nbsp; // Add more tests to the test suite here when testing multiple classes</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   100
<div><code>&nbsp;&nbsp;&nbsp; CleanupStack::Pop( testSuite );</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   101
<div><code>&nbsp;&nbsp;&nbsp; return testSuite;</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   102
<div><code>&nbsp;&nbsp;&nbsp; }</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   103
<div><code>&nbsp;&nbsp;&nbsp;</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   104
<div><span>4.<span>&nbsp;</span></span>Implement test cases.</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   105
<div>One of the important functions should taken into account is the second phase constructor of the test suite. In this function, we must call the macro BASE_CONSTRUCT at the first line. Then the unit test methods can be added by using the macro ADD_SUT. Please note that SetupL() will be invoked before each test case runs and the Teardown() will be invoked after the test case execution.</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   106
<div>We can specify the SetupL() and Teardown() functions for certain test case simply by using the macro ADD_SUTWITH_SETUP_AND_TEARDOWN.</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   107
<div>&nbsp;</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   108
<div>The example code snippet is shown below:</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   109
<div><code>void UT_CRaceCar::ConstructL()</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   110
<div><code>&nbsp;&nbsp;&nbsp; {</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   111
<div><code>&nbsp;&nbsp;&nbsp; BASE_CONSTRUCT</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   112
<div><code>&nbsp;&nbsp;&nbsp; ADD_SUT( UT_AddFuel )</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   113
<div><code>&nbsp;&nbsp;&nbsp; ADD_SUT_WITH_SETUP_AND_TEARDOWN( SetupL, UT_ SpeedL, Teardown )</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   114
<div><code>&nbsp;&nbsp;&nbsp; }</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   115
<div><code>&nbsp;&nbsp;&nbsp;</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   116
<div>Here is an example a a unit test function.</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   117
<div>&nbsp;</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   118
<div><code>void UT_CRaceCar::UT_FuelL()</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   119
<div><code>&nbsp;&nbsp;&nbsp; {</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   120
<div><code>&nbsp;&nbsp;&nbsp; SUT_ASSERT_EQUALS( 0, iRaceCar-&gt;FuelLeft() )</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   121
<div><code>&nbsp;&nbsp;&nbsp; SUT_ASSERT_EQUALS( 0, iRaceCar-&gt;AddFuel( KInitialFuel ) )</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   122
<div><code>&nbsp;&nbsp;&nbsp; SUT_ASSERT_EQUALS( KInitialFuel, iRaceCar-&gt;FuelLeft() )</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   123
<div><code>&nbsp;&nbsp;&nbsp; SUT_ASSERT_EQUALS( 2, iRaceCar-&gt;AddFuel( 1 ) )&nbsp;</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   124
<div><code>&nbsp;&nbsp;&nbsp; SUT_ASSERT_EQUALS( KInitialFuel, iRaceCar-&gt;FuelLeft() )</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   125
<div><code>&nbsp;&nbsp;&nbsp; }</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   126
<div><code>&nbsp;&nbsp;&nbsp;</code></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   127
<div>In this function, we use the macro SUT_ASSERT_EQUALS to assert if the test function's return value is&nbsp;what we expect, more macros will be introduced in later section. It will be marked as failed if the actual return value doesn't match the expected value when using the macro SUT_ASSERT_EQUALS. The rest of the functions should be tested follow the same way as we mentioned in this section.</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   128
<div>&nbsp;</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   129
<div><span>5.<span>&nbsp;</span></span>Create project mmp file.</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   130
<div>In the next step, we will create a project file. Showing below is a snippet of the mmp file:</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   131
<div>1) &nbsp;TARGET&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ut_racecar.dll</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   132
<div>2)&nbsp; TARGETTYPE&nbsp;&nbsp;&nbsp; dll</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   133
<div>3) &nbsp;UID<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0x20022E76 0x0E6A3C34</span></div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   134
<div>4)&nbsp; MACRO&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SYMBIAN_UNIT_TEST</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   135
<div>5) &nbsp;SOURCE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; racecar.cpp</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   136
<div>6)&nbsp; SOURCE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dllEntry.cpp</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   137
<div>7) &nbsp;SOURCE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ut_racecar.cpp</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   138
<div>8)&nbsp; SYSTEMINCLUDE /epoc32/include/symbianunittest</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   139
<div>9) &nbsp;LIBRARY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; symbianunittestfw.lib</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   140
<div>&nbsp;</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   141
<div>The type of the test project is a dll project, so we should indicate the target type of the project, like line 2, we specify dll as the TARGETTYPE.</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   142
<div>At line 1, we set a target file name for the file.</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   143
<div>At line 3, we must specify the UID2 as it is identified for the SymbianUnitTest&rsquo;s DLL.</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   144
<div>At line 4, we must add the SYMBIAN_UNIT_TEST macro to enable the macros the test framework provided.</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   145
<div>We also need to list the source code under test (in this case, the source file of the testing class is racecar.cpp), so we add&nbsp;it at line 5.</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   146
<div>We added dllEntry.cpp to the mmp file as stated in section &quot;3 -- Write dll entry&quot; at line 6.</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   147
<div>ut_racecar.cpp is test case implementation, we add it at line 7.</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   148
<div>Line 8 and line 9 contain the directory of the header files and the library file of the SymbianUnitTest &nbsp;we need in our test.</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   149
<div>&nbsp;</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   150
<div><span>6.<span>&nbsp;</span></span>Create bld.inf for the test project.</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   151
<div>We also need to add the newly created project mmp file to bld.inf, just like the following shown below:</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   152
<div>PRJ_TESTMMPFILES</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   153
<div>ut_racecar.mmp</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   154
<div>&nbsp;</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   155
<div><span>7.<span>&nbsp;</span></span>Build project and run it.</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   156
<div>In the test project group directory, execute the following command to build it:</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   157
<div>bldmake bldfiles</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   158
<div>abld test clean winscw</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   159
<div>abld test reallyclean winscw</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   160
<div>abld test build winscw udeb</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   161
<div>&nbsp;</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   162
<div>Run&nbsp;&quot;symbianunittest.exe -t=ut_racecar.dll&quot; to&nbsp;execute the test, we should get the test result at &quot;c:\sut&quot;.</div>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   163
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   164
</body>
96906a986c3b contribute ITE to symbian foundation
Johnson Ma <johnson.ma@nokia.com>
parents:
diff changeset
   165
</html>