Can cxxtest suite be dynamically extended at run-t

2019-08-10 12:28发布

问题:

I wish to dynamically extend my CxxTest Suite with additional test items, but am finding that all the testing scenerios must be available (hard-coded) at compile time.

My scenario is that I've got a fairly bulky C++ class that has 20+ methods to be tested. This class needs to be tested for 40+ DIFFERENT data sets. These data sets are obtained via the class constructor, controlled via parameters.

My primary objective is to avoid re-writing the same 20 test cases for the different data sets. I would like to use a control file that contains the list of data sets, and just create different fixtures for the same test suite.

Q1) How does one dynamically (at run time) add more tests to the testing suite?

Q2) Can one dynamically add fixtures at run time? Is there a better testing suite that allows for dynamic fixtures?

Q3) Is this something that the TDD technique avoids? Anyone got a good summary of the TDD Technique.

Thanks,

-- J Jorgenson --

回答1:

There's not really any direct way to do it. The number of tests is determined prior to compile time, when you run cxxtestgen. It parses your files and finds method names starting with test. It also finds TestSuite descendants and generates code to instantiate each one once. You can write a createSuite function on each of your test suites to pass parameters to the suite's constructor, but you're still limited to returning just one object.

You could modify your build setup to invoke the test program 40 different times, passing a different parameter on the command line each time. The wrinkle in that plan is that the default main generated by CxxTest doesn't accept command-line parameters. You'll need to provide your own implementation that checks parameters and then invokes the normal test runner afterward. Something like this:

std::string global_var; // check this in your test cases
int main(int argc, char* argv[]) { // add parameter list
  global_var = argv[1]; // read parameter list
  return CxxTest::ErrorPrinter().run(); // standard CxxTest
}

To use that function, omit the --error-printer option when you run cxxtestgen, add <CxxTest world> at the end of the file, and use the --template option to generate your test program.

But as long as you're writing your own main, you might try parsing your data-set file there, too, and then invoking the test runner multiple times. Something like this:

int main() {
  std::fstream dataset("datasetlist.txt");
  int result = 0;
  while (std::getline(dataset, global_var))
    result += CxxTest::ErrorPrinter().run();
  return result;
}