UnitTest++命令行参数
UnitTest++ command line arguments
我想在一个测试中使用命令行参数。我在网上找不到任何这样的例子。
TEST(SomeTest)
{
std::string file("this is some command line argument");
CHECK(something);
}
int main(int argc, char** argv)
{
return UnitTest::RunAllTests();
}
有什么想法吗?
我认为您想要的是指定"测试输入"的能力,而无需为每个输入进行新的测试。
假设你有3组测试数据要运行你的测试例程。你只想运行
./unittest_build SomeTest file1
./unittest_build SomeTest file2
./unittest_build SomeTest file3
而无需将测试烘焙到测试构建中。
但你也可以这么做:
void runCheck(const std::string& filename) {
std::string file(filename);
// well, you're gonna do stuff here
CHECK(something);
}
TEST(SomeTest)
{
runCheck(std::string("your first testing datafile"));
runCheck(std::string("your second testing datafile"));
runCheck(std::string("your third testing datafile"));
// this is what your test is. It tests these files.
}
int main(int argc, char** argv)
{
return UnitTest::RunAllTests();
}
参数化测试意味着测试不再只是一个测试。它现在是一个函数,而不是一个单元测试。
答案
没有真正的理由直接测试命令行参数。相反,编写单元测试来检查给定不同参数的代码(函数和类)的行为。一旦您对您的代码在单元测试中正常工作感到满意,只需将其插入main
,它也应该在那里正常工作。
澄清
假设您对std::string
构造函数的参数进行了单元测试。
TEST(SomeTest)
{
std::string file("this is some command line argument");
CHECK(something);
}
然后将其插入main
。
int main(int argc, char** argv)
{
std::string file(argv[1]);
// do stuff....
return 0;
}
因为在将命令行参数传递给构造函数之前,它不应该发生任何事情,所以您已经对其进行了有效的测试。另一方面,如果您的main
一团糟,我建议您首先进行重构。
相关文章:
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 如何处理linux终端中带有负号(-)的C++中的命令行参数
- 使用 C++ 将命令行参数拆分为参数/向量
- 如何在OMNET++中添加专门的命令行参数?
- 如何在不传递命令行参数的情况下在 c++ 中设置环境变量
- atoi() 在应用于大型命令行参数时会产生不正确的值
- 是否可以在命令行中将输入参数传递给可执行文件
- 命令行参数,cant 或两个变量
- 在 Windows 中使用 boost::p rogram_options 从命令行参数读取 Unicode 字符
- 如何在 Android/NDK 上将命令行参数从 gradlew.bat 传递到 Clang
- 编写一个将 LLVM IR 文件作为命令行参数的程序
- 通过命令行参数获取llvm ir文件时面临问题
- 有没有办法根据命令行参数定义数组大小?运行时与编译时实例化?
- 从命令行获取参数时出现问题
- 如何在不使用文件扩展名的情况下使用命令行参数打开C++中的文本文件?
- 命令行参数在不到 3 个 LOC 中 int?
- 在命令行参数中使用引号
- 使用参数 + stdout + stderr 从命令行调用 MFC 应用程序
- 在VS2013中使用devenv (C++)传递命令行参数argv
- 二进制数据作为命令行参数