cxxtest 套件可以在运行时动态扩展吗? [英] Can cxxtest suite be dynamically extended at run-time?

查看:26
本文介绍了cxxtest 套件可以在运行时动态扩展吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望动态扩展我的 CxxTest 套件 并添加额外的测试项目,但我发现所有测试场景都必须在编译时可用(硬编码).

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.

我的情况是,我有一个相当庞大的 C++ 类,其中有 20 多个要测试的方法.此类需要针对 40 多个不同的数据集进行测试.这些数据集是通过类构造函数获得的,通过参数控制.

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.

我的主要目标是避免为不同的数据集重新编写相同的 20 个测试用例.我想使用一个包含数据集列表的控制文件,并为同一个测试套件创建不同的夹具.

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) 如何动态地(在运行时)向测试套件添加更多测试?

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

Q2) 可以在运行时动态添加设备吗?是否有更好的测试套件允许动态夹具?

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

Q3) 这是 TDD 技术避免的吗?任何人都对 TDD 技术有很好的总结.

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

谢谢,

-- J 乔根森 --

-- J Jorgenson --

推荐答案

实际上并没有任何直接的方法可以做到.当您运行 cxxtestgen 时,测试的数量在编译时间之前确定.它解析您的文件并查找以 test 开头的方法名称.它还查找 TestSuite 后代并生成代码以将每个后代实例化一次.您可以在每个测试套件上编写createSuite 函数以通过套件构造函数的参数,但您仍然只能返回一个对象.

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.

您可以修改构建设置以调用测试程序 40 次,每次都在命令行上传递不同的参数.该计划的问题在于 CxxTest 生成的默认 main 不接受命令行参数.您需要提供自己的实现来检查参数,然后调用正常的测试运行程序.像这样:

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
}

要使用该功能,请在运行 cxxtestgen 时省略 --error-printer 选项,添加 文件末尾,并使用 --template 选项生成您的测试程序.

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.

但只要您正在编写自己的 main,您也可以尝试在那里解析您的数据集文件,然后多次调用测试运行程序.像这样:

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;
}

这篇关于cxxtest 套件可以在运行时动态扩展吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆