Boost.测试和分叉
我正在使用 Boost.Test 进行单元测试,目前正在单独的线程中运行各种模拟服务器,这些线程从每个测试中启动。为了更准确地测试我的代码,模拟服务器实际上应该位于单独的进程中。
我正在考虑按照这些思路做一些事情:
MY_TEST()
if (fork() == 0) {
runMockServer(); // responds to test requests or times out, then returns
exit(0);
}
// Connect to MockServ and Run actual test here
END_TEST()
但我担心这会搞砸测试框架。
这安全吗?有人做过这样的事吗?
如果重要的话,我在 Ubuntu 8.04 上使用 Boost 1.34.1。
I'm using Boost.Test for Unit testing and am currently running various mock servers in separate threads which get launched from within each test. In order to more accurately test my code the mock server's should really be in separate processes.
I was thinking about doing something along these lines:
MY_TEST()
if (fork() == 0) {
runMockServer(); // responds to test requests or times out, then returns
exit(0);
}
// Connect to MockServ and Run actual test here
END_TEST()
but I'm worried that this will screw up the testing framework.
Is this safe? Has anyone done something like this?
I'm using Boost 1.34.1 on Ubuntu 8.04 if that matters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在类似的情况下使用了 Boost 单元测试库,并取得了积极的结果。我想要进行自动测试,看看一个库在分叉时是否能正常工作。尽管在我的例子中它也更接近于系统测试,但如果可用的工具能够实现您想要的效果,我完全赞成使用它们。
不过,需要克服的一个障碍是在不使用 boost 断言宏的情况下从子进程发出错误信号。例如,如果使用
BOOST_REQUIRE
,它将提前中止测试,并且任何后续测试都将在父进程和子进程中执行。我最终使用进程退出代码向等待的父进程发出错误信号。但是,请勿使用exit()
因为 boost 有atexit()
钩子即使子进程中没有错误,也会发出错误信号。请改用_exit()
。我用于测试的设置是这样的。
I have used the Boost Unit Test library in similar circumstances with positive results. I wanted to have automatic tests to see if a library worked as it should when forking. Although it was also closer to a system test in my case I'm all for using available tools if they achieve what you want.
One obstacle to overcome though is to signal errors from the child process without using boost assert macros. If e.g.
BOOST_REQUIRE
would be used it would prematurely abort the test and any subsequent tests would be executed in both parent and child processes. I ended up using the process exit code to signal error to the waiting parent process. However, don't useexit()
as boost haveatexit()
hooks that signal errors in the child process even if there are none. Use_exit()
instead.The setup I used for tests was something like this.
这听起来并不像是针对您想要实现的目标进行单元测试。虽然我不明白为什么它不安全。如果单元测试尚未准备好,您可能会遇到竞争条件,即单元测试连接到 MockServ,但这很容易解决。
我从来没有直接做过这样的事情,但是我已经为 fork/exec 子进程的库编写了单元测试,并且它可以完美地工作。
This doesn't really sound like unit-testing for what you want to achieve. Though I don't see why it would not be safe. You may have a race condition where your unit test connects to the MockServ if it isn't ready yet, but that is easily solvable.
I've never done something like this directly, but I have written unit tests for libraries that fork/exec child processes and it works flawlessly.