仅在系统进程完成运行后才进行 PHPUnit 断言
我正在尝试测试后台 shell 进程(使用 exec() 运行)的结果。为此,我需要 PHPUnit 断言来等待进程结束。
我想知道最好的方法是什么?我是否应该将对正在运行的进程的检查放在循环中,并使用带有非常小的参数的 sleep() 来等待它完成?
I'm trying to test the result of a background shell process (ran with exec()). For this, I need the PHPUnit assertion to wait until the process ends.
I was wondering what is the best way to do this? Should I place the check for the running process in a loop, and use sleep() with a very small parameter to wait until it's finished?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于一天没有人回答,我要尝试一下:
第一个冲动的答案是:“你不应该这样做”。它与单元测试的目标并不相符(例如运行该测试并不是很快)。
如果您正在为调用该脚本的类编写测试,那么您实际上并不是在测试一件事,而是在测试两件事。调用命令行脚本的类,该脚本是它自己的,您可以模拟 shell 部分。
如果您只是想确保 exec 正常工作,我的第一个想法是以一种不会将 shell 进程作为后台任务而是作为前台任务生成的方式构建您的测试,并消除一些“等待”的需要解决方法。
希望有一点帮助
Since no one answered for a day i'm going to take a shot:
The first impulsive Answer would be: "You shouldn't do that". It doesn't really match with the goals of Unittesting (e.g. it's not really fast to run that test).
If you're writing a Test for a Class that calls that Script your not really testing one thing but two. The Class that calls the command line Script and that Script it's self and you could mock out the shell part.
If you're just trying to make sure that exec works my first idea would be to build your Test in a way that doesn't spawn that shell process as a background Task but as a Foreground Task and eliminating the need for some "wait" workaround.
Hope that helped a litte