Menu

#11 Parameterized tests

open
Hetu
None
1
2013-06-10
2013-06-10
Hetu
No

Allow parameterized tests in the spirit of Zohhak. For lack of MATLAB annotations, we could employ specific functions with name matching instead. For example, testwith_my_specific_test provides the parameter sets to be applied on test_my_specific_test:

function params = testwith_minmaxmedian
    pstruct = @(h, d, r) struct('fhandle', h, 'data', d, 'result', r);

    common_data = [1 6 2 1 3];
    params(1) = pstruct(@min, common_data, 1);
    params(2) = pstruct(@max, common_data, 6);
    params(3) = pstruct(@median, common_data, 2);
    params(4) = pstruct(@min, [], []);

function test_minmaxmedian(params)
    assert_equals(params.fhandle(params.data), params.result);

In case of failure, test_my_specific's current parameterization must be put out, along assert_equal's error message as usual.

Alternatively, and for one-line test cases, provide the test function as function handle. Then you only need to make sure, no function with name test_minmaxmedian actually exists.

function [params, testfun] = testwith_minmaxmedian
    pstruct = @(h, d, r) struct('fhandle', h, 'data', d, 'result', r);

    common_data = [1 6 2 1 3];
    params(1) = pstruct(@min, common_data, 1);
    params(2) = pstruct(@max, common_data, 6);
    params(3) = pstruct(@median, common_data, 2);
    params(4) = pstruct(@min, [], []);

    testfun = @(p) assert_equals(p.fhandle(p.data), p.result);

Discussion


Log in to post a comment.

MongoDB Logo MongoDB