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

python+pytest参数化的两种方法

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

python+pytest参数化的两种方法

方法一:

在测试用例前加上
@pytest.mark.parametrize(“参数名”,列表数据)
参数名:用来接收每一项数据,并作为测试用例的参数。
列表数据:一组测试数据。
示例:

@pytest.mark.parametrize("account, password", [('abc', '123456')])
def test_login(self,account, password):
    Course_Page.follow()    
    Login_Page.login(account,password)
方法二:

yaml文件存储数据
1、安装 pip inatall PyYaml
2、新建yaml文件

2、新建account.py 读取到yaml文件下的数据

import os
import yaml

cur_path = os.path.dirname(os.path.realpath(__file__))
configPath= os.path.join(cur_path,'account.yaml')
file=open(configPath)
# YAML 5.1版本后弃用了yaml.load(file)这个用法,需要指定Loader,通过默认加载​​器(FullLoader)禁止执行任意函数,该load函数也变得更加安全
temp=yaml.load(file,Loader=yaml.FullLoader)
user1= temp['user']['user_id']
password1 = temp['user']['password']

3、在方法中调用account.py文件的参数

import pytest
from step.login import Login_Page
from data.account import *

class Test_Login:

    def setup_class():
        print("------->测试登录")


    def test_login(self,account=user1, password=password1):
        Login_Page.login(account,password)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/348181.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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