Tool to add automatic checkpoints to messy code (that can later be turned into unit tests).
This is not meant to be a replacement for a carefully written Testing Framework. Rather, it is a development tool to refactor code that has little or no tests; to make sure that, as you tidy up the code (and write propper tests) it continues to produce the same outputs.
The repo defines three classes:
TestCheckpoint
defines individual test objects (see Usage below)WorkspaceBackup
is a wrapper aroundsave
. It allowsTestCheckpoint
to do workspace dumps from the caller environment, with additional options (e.g. to exclude certain classes).TestSession
is used to store a static (global-like)1 index of test objects. It allows these to be accessed from anywhere in the code, via the structureTestCheckpoint.session.tests
.
- Test objects include a unique
id
, andinput
andoutput
options (WorkspaceBackup
objects) that define which variables are to be (re)stored before and after the code to be tested. - Every test object will be pushed to the static index, as
TestCheckpoint.session.tests.(id)
function define_test_objects()
% Has to run before instrumented code
% Object will become available as TestCheckpoint.session.tests.test_dummy
TestCheckpoint('test_dummy', 'input', {'onlynames', {'x','y'}}, 'output', {'onlynames', 'z'});
end
- Retrieve a test from
TestCheckpoint.session.tests
- Use
obj.do('input')
andobj.do('output')
to mark the spots right before and after the code to be tested:
function z = instrumented_function(x, y)
% Get test object -- requires a previous call to TestCheckpoint('test_dummy',...)
obj = TestCheckpoint.session.tests.test_dummy;
% insert right before code to be tested
obj.do('input');
% Do stuff
z = dummy(x, y);
% insert right after code to be tested.
obj.do('output');
end
When the state
flag of a test object obj
is set to 'setup'
(default, if there are no backup files associated with id
):
obj.do('input')
- Will save all reference workspace inputs, intoobj.input.file
.obj.do('output')
- Will save all reference workspace outputs, intoobj.output.file
.
While obj.state == 'idle'
(once reference files have been stored in a setup
run), calls to obj.do
will not do anything.
If the state
flag of a test object obj
is set to test
(this must be done explicitly):
obj.do('input')
- Will restore all reference workspace inputs, from intoobj.input.file
.obj.do('output')
- Will save all test workspace outputs, intoobj.test_io.file
.
Compare the contents of obj.test_io.file
and obj.output.file
TODO: This should be done automatically, perhaps generating a test-script with references to the test files
Use TestCheckpoint.session.reset(true)
to delete all test objects and their associated files.