2024-06-09 09:09:45 发布
网友
click是一个python包,用于为应用程序创建良好的命令行接口。我一直在玩click,今天在github上推了这个simple罗马数字转换器。
我现在要做的是测试我的点击应用程序。我正在阅读文档,但不知道如何运行测试。
有没有人有测试点击应用程序的经验?
pytest具有断言的处理程序。
pytest
若要对现有脚本运行测试,必须将其“导入”。
import click from click.testing import CliRunner from click_app import configure, cli def test_command_configure(): runner = CliRunner() result = runner.invoke(cli, ["configure"]) assert result.exit_code == 0 assert result.output == 'configure'
将下面的代码放入test_greet.py:
test_greet.py
import click from click.testing import CliRunner def test_greet(): @click.command() @click.argument('name') def greet(name): click.echo('Hello %s' % name) runner = CliRunner() result = runner.invoke(greet, ['Sam']) assert result.output == 'Hello Sam\n' if __name__ == '__main__': test_greet()
如果只是用python test_greet.py调用,则测试通过,并且没有显示任何内容。在测试框架中使用时,它会按预期执行。例如nosetests test_greet.py返回
python test_greet.py
nosetests test_greet.py
. ---------------------------------------------------------------------- Ran 1 test in 0.002s OK
pytest
具有断言的处理程序。若要对现有脚本运行测试,必须将其“导入”。
将下面的代码放入
test_greet.py
:如果只是用
python test_greet.py
调用,则测试通过,并且没有显示任何内容。在测试框架中使用时,它会按预期执行。例如nosetests test_greet.py
返回相关问题 更多 >
编程相关推荐