Test Double Sample: Difference between revisions

From STRIDE Wiki
Jump to navigation Jump to search
 
(28 intermediate revisions by 5 users not shown)
Line 1: Line 1:
==Introduction==
==Introduction==
The Function Double Samples are part of the [[Test_Unit_Samples|STRIDE Test Unit Samples]]. The following content relates to the sample files and workspaces installed in '''<tt>%STRIDE_DIR%\Samples\TestUnits\FunctionDouble</tt>'''.  This sample consists of a [http://en.wikipedia.org/wiki/Microsoft_Visual_Studio Visual Studio] workspace for building a [[Windows_Off-Target_Apps|Windows Off-Target App]], sample source code (C and C++ implementations),  and a STRIDE workspace for doing more advanced test execution.


==Getting Started==
This example using a '''C++ test class''' cover the application of test doubles. Please read the [[Using Test Doubles]] article before proceeding in order to understand the concepts. You may also find it helpful to review the information available [http://xunitpatterns.com/Mocks,%20Fakes,%20Stubs%20and%20Dummies.html here] for more details on test double terminology.


To begin, open the Visual Studio Solution file in the sample directory.  This solution (and corresponding project) were created for Visual Studio 2005.  If you have a later version of Visual Studio installed, you should be able to open this solution and it will be automatically upgraded if necessary.  If you do not currently have any version of Visual Studio, it is recommended that you install the current free version of [http://en.wikipedia.org/wiki/Visual_Studio_Express Visual Studio Express].
==Tests Description==


Once you have successfully opened the solution, rebuild it.  The build process has custom STRIDE build rules integrated and will produce a STRIDE database, intercept module source files, and a Windows Off-Target App that incorporates the test class source.
===TestFunction===


Once the build is complete, perform the following steps to run the test classes in the workspace:
This case validates <tt>test_func1</tt> function (implemented in <tt>s2_testdouble_function.h/c</tt>) which in turn depends on <tt>depend1</tt> function (in <tt>s2_testdouble_depend.h/c</tt>). Since the caller (<tt>test_func1</tt>) and the callee (<tt>depend1</tt>) are in separate compilation units the depencency is captured for interception (via [[Using Test Doubles#Configuring_the_Double_Using_SCL|scl_function]] pragma in <tt>s2_testdouble_function_test.h/c</tt>) using ''"DEFINITION"'' and ''"IMPLICIT"'' options.


# launch the Windows Off-Target App, FunctionDouble.exe.  This will run in a standard console window.
;CallNoDoubling
# open a command prompt window and change to this sample's directory.
: Simple case to demonstrate operation where no doubling is used. The true <tt>depend1</tt> is used when testing <tt>test_func1</tt>.  
# at the command prompt, run the command <tt>'''TestUnitRun.pl -v'''</tt>.  This will execute all of the test units in the workspace and open a browser to display the results.
# quit the FunctionDouble.exe application by typing 'q' in its console window.


==Function Double Sample==
;CallWithFake
: Dynamically replaces dependency <tt>depend1</tt> with a fake <tt>fake_depend_1</tt> function, which simply returns a constant.


Now that you have built the Windows Off-Target App and executed the test classes it contains, you can take time to peruse the function double source and the corresponding results that each produces.  This section provides a brief description for each.
;CallWithMock
: Dynamically replaces dependency <tt>depend1</tt> with a mock <tt>mock_depend_1</tt> function, which validates the passed in parameter and proceeds as the original <tt>depend1</tt>.


===DefinitionContext Examples===
===TestFunctionWithDepend===
This case, similar to [[#Basic::TestFunction|Basic::TestFunction]], validates <tt>test_func2</tt> function which in turn depends on <tt>depend2</tt> function. It differs in that the implementation is done in the same source file (both implemented in <tt>s2_testdouble_function_with_depend.h/c</tt>). Since the caller (<tt>test_func2</tt>) and the callee (<tt>depend2</tt>) are in the same compilation unit the depencency is captured for interception (via [[Using Test Doubles#Configuring_the_Double_Using_SCL|scl_function]] pragma in <tt>s2_testdouble_function_with_depend_test.h</tt>) using ''"REFERENCE"'' and ''"EXPLICIT"'' options.


====01_01_Cpp_Test_Function====
;CallNoDoubling
: Simple case to demonstrate operation where no doubling is used. The true <tt>depend2</tt> is used when testing <tt>test_func2</tt>.


This example demonstrates uses of the [[Scl_function|scl_function]] pragma to qualify a captured function as a intercept candidate, where the intercept '''context''' is at the function definition, and the [[Name_Mangling#Name_Mangling_Conflicts|mangling type is explicit]].
;CallWithFake
: Dynamically replaces dependency <tt>depend2</tt> with a fake <tt>fake_depend_2</tt> function, which simply returns a constant.  


====01_02_Cpp_Test_Function_With_Depend====
;CallWithMock
 
: Dynamically replaces dependency <tt>depend2</tt> with a mock <tt>mock_depend_2</tt> function, which validates the passed in parameter and proceeds as the original <tt>depend2</tt>.
====02_01_C_Test_Function====
 
====02_02_C_Test_Function_With_Depend====
 
===Command Line Execution===
 
Command line execution for the function double sample is done using the [[Test_Runners#TestUnitRun.pl|TestUnitRun utility]].  Here are several examples of specific syntax to execute the sample.  All of these commands can be invoked from a standard [http://en.wikipedia.org/wiki/Command_Prompt_(Windows) command shell] (or other shell of your choosing) and the arguments shown assume that the commands are executed with the sample's directory as the starting directory. You must have your FunctionDouble.exe application running in order for the runner to be able to initiate a connection to the target simulator. In addition, you should verify that your %STRIDE_DIR%\bin\transport.cfg file is using the TCP transport to connect to port 8000 (these are the default settings when the product is installed).
 
====Simple execution of all test units====
 
The following command executes all of the test units found in the STRIDE database you have previously generated.  For the purpose of this sample, since there is only one database, the -d parameter is not strictly needed, but it is shown here for completeness.
 
  TestUnitRun.pl -d FunctionDouble.sidb
 
This command executes all Test Units found in the database in descending alpha-numeric sort order.  Any Test Unit initialization arguments are given default values (typically zero or NULL).
 
When you run this command, you should see console output like:
 
  Attempting connection using [Sockets (S2)] transport ...
  Connected to device.
  Initializing STRIDE database objects...
  Done.
  Running Test DefinitionContext::ExplicitMangling...
  Disconnected from device.
  Test Results saved to C:\STRIDE\Samples\TestUnits\FunctionDouble\FunctionDouble.xml
  Test Report saved to C:\STRIDE\Samples\TestUnits\FunctionDouble\FunctionDouble.html
  ***************************************************************************
  Results Summary
  ***************************************************************************
    Passed:              13
    Failed:              15
    In Progress:          0
    Not Applicable:      0
    ...in 13 suites.
  ***************************************************************************
 
===Workspace-based Execution===
 
FunctionDouble.ssw, a workspace in the FunctionDouble directory, demonstrates the use of script execution with Studio to manage test order and hierarchy.  This workspace was created using [[WorkspaceSetup.pl]].  The setup and teardown folders provide basic infrastructure scripts that start/stop the simulator application (FunctionDouble.exe) and to manage traceviews used for [[Runtime_Reference#Logging_Services|srPrint]] message collection.  The scripts that drive the testing are in the workspace '''test''' folder. What follows is a brief description for each.
 
====RunAll====
 
This folder contains a script, All.js, that iterates through the entire collection of test units and executes them one at a time. The order of execution will be in ascending alphabetical order (by name) since the [[AutoScript#ascript.TestUnits|ArrangeBy]] collection method was called.
 
====Run Individual====
 
This folder shows how to use individual scripts to execute test classes. Each script has the following form:
 
  ascript.TestUnits.Item('''TEST_UNIT_NAME''').Run();
 
The '''TEST_UNIT_NAME''' is the name of the test unit to be run.  The order and hierarchy of each script may be changed via the Studio tree control by moving the script within the '''Run Individual''' folder.  


[[Category: Samples]]
[[Category: Samples]]

Latest revision as of 23:50, 6 June 2011

Introduction

This example using a C++ test class cover the application of test doubles. Please read the Using Test Doubles article before proceeding in order to understand the concepts. You may also find it helpful to review the information available here for more details on test double terminology.

Tests Description

TestFunction

This case validates test_func1 function (implemented in s2_testdouble_function.h/c) which in turn depends on depend1 function (in s2_testdouble_depend.h/c). Since the caller (test_func1) and the callee (depend1) are in separate compilation units the depencency is captured for interception (via scl_function pragma in s2_testdouble_function_test.h/c) using "DEFINITION" and "IMPLICIT" options.

CallNoDoubling
Simple case to demonstrate operation where no doubling is used. The true depend1 is used when testing test_func1.
CallWithFake
Dynamically replaces dependency depend1 with a fake fake_depend_1 function, which simply returns a constant.
CallWithMock
Dynamically replaces dependency depend1 with a mock mock_depend_1 function, which validates the passed in parameter and proceeds as the original depend1.

TestFunctionWithDepend

This case, similar to Basic::TestFunction, validates test_func2 function which in turn depends on depend2 function. It differs in that the implementation is done in the same source file (both implemented in s2_testdouble_function_with_depend.h/c). Since the caller (test_func2) and the callee (depend2) are in the same compilation unit the depencency is captured for interception (via scl_function pragma in s2_testdouble_function_with_depend_test.h) using "REFERENCE" and "EXPLICIT" options.

CallNoDoubling
Simple case to demonstrate operation where no doubling is used. The true depend2 is used when testing test_func2.
CallWithFake
Dynamically replaces dependency depend2 with a fake fake_depend_2 function, which simply returns a constant.
CallWithMock
Dynamically replaces dependency depend2 with a mock mock_depend_2 function, which validates the passed in parameter and proceeds as the original depend2.