列表
#! /usr/bin/env python
# -*- coding:utf-8 -*-
# author: Test
import pytest
def add(a,b):
return a+b
def data():
return [
[1,1,2],
['wuya','Share','wuyaShare'],
[1.0,1.0,2.0]
]
@pytest.mark.parametrize('a,b,result',data())
def test_add_list(a,b,result):
assert add(a=a,b=b) == result
if __name__=='__main__':
pytest.main(['-v',"main.py"])
元組
#! /usr/bin/env python
# -*- coding:utf-8 -*-
# author: Test
import pytest
def add(a,b):
return a+b
def data():
return [
(1,1,2),
('wuya','Share','wuyaShare'),
(1.0,1.0,2.0)
]
@pytest.mark.parametrize('a,b,result',data()) # 調(diào)用data函數(shù)勒庄,分別賦值給a,b,result
def test_add_tuple(a,b,result): # a,b,result作為參數(shù)傳入
assert add(a=a,b=b) == result
if __name__=='__main__':
pytest.main(['-v',"main.py"])
字典
#! /usr/bin/env python
# -*- coding:utf-8 -*-
# author: Test
import pytest
def add(a,b):
return a+b
def data():
return [
{'a':1,'b':1,'result':2},
{'a':'wuya','b':'Share','result':'wuyaShare'},
{'a':1.0,'b':1.0,'result':2.0}
]
@pytest.mark.parametrize('data',data()) # 調(diào)用data函數(shù)串前,分別賦值給data
def test_add_dict(data): # data['a'],data['b'],data['result']作為參數(shù)傳入
assert add(a=data['a'],b=data['b']) == data['result']
if __name__=='__main__':
pytest.main(['-s','-v',"main.py"])
JSON
# add.json
{
"datas":
[
{"a":1,"b":1,"result":2},
{"a":"wuya","b":"Share","result":"wuyaShare"},
{"a":1.0,"b":1.0,"result":2.0}
]
}
# main.py
#! /usr/bin/env python
# -*- coding:utf-8 -*-
# author: Test
import pytest
import json
def add(a,b):
return a+b
def readJson():
return json.load(open('add.json'))['datas']
@pytest.mark.parametrize('data',readJson()) # 調(diào)用data函數(shù)瘫里,分別賦值給data
def test_add_dict(data): # data['a'],data['b'],data['result']作為參數(shù)傳入
assert add(a=data['a'],b=data['b']) == data['result']
if __name__=='__main__':
pytest.main(['-s','-v',"main.py"])
Yaml
# add.yml
---
# two integer add
a: 1
b: 1
result: 2
---
# two string add
a: wuya
b: Share
result: wuyaShare
---
# two float add
a: 1.0
b: 1.0
result: 2.0
# main.py
#! /usr/bin/env python
# -*- coding:utf-8 -*-
# author: Test
import pytest
import yaml
def add(a,b):
return a+b
def readYaml():
with open('add.yml') as f:
return list(yaml.safe_load_all(f))
@pytest.mark.parametrize('data',readYaml()) # 調(diào)用data函數(shù),分別賦值給data
def test_add_dict(data): # data['a'],data['b'],data['result']作為參數(shù)傳入
assert add(a=data['a'],b=data['b']) == data['result']
if __name__=='__main__':
pytest.main(['-s','-v',"main.py"])
request.param
#! /usr/bin/env python
# -*- coding:utf-8 -*-
# author: Test
import pytest
def add(a,b):
return a+b
def data():
return [
[1,1,2],
['wuya','Share','wuyaShare'],
[1.0,1.0,2.0]
]
@pytest.fixture(params=data())
def param(request):
return request.param
def test_add_request(param):
assert add(a=param[0],b=param[1]) == param[2]
if __name__=='__main__':
pytest.main(['-v',"main.py"])