python - 如何在 pytest 中迭代 test_function 以获得多个值?
问题描述
我想知道如何在 pytest 中迭代 test_funtion() 以获得不同的值?例如。
list = ['ls','ps', 'df' ,'du'] #list of Linux commands
def test_method(self):
for I in list:
r=subprocess.check_output(I)
if r:
assert True
else:
assert False
现在,当我运行 pytest -k test_method 时,它显示只有一个测试用例通过。但我希望所有 4 个案例都使用单个函数运行,并且需要在输出中传递 4 个测试用例。我怎样才能实现它?
解决方案
扩展@Nithin-Mohan 先前的回答
commands = ['ls','ps', 'df' ,'du'] #list of Linux commands
@pytest.mark.parametrize("cmds",commands)
def test_method(cmds):
r=subprocess.check_output(cmds)
if r:
assert True
else:
assert False
这是作为 4 个单独测试运行的输出
test_sflow.py::test_method[ls] PASSED [ 25%]
test_sflow.py::test_method[ps] PASSED [ 50%]
test_sflow.py::test_method[df] PASSED [ 75%]
test_sflow.py::test_method[du] PASSED [ 100%]
推荐阅读
- javascript - Vue 中清除事件的 md-autocomplete
- javascript - 使用 MediaRecorder 以 MP4 格式从网络摄像头录制视频
- angular - 角度日期选择器,没有 ngModel 无法设置默认日期
- javascript - 字符串中不包含“图像”的 URL 的正则表达式
- python - Pywinauto:如何打开菜单然后打开文件?
- javascript - 如何在javascript中修复“错误:产生ENAMETOOLONG”(可能由jq引起)?
- azure - 使用 Azure Functions/Powershell 应用 PnP 预配模板
- javascript - 在 JSON 资源文件中注释 Node.js 和相关项目
- codeigniter - 我想在函数读取中添加一些给定的代码
- php - realpath() 不显示 realpath GitLab ci runner