Training Doubling: Difference between revisions

From STRIDE Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
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
* Build TestApp using SDK makefile
Line 51: Line 51:
* 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 '''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'''
Line 89: Line 89:




== 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. To make it easier for now we recommend that you update your existing option file (myoptions.txt) with the following if not already done:  
Line 103: Line 103:
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 124:
** [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]]

Revision as of 22:25, 31 August 2011

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 not already done, create an option file (myoptions.txt) using the following content (Windows example)
 ##### 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
 > stride -O myoptions.txt --run TestDouble_Reference 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:
      • 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:
      • Log its name when it is called
      • 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")
      • Hint - Reset the original function within the mock
    • 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 -O 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. 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 https://username:password@yourcompany.stridetestspace.com
 --project Training
 --name YOURNAME
  > stride -O 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.