Python中pytest的参数化实例解析
pytest的参数化
参数化多个参数:
可以使用多个参数来参数化测试。例如:
1 2 3 4 5 6 7 8 | import pytest @pytest .mark.parametrize( "x, y, expected" , [ ( 1 , 2 , 3 ), ( 3 , 4 , 7 ), ( 5 , 6 , 11 ), ]) def test_addition(x, y, expected): assert x + y = = expected |
参数化列表:
可以使用列表来参数化测试。例如:
1 2 3 4 5 6 7 8 | import pytest @pytest .mark.parametrize( "test_input, expected_output" , [ ([ 1 , 2 , 3 ], 6 ), ([ 4 , 5 , 6 ], 15 ), ([ 7 , 8 , 9 ], 24 ), ]) def test_sum(test_input, expected_output): assert sum (test_input) = = expected_output |
参数化字典:
可以使用字典来参数化测试。例如:
1 2 3 4 5 6 7 8 | import pytest @pytest .mark.parametrize( "test_input, expected_output" , [ ({ "x" : 1 , "y" : 2 }, 3 ), ({ "x" : 3 , "y" : 4 }, 7 ), ({ "x" : 5 , "y" : 6 }, 11 ), ]) def test_addition(test_input, expected_output): assert test_input[ "x" ] + test_input[ "y" ] = = expected_output |
参数化文件:
可以使用文件来参数化测试。例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 | import pytest import csv def read_csv(): with open ( 'testdata.csv' , 'r' ) as f: reader = csv.reader(f) rows = [] for row in reader: rows.append(row) return rows[ 1 :] @pytest .mark.parametrize( "test_input, expected_output" , read_csv()) def test_addition(test_input, expected_output): x, y = map ( int , test_input.split( ',' )) assert x + y = = int (expected_output) |
动态参数化:
可以使用 Python 代码动态生成参数。例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import pytest import time def get_test_data(): test_data = [] start_time = time.time() while time.time() - start_time < 10 : # 运行时间小于 10 秒 x = random.randint( 1 , 100 ) y = random.randint( 1 , 100 ) expected = x + y test_data.append((x, y, expected)) return test_data @pytest .mark.parametrize( "x, y, expected" , get_test_data()) def test_addition(x, y, expected): assert x + y = = expected |
从外部数据源加载数据:
可以使用动态参数化从外部数据源加载测试数据,例如数据库、API 或其他 Web 服务。例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import pytest import requests def get_test_data(): response = requests.get( 'https://api.example.com/data' ) test_data = [] for item in response.json(): x = item[ 'x' ] y = item[ 'y' ] expected = item[ 'expected' ] test_data.append((x, y, expected)) return test_data @pytest .mark.parametrize( "x, y, expected" , get_test_data()) def test_addition(x, y, expected): assert x + y = = expected |
在上面的例子中,get_test_data 函数使用 requests 库从远程 API 加载测试数据,并返回一个测试数据列表。然后,使用 @pytest.mark.parametrize
装饰器动态参数化测试,使用从 API 加载的测试数据作为参数。
组合参数:
可以使用 itertools
库中的 product 函数生成参数的所有组合。例如:
1 2 3 4 5 | import pytest import itertools @pytest .mark.parametrize( "x, y" , itertools.product([ 1 , 2 , 3 ], [ 4 , 5 , 6 ])) def test_multiplication(x, y): assert x * y = = y * x |
在上面的例子中,使用 itertools.product
函数生成 x 和 y 的所有组合,并将它们作为参数传递给测试函数。
参数化生成器:
可以使用生成器函数生成参数。例如:
1 2 3 4 5 6 7 8 9 10 11 | import pytest import random def get_test_data(): while True : x = random.randint( 1 , 100 ) y = random.randint( 1 , 100 ) expected = x + y yield (x, y, expected) @pytest .mark.parametrize( "x, y, expected" , get_test_data()) def test_addition(x, y, expected): assert x + y = = expected |
到此这篇关于Python中pytest的参数化实例解析的文章就介绍到这了,更多相关pytest的参数化实例解析内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

微信公众号搜索 “ 脚本之家 ” ,选择关注
程序猿的那些事、送书等活动等着你
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 reterry123@163.com 进行投诉反馈,一经查实,立即处理!
相关文章
OpenCV python sklearn随机超参数搜索的实现
这篇文章主要介绍了OpenCV python sklearn随机超参数搜索的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-01-01Python HTMLTestRunner可视化报告实现过程解析
这篇文章主要介绍了Python HTMLTestRunner可视化报告实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2020-04-04
最新评论