简体   繁体   English

cppunit使用命令行参数

[英]cppunit to use command line arguments

I have a CPP unit test which tests a class which is designed to read configuration: we can call this class 我有一个CPP单元测试,它测试一个旨在读取配置的类:我们可以调用该类

Config

The config class has the capacity of doing config类有能力做

Config c;
c.read("/tmp/random-tmp-directory/test.conf");

The random-temp-directory is created by a bash script and should be passed into the test binary. random-temp-directory由bash脚本创建,应传递到测试二进制文件中。

#!/bin/bash
TEMPDIR=$(mktemp)
cp files/config/test.conf $TEMPDIR/.
./testConfig $(mktemp)/test.conf

The above creates a temp directory, copies our temporary file and passes the path to the test, so it can load the correct file. 上面的代码创建了一个temp目录,复制了我们的临时文件,并将路径传递给测试,以便它可以加载正确的文件。

Is there a way to tell CPPUNIT to send the commandline arguments, or any arguments to the test registry? 有没有办法告诉CPPUNIT将命令行参数或任何参数发送到测试注册表?

Here is my testConfig.cpp 这是我的testConfig.cpp

#include <all the required.h>

CPPUNIT_TEST_SUITE_REGISTRATION(testConfig);

int main(int argc, char ** argv)
{
    CPPUNIT_NS::TestResult testresult;
    CPPUNIT_NS::TestRunner runner;
    CPPUNIT_NS::TestFactoryRegistry &registry = CPPUNIT_NS::TestFactoryRegistry::getRegistry();

    // register listener for collecting the test-results
    CPPUNIT_NS::TestResultCollector collectedresults;
    testresult.addListener(&collectedresults);

    runner.addTest(registry.makeTest());
    runner.run(testresult);

    // Print test in a compiler compatible format.
    CppUnit::CompilerOutputter outputter( &collectedresults, std::cerr );
    outputter.write(); 

    return collectedresults.wasSuccessful() ? 0 : 1;
}

Consider dividing your code into at least three distinct methods: the part that constructs the config file name, the part that reads the config file, and the part that parses what was read from the config file. 考虑将代码至少分为三种不同的方法:用于构造配置文件名称的部分,用于读取配置文件的部分以及用于分析从配置文件读取的内容的部分。 You can easily and thoroughly unit test both the file name builder and the parser methods. 您可以轻松而彻底地对文件名构建器和解析器方法进行单元测试。 And as long as you can test simply reading data from the file even one time, you should be golden. 而且,只要您可以测试甚至仅一次读取文件中的数据,就应该是黄金。

[edit] [编辑]

For example, you might have a method like string & assembleConfigFileName(string basepath, string randompath, string filename) that takes in the different components of your path and filename, and puts them together. 例如,您可能有一个像string & assembleConfigFileName(string basepath, string randompath, string filename)方法,该方法将路径和文件名的不同部分组合在一起。 One unit test should look like this: 一个单元测试应如下所示:

void TestConfig::assembleConfigFileName_good()
{
    string goodBase("/tmp");
    string goodPath("1234");
    string goodName("test.conf");

    string actual(assembleConfigFileName(goodBase, goodPath, goodName));

    string expected("/tmp/1234/test.conf");

    CPPUNIT_ASSERT_EQUAL(expected, actual);
}

Now you can test that you're building the fully qualified config file name exactly correctly. 现在,您可以测试您是否正在正确正确地构建标准配置文件名。 The test is not trying to read a file. 测试未尝试读取文件。 The test is not trying to generate a random number. 测试未尝试生成随机数。 The test is providing an example of exactly what kinds of input the routine needs to take, and stating exactly what the output should look like given that exact input. 该测试提供了例程需要采取哪种输入的示例,并给出了给出确切输入后输出应该是什么样的示例。 And it's proving the code does exactly that. 事实证明,代码确实可以做到这一点。

It's not important for this routine to actually read a config file out of a temp directory. 对于该例程而言,从临时目录中实际读取配置文件并不重要。 It's only important that it generate the right file name. 生成正确的文件名很重要。

Similarly, you build a unit test to test for each possible flow through your code, including error scenarios. 同样,您将构建一个单元测试以测试代码中的每个可能流,包括错误情形。 Let's say you wrote an exception handler that throws if the random path is wrong. 假设您编写了一个异常处理程序,如果随机路径错误则抛出该异常处理程序。 Your unit test will test the exception mechanism: 您的单元测试将测试异常机制:

void TestConfig::assembleConfigFileName_null_path()
{
    string goodBase("/tmp");
    string nullPath;
    string goodName("temp.config");

    CPPUNIT_ASSERT_THROW(assembleConfigFileName(goodBase, nullPath, goodName), MissingPathException);
}

The tests are now a document that says exactly how it works, and exactly how it fails. 现在,这些测试是一个文档,其中确切说明了其工作原理以及失败的原因。 And they prove it every single time you run the tests. 他们每次运行测试时都会证明这一点。

Something you appear to be trying to do is to create a system test, not a unit test. 您似乎要尝试做的事情是创建系统测试,而不是单元测试。 In a unit test, you do NOT want to be passing in randomly pathed config files. 在单元测试中,您不想传递随机路径的配置文件。 You aren't trying to test the external dependencies, that the file system works, that a shell script works, that $TMPDIR works, none of that. 您不是要测试外部依赖关系,文件系统是否正常工作,shell脚本是否正常工作,$ TMPDIR是否正常工作等等。 You're only trying to test that the logic you've written works. 您只想测试您编写的逻辑是否有效。

Testing random files in the operating system is very appropriate for automated system tests, but not for automated unit tests. 在操作系统中测试随机文件非常适合自动化系统测试,但不适用于自动化单元测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM