pytest之通过yaml实现数据驱动

一、安装pyyaml

pip install pyyaml

二、yaml基本语法

略过

三、将测试用例数据写入yaml

#用例1
-
 interfaceName: 首屏-考试数据接口
 url: http://stupad-stre****
 headers: {
  'Content-Type': 'application/json',
  'requestid': '021618985865',
  'token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiI4OTg2MyIsInN5c3RlbUlkIjoiODE5NTEwNTU3NDEiLCJvcmdJZCI6IjgwIiwidGltZXN0YW1wIjoiMTYxODk4NTcwODkyOCJ9.rTSoq0rwvCcHNLXiwKCa76_Vw_i926BuMoFrl1Tv0Q0'
 }
 data: {
  "type": 1
 }


#用例2
-
 interfaceName: 首屏-考试数据接口
 url: http://stupad-stres****
 headers: {
  'Content-Type': 'application/json',
  'requestid': '021618985865',
  'token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiI4OTg2MyIsInN5c3RlbUlkIjoiODE5NTEwNTU3NDEiLCJvcmdJZCI6IjgwIiwidGltZXN0YW1wIjoiMTYxODk4NTcwODkyOCJ9.rTSoq0rwvCcHNLXiwKCa76_Vw_i926BuMoFrl1Tv0Q0'
 }
 data: {
  "type": 2
 }


#用例3
-
 interfaceName: 首屏-考试数据接口
 url: http://stupad-str****
 headers: {
  'Content-Type': 'application/json',
  'requestid': '021618985865',
  'token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiI4OTg2MyIsInN5c3RlbUlkIjoiODE5NTEwNTU3NDEiLCJvcmdJZCI6IjgwIiwidGltZXN0YW1wIjoiMTYxODk4NTcwODkyOCJ9.rTSoq0rwvCcHNLXiwKCa76_Vw_i926BuMoFrl1Tv0Q0'
 }
 data: {
  "type": 3
 }

四、封装读取yaml的方法

  1. 创建ymal_tuil.py文件
  2. 封装:用_init_()方法来传入文件,再通过yaml.load()来进行反序列化
import yaml

class yamlUtil():
    def __init__(self,yaml_file):

        '''
        通过init把文件传入到这个类
        :param yaml_file:
        '''
        self.yaml_file = yaml_file

    #读取ymal文件
    def read_yaml(self):
        '''
        读取yaml,将yaml反序列化,就是把我们yaml格式转换成dict格式
        :return:
        '''
        with open(self.yaml_file,encoding="utf-8") as f:
            value = yaml.load(f,Loader=yaml.FullLoader) #文件流,加载方式
            print(value)


if __name__ == '__main__':
    yamlUtil("./interface.yaml").read_yaml()

运行结果:

{'interfaceName': '首屏-考试数据接口', 'url': 'http://stupad-st****', 'header': {'Content-Type': 'application/json', 'requestid': '021618985865', 'token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiI4OTg2MyIsInN5c3RlbUlkIjoiODE5NTEwNTU3NDEiLCJvcmdJZCI6IjgwIiwidGltZXN0YW1wIjoiMTYxODk4NTcwODkyOCJ9.rTSoq0rwvCcHNLXiwKCa76_Vw_i926BuMoFrl1Tv0Q0'}, 'data': {'type': 2}}
  • 1

五、通过@pytest.mark.parametrize()实现yaml的数据驱动

import pytest
import os
from common.yaml_util import yamlUtil

class Test_yaml():

    @pytest.mark.parametrize("args",yamlUtil(os.getcwd()+"/testcase/test_yaml/interface.yaml").read_yaml())
    def test_yaml(self,args):
        # print(args)
        interfaceName = args['interfaceName']
        url = args["url"]
        headers = args["headers"]

        assert 2 == args["data"]["type"]

 

运行结果:

===================================================================================== test session starts ======================================================================================
platform darwin -- Python 3.9.4, pytest-6.2.3, py-1.10.0, pluggy-0.13.1 -- /Users/dongshuai/venv/python3.9/bin/python
cachedir: .pytest_cache
metadata: {'Python': '3.9.4', 'Platform': 'macOS-11.2.3-x86_64-i386-64bit', 'Packages': {'pytest': '6.2.3', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'variables': '1.9.0', 'rerunfailures': '9.1.1', 'html': '3.1.1', 'xdist': '2.2.1', 'metadata': '1.11.0', 'allure-pytest': '2.8.40', 'ordering': '0.6', 'forked': '1.3.0'}}
rootdir: /Users/dongshuai/PycharmProjects/ui-test, configfile: pytest.ini, testpaths: /Users/dongshuai/PycharmProjects/ui-test
plugins: variables-1.9.0, rerunfailures-9.1.1, html-3.1.1, xdist-2.2.1, metadata-1.11.0, allure-pytest-2.8.40, ordering-0.6, forked-1.3.0
collected 1 item                                                                                                                                                                                

testcase/test_yaml/Test_yaml.py::Test_yaml::test_yaml[args0] PASSED

======================================================================================= 1 passed in 0.06s =======================================================================================
(python3.9) (base) dongshuai@dongshuaideMacBook-Air ui-test %
 
posted @ 2022-04-25 16:58  平行时空的旅者  阅读(272)  评论(0编辑  收藏  举报