【发布时间】:2020-06-17 10:47:32
【问题描述】:
我是 python 中 Pytest 的新手。
我面临一个棘手的场景,我需要使用 Pytest 模块测试退出代码 - exit(1) 和 exit(0) 。 下面是python程序:
def sample_script():
count_file = 0
if count_file == 0:
print("The count of files is zero")
exit(1)
else:
print("File are present")
exit(0)
现在我想测试上述程序的退出代码 exit(1) 和 exit(0) 。使用 Pytest 我们如何构建测试代码以便我们可以测试或资产函数 sample_script 的退出代码?
请帮帮我。
【问题讨论】:
-
@Martin Prikryl,你能帮我解决这个问题吗?
-
为什么不将
exit()替换为assert()或raise()? -
@Martin Prikryl,你能帮我解决这个问题吗?
-
@Jens ,我们只需要测试退出代码。函数 sample_script () 有 exit code 、 exit(1) 和 exit(0) 。使用 Pytest ,我们需要一个测试程序来测试 sample_script () 的退出代码
-
你的 Python 函数没有意义,它总是会以代码 1 退出,永远不会到达 else 分支。
标签: python python-3.x function pytest exit-code