c++ - Generate Unit Tests from a scenario table where inputs and expected results are specified -
i'm trying generate post coding ut module. module classifies input data db, if match found, proper value returned.
after building table possible input scenarios , expected results, discovered got on 50 tests create. tests same, except test name , inputs, kind of template seemed fit here, reducing code mess.
what imagined, either macro creates such template , expands tests input data , expected result or using structure representing scenario table macro wrap up. (i guess c++ template adequate here).
however, i'm not sure correct path take , know if can give recommendations of how tackle it. there question of how fits tdd approach..
here idea using cpputest:
#define gen_test_mod_classifier(group_name, test_name, priority, iscompress, isx, expecting) \ test(group_name, test_name) \ { \ int hit; \ setupdb(priority, iscompress, isx); \ hit = func(priority, iscompress, isx); \ check_equal(expecting, hit); \ }
usage example:
gen_test_mod_classifier(classifier_tests, lowpriority_nocompress_nox__hit, prio_low, not_compress, no_x, hit_db)
thanks, edy.
well, w/o external tools best can using macros. that's because need have test(group_name, test_name)
each test case.
it may not case. may fine not having separate test case each scenario or cpputest may have support programmatic way of adding test cases. in case, can create method takes vector of input-output-testcasename tuples. , add testcases / run tests on each tuple. no macros needed.
something (pseudocode):
typedef std::tuple<std::string, std::string, prioritytype, compresstype, expectedvaluetype> testinfo; void runtest(const testinfo& testinfo) { // assuming here you're ok kind of test cases separation std::cout << "running test" << std::get<0>(testinfo) << ", " << std::get<1>(testinfo) << std::endl; int hit; setupdb(std::get<2>(testinfo), std::get<3>(testinfo), isx); hit = func(std::get<2>(testinfo), std::get<3>(testinfo), isx); check_equal(std::get<4>, hit); } void runtests(const testinfo& testinfo) { std::for_each(testinfo.begin(), testinfo.end(), runtest); } std::vector<testinfo> tests = { test1, test2 }; runtests(tests);
if doesn't work, macros fine, , go table based approach there well. take @ boost preprocessor (http://www.boost.org/doc/libs/1_54_0/libs/preprocessor/doc/index.html)
pseudocode again:
#include <boost\preprocessor\seq.hpp> #define generate_test_case(_ignore1_, _ignore2_, _testcaseinfosequence_) \ test(boost_pp_seq_elem(0, testcaseinfosequence), boost_pp_seq_elem(1, testcaseinfosequence)) \ { \ int hit; \ setupdb(boost_pp_seq_elem(2, testcaseinfosequence), boost_pp_seq_elem(3, testcaseinfosequence), boost_pp_seq_elem(4, testcaseinfosequence)); \ hit = func(boost_pp_seq_elem(2, testcaseinfosequence), boost_pp_seq_elem(3, testcaseinfosequence), boost_pp_seq_elem(4, testcaseinfosequence)); \ check_equal(boost_pp_seq_elem(5, testcaseinfosequence), hit); \ } #define testcases \ ( \ (group1)(test1)(prio1)(iscompress1)(isx1)(expectedval1) \ (group1)(test2)(prio2)(iscompress2)(isx2)(expectedval2) \ ) // statement generate tests cases in defined in testcases sequnce. boost_pp_seq_foreach(generate_test_case, _ignore_, testcases);
Comments
Post a Comment