Difference between revisions of "Training Doubling"

From STRIDE Wiki
Jump to: navigation, search
m (Reverted edits by Jeffs (Talk) to last revision by Marku)
(Undo revision 13914 by Jeffs (Talk))
Line 23: Line 23:
 
* [[Building_an_Off-Target_Test_App#Build_Steps | 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  
Line 59: Line 53:
 
** 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'''
Line 74: Line 67:
 
** 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  
Line 92: Line 84:
 
=== Run and Publish Results ===
 
=== Run and Publish Results ===
  
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:
+
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]].  
 
 
  #### 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 --options_file myoptions.txt --run TestDouble_Reference TestDouble_Definition --space TestDouble --upload
 
   > stride --options_file myoptions.txt --run TestDouble_Reference TestDouble_Definition --space TestDouble --upload

Revision as of 18:34, 1 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
    • 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.