craft 命令测试
命令测试
您可以使用craft
测试助手测试在控制台中运行的 自定义命令。
def test_my_command(self):
self.craft("my_command", "arg1 arg2").assertSuccess()
如果该命令已在您的项目中注册,这将以编程方式运行该命令,并断言未报告任何错误。
可用断言
使用craft
测试命令时,可以使用以下断言。
- assertSuccess
- assertHasErrors
- assertOutputContains
- assertExactOutput
- assertOutputMissing
- assertExactErrors
assertSuccess
断言该命令以代码 0 退出,这意味着它运行成功。
self.craft("my_command").assertSuccess()
assertHasErrors
断言命令输出有错误。
self.craft("my_command").assertHasErrors()
assertOutputContains
断言命令输出包含给定的字符串。
self.craft("my_command").assertOutputContains(output)
assertExactOutput
断言命令输出与给定的参考输出完全相同。使用此断言方法时要小心添加最终的行尾字符。
self.craft("my_command").assertExactOutput(output)
assertOutputMissing
断言命令输出不包含给定的参考输出。
self.craft("my_command").assertOutputMissing(output)
assertExactErrors
断言命令输出完全具有给定的错误。
self.craft("my_command").assertExactErrors(errors)
本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。