pytest参数化的两种方式

1、传统方式

 1 import requests,pytest
 2 from Learning.variable import *
 3 
 4 # 定义变量
 5 #url = "https://www.baidu.com"
 6 
 7 class TestClass(object):
 8     global url    #在此获取全局变量,并将其设置为TestClass类的全局变量
 9     def setup_class(self):
10         print("start...")
11 
12     def test_get(self):
13         #global url    #在此获取全局变量,并将其设置为test_get方法内的全局变量
14         res = requests.get(url=url)
15         assert res.status_code == 200
16 
17 
18 if __name__ == '__main__':
19     pytest.main()

2、pytest推荐模式,即conftest测试数据共享

2.1、在function中使用

# content of test01.py

import  pytest,requests

#将conftest中的com_variable方法传入用例中,不需要导入即可使用
def test_getBaidu(com_variable):
    a=requests.get(com_variable['url'])
    code = a.status_code
    assert code == 200
def test_paas():
    pass
# content of conftest.py
import pytest

iaas={
    'url':'https://www.baidu.com',

}

@pytest.fixture(scope="module")
def com_variable():
    return iaas

 2.2、在class中使用

conftest不变,直接在class中的方法入参中传入即可

import requests,pytest
from Learning.variable import *

# 定义变量
#url = "https://www.baidu.com"

class TestClass(object):
    #global url    #在此获取全局变量,并将其设置为TestClass类的全局变量
    def setup_class(self):
        print("start...")
    #直接在此传入即可
    def test_get(self,com_variable):
        #global url    #在此获取全局变量,并将其设置为test_get方法内的全局变量
        res = requests.get(url=com_variable['url'])
        assert res.status_code == 200


if __name__ == '__main__':
    pytest.main()

 

推荐第二种方式!

 

转载于:https://www.cnblogs.com/phyger/p/11366620.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值