PHPUnit用于命令行集成测试

时间:2022-10-15 22:09:30

Recently, I've started to working on Email2SMS feature in our product. When I joined the project this component had zero code coverage by unit-tests. Legacy code.

最近,我开始在我们的产品中使用Email2SMS功能。当我加入项目时,这个组件的单元测试代码覆盖率为零。遗留代码。

Since I started to working on it, I was using test-first approach. But code quality was very low. It was very hard to split it and tests small chunks by unit-tests, so I decided to write integration test.

自从我开始研究它以来,我一直在使用测试优先方法。但代码质量非常低。拆分它并通过单元测试测试小块是非常困难的,所以我决定编写集成测试。

There is php script, which accepts some message info, search for user in DB and save some info if everything is OK.

有php脚本,它接受一些消息信息,在DB中搜索用户并保存一些信息,如果一切正常。

$last_line = system('php emailtosms.php -file=unicode_message.txt ', $retval);

$this->assertStringExistsInLogFile('Email to SMS message was not sent');

Is it bad or not? How would you solve this problem?

这不好吗?你怎么解决这个问题?

1 个解决方案

#1


Not so good.

不太好。

Test at a lower level, by having the emailtosms.php script as a simple wrapper that processes the command line args, and then pass off to more testable class. Depending on the final step, you could write unit tests to do almost every but the final send (maybe mock the final step that actually sends it, and just store it to a variable, then check you've got something valid there).

通过将emailtosms.php脚本作为处理命令行args的简单包装器,然后传递给更可测试的类,在较低级别进行测试。根据最后一步,您可以编写单元测试来完成除最终发送之外的几乎所有操作(可能模拟实际发送它的最后一步,并将其存储到变量中,然后检查您是否在那里有效)。

The final integration, runs the full class end-to-end, and check its output, looking at the final log - or a little higher-level, what would be put into to log.

最后的集成,端到端运行完整的类,并检查其输出,查看最终日志 - 或更高级别,将记录什么。

#1


Not so good.

不太好。

Test at a lower level, by having the emailtosms.php script as a simple wrapper that processes the command line args, and then pass off to more testable class. Depending on the final step, you could write unit tests to do almost every but the final send (maybe mock the final step that actually sends it, and just store it to a variable, then check you've got something valid there).

通过将emailtosms.php脚本作为处理命令行args的简单包装器,然后传递给更可测试的类,在较低级别进行测试。根据最后一步,您可以编写单元测试来完成除最终发送之外的几乎所有操作(可能模拟实际发送它的最后一步,并将其存储到变量中,然后检查您是否在那里有效)。

The final integration, runs the full class end-to-end, and check its output, looking at the final log - or a little higher-level, what would be put into to log.

最后的集成,端到端运行完整的类,并检查其输出,查看最终日志 - 或更高级别,将记录什么。