|
C.1 Simplified Unit Testing and Debugging
|
Unit testing is a method used to verify that individual parts of your code
function as intended. SheerPower simplifies this process by enabling you to
quickly write and execute unit tests, which helps you identify bugs early
and maintain high code quality.
Testing your code is crucial. With SheerPower's extremely fast compile and
link times (typically under a second), you can easily write new code and
test it incrementally, every few lines. SheerPower also allows you to
conveniently write unit tests alongside your main code, ensuring continuous
verification as you develop.
SheerPower makes unit testing quick and easy with just one statement
and two simple directives:
- option test on -- enable test mode
- %test some_statement to compile only if in test mode
- %test_ignore some_statement to ignore if in test mode
sheerpower's testing features help developers write reliable and error-free code by allowing them to
define and run unit tests easily. By using %test and %test_ignore directives,
developers can mark specific
routines and lines of code for testing or ignoring during test runs.
Enabling test mode with option test on ensures that all marked
tests are executed, providing immediate feedback on the code's functionality. This systematic
testing approach helps catch bugs early, maintain code quality, and ensure that new changes
do not break existing functionality.
option test on
%test print 'We are in test mode'
age = 15
%test age = 25
debug show age
Let's say there is an include file filled with testing code. You could
conditionally compile and run that testing code by:
option test on
tax_rate = 6.5/100
%test %include 'safe_unit_tests.spinc'
Here is what safe_unit_tests.spinc looks like. Notice the use of the
assert statement. You tell
assert what is
expected and it produces an
exception if the assert fails:
// safe_unit_tests.spinc
print 'Testing the tax_rate of '; tax_rate
assert tax_rate > 0 and tax_rate <= 1.00, 'Tax rate is out of bounds'
print 'Tax rate is fine'
For clarity in your code, you can explicitly turn off test mode by:
option test off
By default, test mode is turned off.
Your code can contain any number of
option test on or
option test
off statements. However, for easier code maintenance, just having one at
the top of your program is best. Your code can also contain any number of
%test and
%test_ignore directives.
In addition, you can add one or more
debug all statements to your code. This statement assists with debugging
by writing handy debug information to a file. This helps with debugging with:
- Comprehensive Debugging: Provides detailed information on variable values, routine calls, and errors.
- Runtime Execution: Helps debug complex issues by activating in the middle of program execution.
- Efficient Issue Identification: Aids in quickly locating and resolving bugs.
- Enhanced Debugging Insights: Offers thorough insights into program behavior, facilitating deeper understanding and troubleshooting.
The debug information includes:
- the names of all variables and their current values
- the current call stack
- the names all open files and tables
- and more.
The name of the debug information file is in the form
myprogram_debug.txt where
myprogram is the name of your program.
Optionally you can supply your own filename:
For example:
debug all "myfile.txt"
Using
debug all is especially helpful when dealing with
elusive bugs.
Common Errors Reported at compile-time:
- Uninitialized Variables
- Type Mismatches
- Missing Routine Parameters
- Misspelled variable names or routines names (suggested spellings are given)
- Syntax Errors with highlighting
- Inconsistent variable Scoping