小编Mec*_*ran的帖子

在 Python 3 中,使用 Pytest,我们如何测试 Python 程序的退出代码:exit(1) 和 exit(0)?

我是 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)
Run Code Online (Sandbox Code Playgroud)

现在我想测试上述程序的退出代码 exit(1) 和 exit(0) 。使用 Pytest 我们如何构建测试代码,以便我们可以测试或资产函数 sample_script 的退出代码?

请帮我。

python function exit-code pytest python-3.x

3
推荐指数
1
解决办法
2763
查看次数

标签 统计

exit-code ×1

function ×1

pytest ×1

python ×1

python-3.x ×1