通过读取yaml文件获得多个参数

import pytest
import requests
from utils.read_yaml import get_yaml_data
# 多个参数(’class1,class2‘,[('age','eat'),('age','eat')])
@pytest.mark.parametrize('title,body,userId',get_yaml_data()['list_test'])
def test_params(title,body,userId):
    HOST = 'ip'
    URL = '/posts/'
    url = HOST + URL
    json = {
        "title": title,
        "body": body,
        "userId": userId
    }

    res = requests.post(url, json=json)
    print(res.text)

    print(res.status_code)
    assert res.status_code==201
import yaml
def get_yaml_data():
    with open('../configs/params.yaml') as fo:
        return yaml.safe_load(fo)
name:
  - fqs
  - doudou # name:[fqs,doudou]
animal:
  dog:
    age: 3
    eat: meat
  cat:
    age: 2
    eat: fish # animal:{dog:{age:3,eat:meat}{cat:{age:2,eat:fish}}}
key:
  - value1
    value2
    value3 # key:[value1,value2,value3]
json_test:
  title: foo
  body: bar
  userId: 1
list_test:
  - foo
  - bar
  - 1

 进阶版  可以产生3条用例

import pytest
import requests
from utils.read_yaml import get_yaml_data
# 多个参数(’class1,class2‘,[('age','eat'),('age','eat')])
@pytest.mark.parametrize('title,body,userId',get_yaml_data()['lis'])
def test_params(title,body,userId):
    HOST = 'ip'
    URL = '/posts/'
    url = HOST + URL
    json = {
        "title": title,
        "body": body,
        "userId": userId
    }

    res = requests.post(url, json=json)

    assert res.status_code==201
name:
  - fqs
  - doudou # name:[fqs,doudou]
animal:
  dog:
    age: 3
    eat: meat
  cat:
    age: 2
    eat: fish # animal:{dog:{age:3,eat:meat}{cat:{age:2,eat:fish}}}
key:
  - value1
    value2
    value3 # key:[value1,value2,value3]
json_test:
  title: foo
  body: bar
  userId: 1
list_test:
  - foo
  - bar
  - 1
lis:
  - [foo,bar,1]
  - [foo1,bar1,11]
  - [foo2,bar2,12]
============================= test session starts =============================
collecting ... collected 3 items

test_params.py::test_params[foo-bar-1] 
test_params.py::test_params[foo1-bar1-11] 
test_params.py::test_params[foo2-bar2-12] 

============================== 3 passed in 3.50s ==============================

Process finished with exit code 0
PASSED                            [ 33%]PASSED                         [ 66%]PASSED                         [100%]

 

posted @ 2023-08-02 21:16  胖豆芽  阅读(125)  评论(0编辑  收藏  举报