Test Double Sample: Difference between revisions

From STRIDE Wiki
Jump to navigation Jump to search
 
(10 intermediate revisions by 3 users not shown)
Line 1: Line 1:
==Introduction==
==Introduction==


Please read the [[Using Test Doubles]] article before proceeding in order to understand the concepts. You may also find it helpful to  
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.
review the information available [http://xunitpatterns.com/Mocks,%20Fakes,%20Stubs%20and%20Dummies.html here] for more details on test double terminology.


This example presents two different function double settings that differ in the point of interception (at DEFINITION vs. at REFERENCE).
==Tests Description==


==Tests Description==
===TestFunction===
 
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.
 
;CallNoDoubling
: Simple case to demonstrate operation where no doubling is used. The true <tt>depend1</tt> is used when testing <tt>test_func1</tt>.
 
;CallWithFake
: Dynamically replaces dependency <tt>depend1</tt> with a fake <tt>fake_depend_1</tt> function, which simply returns a constant.


=== Basic ===
;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>.


====Basic::TestFunction====
===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.


This example uses a '''C++ test class''' to validate the '''test_func1''' function under test. The dependency '''depend1''' is left in place during the '''CallNoDoubling''' method. The dependency is replaced with a fake method, which simply returns a constant in the '''CallWithFake''' method. The dependency is replaced with a mock method, which validates the passed in parameter, in the '''CallWithMock''' method.<br>
;CallNoDoubling
The intercept mangling parameters in this example, in the [[Function_Double#Configuring_the_Double_Using_SCL|scl_function pragma]], uses the '''"DEFINITION"''' option to indicate the the intercept will be at the function definition, and the'''"IMPLICIT"''' option because no mangling is required to intercept (because the caller and callee are in separate compilation units).
: Simple case to demonstrate operation where no doubling is used. The true <tt>depend2</tt> is used when testing <tt>test_func2</tt>.  


====Basic::TestFunctionWithDepend====
;CallWithFake
This example uses a '''C++ test class''' to validate the '''test_func2''' function under test. The dependency '''depend2''' is left in place during the '''CallNoDoubling''' method. The dependency is replaced with a fake method, which simply returns a constant in the '''CallWithFake''' method. The dependency is replaced with a mock method, which validates the passed in parameter, in the '''CallWithMock''' method.<br>
: Dynamically replaces dependency <tt>depend2</tt> with a fake <tt>fake_depend_2</tt> function, which simply returns a constant.  
The intercept mangling parameters in this example, in the [[Function_Double#Configuring_the_Double_Using_SCL|scl_function pragma]], uses the '''"REFERENCE"''' option to indicate the the intercept will be at the function call, and the'''"EXPLICIT"''' option because mangling is required to intercept (because the caller and callee reside in the same source file).


;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>.


[[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.