栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python pytest 参数化

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python pytest 参数化

pytest数据参数化 参数化使用

@pytest.mark.parametrize(argnames,argvalues)

argnames:要参数化的变量,string,list,tuple

argvalues:参数化的值,list,list[tuple]

使用string
import pytest
@pytest.mark.parametrize("a,b",[(10,20),(10,30)])
def test_case1(a,b):
    print(a+b)

if __name__ == '__main__':
    pytest.main(["-s","-v",r"E:pythoncodepython_test2021103_test01.py"])
使用tuple
import pytest
@pytest.mark.parametrize(("a","b"),[(10,20),(10,30)])
def test_case1(a,b):
    print(a+b)

if __name__ == '__main__':
    pytest.main(["-s","-v",r"E:pythoncodepython_test2021103_test01.py"])
使用list
import pytest

class Testdata:
    @pytest.mark.parametrize(["a","b"],[(10,20),(10,5),(10,7)])
    def test_demo1(self,a,b):
        a=10
        b=20
        print(a+b)

if __name__ == '__main__':
    pytest.main(["-s","-v",r"E:pythoncodepython_test2021103_test01.py"])
yaml的基本使用 安装 pyyaml 库
pip install pyyaml
yaml实现list
list
    - 10
    - 20
    - 30
yaml实现字典
dict
    by:id
    locator:name
    action:click
yaml进行嵌套
-
    - by:id
    - locator:name
    - action: click
companies:
    -
        id:1
        name:company1
        price:200W
    -
        id:2
        name:company2
        price:500W
加载yaml文件
yaml.safe_load(open("./data.yaml"))
pytest数据参数化加载yaml
@pytest.mark.parametrize(["a","b"],yaml.safe_load(open("./data.yaml")))
def test_param(self,a,b):
    print(a+b)
 创建data.yaml文件
-
  - 10
  - 10
-
  - 20
  - 10
-
  - 10
  - 30
import pytest
import yaml

class Testdata:
    @pytest.mark.parametrize(["a","b"],yaml.safe_load(open(r"E:pythoncodepython_testdata.yaml")))
    def test_demo1(self,a,b):
        print(a+b)

if __name__ == '__main__':
    pytest.main(["-s","-v",r"E:pythoncodepython_test2021103_test01.py"])

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/286325.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号