简体   繁体   English

使用Google Test测试文件输出?

[英]Use Google Test to test file output?

I'm working on a project to create a simulator (for modeling biological systems) in C++. 我正在开发一个用C ++创建模拟器(用于建模生物系统)的项目。 The simulator takes an input file of parameters and then generates an output file with hundreds of molecule counts at different time points from the simulation. 模拟器获取参数的输入文件,然后在模拟的不同时间点生成具有数百个分子计数的输出文件。 I'm using Google Test for all of my unit testing. 我正在使用Google Test进行所有单元测试。 I also want to include some higher level tests where I supply an input file with various model parameters and then check that the output file matches some reference file. 我还想包括一些更高级别的测试,其中我提供具有各种模型参数的输入文件,然后检查输出文件是否与某个参考文件匹配。 Someone recommended using bash-tap for these higher level tests, but I'd prefer to stick to Google Test if possible. 有人建议使用bash-tap进行这些更高级别的测试,但我更愿意坚持使用Google Test。 Is it possible to use Google Test for the higher level tests that I've described here? 是否可以将Google Test用于我在此处描述的更高级别的测试?

We write CAE software (simulators) and use Google Test. 我们编写CAE软件(模拟器)并使用Google Test。 We face similar issues, so hopefully you'll find the answers practical. 我们面临类似的问题,所以希望你能找到切实可行的答案。

You can write the higher-level tests, but you will often have to do more than just "EXPECT_EQ()" for checking pass/fail. 您可以编写更高级别的测试,但是您通常需要做的不仅仅是“EXPECT_EQ()”来检查通过/失败。 For example, if you had to test the connectivity of two abitrary graphs, it can be difficult if the algorithms are allowed to vary the order of nodes. 例如,如果必须测试两个abitrary图的连通性,那么如果允许算法改变节点的顺序则可能很困难。 Or, if you are comparing a matrix, sometimes you can have cases where the matrix rows and columns can be switched with no problem. 或者,如果您要比较矩阵,有时您可能会遇到矩阵行和列可以毫无问题地切换的情况。 Perhaps round-off error is ok. 也许圆整错误是可以的。 Be prepared to deal with these types of problems as they will be much more of an issue with a full simulator than with a unit test. 准备好处理这些类型的问题,因为对于完整的模拟器而言,它们将比单元测试更成问题。

A more practical issue is when your organization says "run all tests before you check in." 一个更实际的问题是,当您的组织说“在您办理登机手续之前运行所有测试”。 Or, maybe they run every time you hit the build button. 或者,也许每次点击构建按钮时它们都会运行。 If that's the case, you need to differentiate these unit tests from the higher level tests. 如果是这种情况,则需要将这些单元测试与更高级别的测试区分开来。 We use Google Test Runner in Visual Studio, and it expects to run everything where the filename is "*Test*". 我们在Visual Studio中使用Google Test Runner,它希望运行文件名为“* Test *”的所有内容。 It is best to name the higher level tests something else to be clear. 最好将更高级别的测试命名为其他内容。

We also had to turn our entire executable into a DLL so that it could have tests run on top of it. 我们还必须将整个可执行文件转换为DLL,以便它可以在其上运行测试。 There are other approaches (like scripting) which could be used with Google Test, but we've found the executable-as-a-dll approach to work. 还有其他方法(如脚本)可以与Google Test一起使用,但我们已经找到了可执行的as-a-dll方法。 Our "real" product executable is simply a main() function that calls app_main() in the dll. 我们的“真正的”产品可执行文件只是一个main()函数,它调用dll中的app_main()。

And, one final tip when using the Runner: If your app gets the --gtest_list_tests argument, don't do a bunch of expensive setup: 并且,使用Runner时的最后一个提示:如果你的应用程序获得了--gtest_list_tests参数,请不要做一堆昂贵的设置:

// Don't run if we are just listing tests.
if (!::testing::GTEST_FLAG(list_tests))
{
        // Do expensive setup stuff here.
}

int result = RUN_ALL_TESTS();

if (!::testing::GTEST_FLAG(list_tests))
{
        // Do expensive shutdown stuff here.
}

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

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