其他分享
首页 > 其他分享> > pytest 参数化

pytest 参数化

作者:互联网

  1. params传入需要的list

```
@pytest.fixture(params=is_leap)
def is_leap(self, request):
return request.param
```
- params 参数,当前参数在'request.param'中可用。
- test测试用例中直接调用is_leap方法
- pytest.fixture()中传入的参数为list,用例执行时,遍历list中的值,每传入一次值,则相当于执行一次用例。
- ps:@pytest.fixture()装饰的函数中,传入了一个参数为request

import is_leap_year
import pytest


class TestPara():
is_leap = [4, 40, 400, 800, 1992, 2996]
is_not_leap = [1, 100, 500, 1000, 1999, 3000]
is_valueerror = [0, -4, -100, -1996, -2000]
is_typeerror = ['-4', '4', '100', 'ins', '**', '中文']

# params传入需要的list
@pytest.fixture(params=is_leap)
def is_leap(self, request):
return request.param
# params: an optional list of parameters which will cause multiple
# invocations of the fixture function and all of the tests using it.
# The current parameter is available in ``request.param``.
# params 参数,当前参数在'request.param'中可用。
# def get_direct_param_fixture_func(request):
# return request.param

@pytest.fixture(params=is_typeerror)
def is_typeerror(self, request):
return request.param

def test_is_leap(self, is_leap):
assert is_leap_year.is_leap_year(is_leap) == True

def test_is_typeerror(self, is_typeerror):
with pytest.raises(TypeError):
is_leap_year.is_leap_year(is_typeerror)


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

# conftest.py文件存储参数化数据和函数,模块下的用例执行时,会自动读取conftest.py文件中的数据
- conftest.py

import pytest

is_leap = [4, 40, 400, 800, 1992, 2996]
is_not_leap = [1, 100, 500, 1000, 1999, 3000]
is_valueerror = [0, -4, -100, -1996, -2000]
is_typeerror = ['-4', '4', '100', 'ins', '**', '中文']


# params传入需要的list
@pytest.fixture(params=is_leap)
def is_leap_y(request):
return request.param

@pytest.fixture(params=is_typeerror)
def is_type_error(request):
return request.param
```

- test_para.py
- 直接调用is_leap_y方法
```
import is_leap_year
import pytest


class TestPara:
def test_is_leap(self, is_leap_y):
assert is_leap_year.is_leap_year(is_leap_y) == True

def test_is_typeerror(self, is_type_error):
with pytest.raises(TypeError):
is_leap_year.is_leap_year(is_type_error)


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

# pytest.mark.parametrize()方式进行参数化
- 采用标记函数参数化,传入单个参数,pytest.mark.parametrize("参数名",lists)

import is_leap_year
import pytest


class TestPara():
is_leap = [4, 40, 400, 800, 1992, 2996]
is_typeerror = ['-4', '4', '100', 'ins', '**', '中文']

@pytest.mark.parametrize('year', is_leap)
def test_is_leap(self, year):
assert is_leap_year.is_leap_year(year) == True

@pytest.mark.parametrize('year', is_typeerror)
def test_is_typeerror(self, year):
with pytest.raises(TypeError):
is_leap_year.is_leap_year(year)

- 采用标记函数传入多个参数,如pytest.mark.parametrize("para1, para2", [(p1_data_0, p2_data_0), (p1_data_1, p2_data_1),...]

- 测试用例中传入2个参数,year和期望结果,使输入数据与预期结果对应,构造了2组会失败的数据,在执行结果中,可以看到失败原因:

import is_leap_year
import pytest


class TestPara():

value = [(4, True), (40, True), (400, True), (800, True), (1992, True), (2996, True)]

@pytest.mark.parametrize('year, assert_value', value)
def test_is_leap(self, year, assert_value):
assert is_leap_year.is_leap_year(year) == assert_value

value_error = [(0, ValueError), ('-4', TypeError), (-4, ValueError), ('ss', TypeError), ('中文', TypeError), ('**', TypeError)]

@pytest.mark.parametrize('year,assert_value',value_error)
def test_is_typeerror(self,year, assert_value):
if assert_value == ValueError:
with pytest.raises(ValueError) as excinfo:
is_leap_year.is_leap_year(year)
assert excinfo.type == assert_value
else:
with pytest.raises(TypeError) as excinfo:
is_leap_year.is_leap_year(year)
assert excinfo.type == assert_value


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

 

标签:request,assert,leap,pytest,参数,year,def
来源: https://www.cnblogs.com/jescs/p/12107765.html