Training Doubling: Difference between revisions

From STRIDE Wiki
Jump to navigation Jump to search
No edit summary
 
(12 intermediate revisions by 4 users not shown)
Line 1: Line 1:
= Objectives =
== Objectives ==


This Training Module is focused on explaining how to leverage '''Test Doubles''' in the context of executing a test. For an overview of '''intercepting''' existing functions please refer to [[Using_Test_Doubles | Test Doubles]]. The module covers following topics:
This Training Module is focused on explaining how to leverage '''Test Doubles''' in the context of executing a test. For an overview of '''intercepting''' existing functions please refer to [[Using_Test_Doubles | Test Doubles]]. The module covers following topics:
Line 17: Line 17:




= Instructions =
== Instructions ==


== Build and Run TestApp ==
=== Build and Run TestApp ===


* Build TestApp using SDK makefile
* [[Building_an_Off-Target_Test_App#Build_Steps | Build TestApp]] using SDK makefile
* Startup TestApp
* Startup TestApp
* If not already done, create an [[Stride_Runner#Options | option file]] (myoptions.txt) using the following content (Windows example)
* If you have not created an option file, please refer to [[Training_Getting_Started#Run_Training_Tests| setup]]  
 
  ##### Command Line Options ######
  --device "TCP:localhost:8000"
  --database %STRIDE_DIR%\SDK\Windows\out\TestApp.sidb
  --output %STRIDE_DIR%\SDK\Windows\sample_src\TestApp.xml
  --log_level all


* Execute ''Test Double'' Test Units only  
* Execute ''Test Double'' Test Units only  


   > stride -O myoptions.txt --run TestDouble_Reference TestDouble_Definition
   > stride --options_file myoptions.txt --run TestDouble_Reference --run TestDouble_Definition


   Loading database...
   Loading database...
Line 49: Line 43:




* Review the details of the test results using a Browser. Open '''TestApp.xml''' which can be found in the ''sample_src'' directory (based on the ''output'' option). By opening the xml file in a web browser the xsl is automatically applied to create html.
* Review the details of the test results using a Browser. Open [[Building_an_Off-Target_Test_App#Interpreting_Results | TestApp.xml ]] which can be found in the ''sample_src'' directory (based on the ''output'' option). By opening the xml file in a web browser the xsl is automatically applied to create html.


== Implement Exercise ==
=== Implement Exercise ===


* '''TestDouble_Reference::Exercise'''
* '''TestDouble_Reference::Exercise'''
** Implement a new ''test double'':
** Implement a new ''test double'' for the ''strlen()'' routine:
*** Add a ''NOTE'' capturing its name when it is called
*** Add a ''NOTE'' capturing its name when it is called
*** Uses the real ''strlen()'' to return the length of the passed in string
*** Uses the real ''strlen()'' to return the length of the passed in string
** ''Hint:'' You will probably want to read about [[Using Test Doubles]]
** Use ''srEXPECT_EQ()'' to validate that ''sut_strcheck()'' returns correct length  
** Use ''srEXPECT_EQ()'' to validate that ''sut_strcheck()'' returns correct length  
** Make sure to restore the original routine  
** Make sure to restore the original routine  


* '''TestDouble_Definition::Exercise'''
* '''TestDouble_Definition::Exercise'''
** Implement a new ''test double'':
** Implement a new ''test double'' for the ''sut_strcpy()'' routine:
*** Log its name when it is called
*** Log its name when it is called
*** Validate string passed to ''sut_strsave()'' is received correctly by the ''test double''
*** Validate string passed to ''sut_strsave()'' is received correctly by the ''test double''
*** Call the original function with a '''DIFFERENT STRING''' (i.e "Exercise Test String 2")
*** Remember that ''sut_strsave()'' calls ''sut_strcpy()'' with the string passed to it
*** Hint - ''Reset the original function within the mock''
*** Can use a test macro to validate the string is correctly passed to the double
 
*** Call the original function (''sut_strcpy()'') with a '''DIFFERENT STRING''' (i.e "Exercise Test String 2")
**** Call the original within the test double
**** Make sure to reset the original function within the mock before calling it (i.e. srDOUBLE_RESET)
** Call ''sut_strsave()'' with a string (i.e. "Exercise Test String 1")
** Call ''sut_strsave()'' with a string (i.e. "Exercise Test String 1")
** Use ''sut_strget()'' to retrieve a string
** Use ''sut_strget()'' to retrieve a string
** Compare retrieved string with the '''DIFFERENT STRING''' inserted by the ''test double''
** Compare retrieved string with the '''DIFFERENT STRING''' inserted by the ''test double''
** NOTE - won't work if you wait until the end of the test to restore the original routine (i.e. srDOUBLE_SET)
** NOTE - won't work if you wait until the end of the test to restore the original routine (i.e. srDOUBLE_SET)


* Execute ''Test Double'' Test Units only  
* Execute ''Test Double'' Test Units only  


   > stride -O myoptions.txt --run TestDouble_Reference TestDouble_Definition
   > stride --options_file myoptions.txt --run TestDouble_Reference TestDouble_Definition


   Loading database...
   Loading database...
Line 88: Line 83:
     Summary: 6 passed, 0 failed, 0 in progress, 0 not in use.
     Summary: 6 passed, 0 failed, 0 in progress, 0 not in use.


=== Run and Publish Results ===


== Run and Publish Results ==
When you have completed the Exercise(s) publish your results to Test Space. If you have not added test space options to your options file (<tt>myoptions.txt</tt>) please see [[Training_Getting_Started#Test_Space_Access| testspace access]].  
 
When you have completed the Exercise(s) publish your results to Test Space. To make it easier for now we recommend that you update your existing option file (myoptions.txt) with the following if not already done:
 
  #### Test Space options (partial) #####
  #### Note - make sure to change username, etc. ####
  --testspace <nowiki>https://username:password@yourcompany.stridetestspace.com</nowiki>
  --project Training
  --name YOURNAME


   > stride -O myoptions.txt --run TestDouble_Reference TestDouble_Definition --space TestDouble --upload
   > stride --options_file myoptions.txt --run TestDouble_Reference TestDouble_Definition --space TestDouble --upload


Note: This space has been set up with a Baseline of [[Training_Getting_Started#Test_Space_Access | ''expected test results'']] that you can use to validate your results.
Note: This space has been set up with a Baseline of [[Training_Getting_Started#Test_Space_Access | ''expected test results'']] that you can use to validate your results.


= Reference =
== Reference ==
The following reference information is related to passing parameters to Test Units.
The following reference information is related to passing parameters to Test Units.


== Wiki ==
=== Wiki ===
* [[Using_Test_Doubles | Using Test Doubles]] - Outlines the basic steps to enable a ''double''
* [[Using_Test_Doubles | Using Test Doubles]] - Outlines the basic steps to enable a ''double''
** Capture the f(x) that is going to be doubled
** Capture the f(x) that is going to be doubled
Line 124: Line 112:
** [http://msdn.microsoft.com/en-us/magazine/cc163358.aspx MSDN Magazine description]
** [http://msdn.microsoft.com/en-us/magazine/cc163358.aspx MSDN Magazine description]


== Samples ==
=== Samples ===


* [[Test_Double_Sample | Test Double Sample]] that can be a useful reference. This reference example can be built and executed like all of samples using the Off-Target environment.
* [[Test_Double_Sample | Test Double Sample]] that can be a useful reference. This reference example can be built and executed like all of samples using the Off-Target environment.


[[Category: Training]]
[[Category: Training]]

Latest revision as of 17:41, 27 February 2013

Objectives

This Training Module is focused on explaining how to leverage Test Doubles in the context of executing a test. For an overview of intercepting existing functions please refer to Test Doubles. The module covers following topics:

  • How to apply pragmas for function intercepting
  • How to decide what kind of mangling is required
    • Definition verses Reference
    • Explicit verses Implicit
  • Setting and Resetting the Double implementation


There are two new files used -- TestDouble.cpp & TestDouble.h --- that implement two test Units:

  • TestDouble_Reference
  • TestDouble_Definition


All of the Test Units have test cases already implemented (used for reference) and have one test method that you are required to implement called Exercise. Currently the exercise methods return a NOT IN USE status.


Instructions

Build and Run TestApp

  • Build TestApp using SDK makefile
  • Startup TestApp
  • If you have not created an option file, please refer to setup
  • Execute Test Double Test Units only
 > stride --options_file myoptions.txt --run TestDouble_Reference --run TestDouble_Definition
 Loading database...
 Connecting to device...
 Executing...
   test unit "TestDouble_Reference"
     > 2 passed, 0 failed, 0 in progress, 1 not in use.
   test unit "TestDouble_Definition"
     > 2 passed, 0 failed, 0 in progress, 1 not in use.
   ---------------------------------------------------------
   Summary: 4 passed, 0 failed, 0 in progress, 2 not in use.

 Disconnecting from device...
 Saving result file...


  • Review the details of the test results using a Browser. Open TestApp.xml which can be found in the sample_src directory (based on the output option). By opening the xml file in a web browser the xsl is automatically applied to create html.

Implement Exercise

  • TestDouble_Reference::Exercise
    • Implement a new test double for the strlen() routine:
      • Add a NOTE capturing its name when it is called
      • Uses the real strlen() to return the length of the passed in string
    • Hint: You will probably want to read about Using Test Doubles
    • Use srEXPECT_EQ() to validate that sut_strcheck() returns correct length
    • Make sure to restore the original routine
  • TestDouble_Definition::Exercise
    • Implement a new test double for the sut_strcpy() routine:
      • Log its name when it is called
      • Validate string passed to sut_strsave() is received correctly by the test double
      • Remember that sut_strsave() calls sut_strcpy() with the string passed to it
      • Can use a test macro to validate the string is correctly passed to the double
      • Call the original function (sut_strcpy()) with a DIFFERENT STRING (i.e "Exercise Test String 2")
        • Call the original within the test double
        • Make sure to reset the original function within the mock before calling it (i.e. srDOUBLE_RESET)
    • Call sut_strsave() with a string (i.e. "Exercise Test String 1")
    • Use sut_strget() to retrieve a string
    • Compare retrieved string with the DIFFERENT STRING inserted by the test double
    • NOTE - won't work if you wait until the end of the test to restore the original routine (i.e. srDOUBLE_SET)
  • Execute Test Double Test Units only
 > stride --options_file myoptions.txt --run TestDouble_Reference TestDouble_Definition
 Loading database...
 Connecting to device...
 Executing...
   test unit "TestDouble_Reference"
     > 3 passed, 0 failed, 0 in progress, 0 not in use.
   test unit "TestDouble_Definition"
     > 3 passed, 0 failed, 0 in progress, 0 not in use.
   ---------------------------------------------------------
   Summary: 6 passed, 0 failed, 0 in progress, 0 not in use.

Run and Publish Results

When you have completed the Exercise(s) publish your results to Test Space. If you have not added test space options to your options file (myoptions.txt) please see testspace access.

  > stride --options_file myoptions.txt --run TestDouble_Reference TestDouble_Definition --space TestDouble --upload

Note: This space has been set up with a Baseline of expected test results that you can use to validate your results.

Reference

The following reference information is related to passing parameters to Test Units.

Wiki

  • Using Test Doubles - Outlines the basic steps to enable a double
    • Capture the f(x) that is going to be doubled
    • Code the test logic to additionally set the Double and restore original f(x)
  • Intercepting a function - Pragma specifics outline
    • For this exercise, there are NO optional parameters when capturing a f(x) to be intercepted
  • Details of the pragma parameters can be understood in the Intercept Module article. Please review in detail the following:
    • context - This refers to incepting a callee or the called routine.
    • name-mangling - How the function name is switched during the compiling process
    • group-id - Used to associate a group of intercepted functions

Samples

  • Test Double Sample that can be a useful reference. This reference example can be built and executed like all of samples using the Off-Target environment.