Training Doubling: Difference between revisions
Line 51: | Line 51: | ||
*** 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 |
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
- Implement a new test double for the strlen() 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)
- Implement a new test double for the sut_strcpy() routine:
- 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
- Some external links to review:
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.