简体   繁体   English

我应该使用std :: system来编写单元测试的脚本吗?

[英]Should I use std::system to do scripting parts of Unit Tests?

I'm in the situation that I have a unit test for a C++ tool where part of the tests need to actually run a test-commandline-tool (that will then trigger something I can check in the test once the test-tool has ended). 我的情况是我有一个C ++工具的单元测试,其中部分测试需要实际运行一个测试命令行工具(然后在测试工具结束后会触发我可以在测试中检查的东西) )。

I think the C++ Unit Testing Framework I use is quite irrelevant, my question pertains to the legitimate use of system("test_tool.exe ...") -- it would seem that system isn't on the top tool list for C++ developers (and rightly so in general, IMHO). 我认为我使用的C ++单元测试框架是无关紧要的,我的问题与system("test_tool.exe ...")的合法使用有关system("test_tool.exe ...") - 似乎 系统 不在 C ++开发人员的顶级工具列表中(一般而言,恕我直言)。

So, is system known to be used successfully in Unit Tests? 那么, system是否已成功用于单元测试? Should I expect any problem when using it to execute a well-known tool in a UT environment? 在UT环境中使用它来执行一个着名的工具时,我是否应该遇到任何问题?

To me, this is an acceptable use of system - but you will need to be careful about "how do you check that the test_tool did what is said it would. 对我来说,这是一个可接受的system使用 - 但是你需要注意“你如何检查test_tool是否按照它说的那样做了。

However, hopefully your overall test system has reasonable security, so malicious users should be less of an issue than a general use of system in a random application. 但是,希望您的整体测试系统具有合理的安全性,因此恶意用户应该比在随机应用程序中普遍使用system更少。

Of course, it also wouldn't be that hard to write some code that uses CreateProcess or another "less evil" variant than system . 当然,编写一些使用CreateProcess或其他“不那么邪恶”的变体而不是system代码也不难。 Using this will probably also give you a bit more flexibility and control over the newly started process - eg you can monitor the process and tell when it finished and what status it exited with, including detecting if it exited nicely or crashed. 使用它可能还可以让您更灵活地控制新启动的流程 - 例如,您可以监控流程并告知流程何时完成以及退出的状态,包括检测流程是否正常退出或崩溃。

If test_tool is a tool you've installed, there shouldn't be any problem. 如果test_tool是您安装的工具,那么应该没有任何问题。 What you want to avoid is using system with a string which even partially comes from user input (unless you've really vetted that input), and using it to invoke system specific commands (since your portability goes down the drain). 您要避免的是使用带有字符串的system ,该字符串甚至部分来自用户输入(除非您确实检查过该输入),并使用它来调用系统特定的命令(因为您的可移植性耗尽了)。 With regards to the latter, you might want to invoke the test using system( "test_tool ..." ) , without the .exe . 关于后者,您可能希望使用system( "test_tool ..." )调用测试,而不使用.exe

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

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