在下面的脚本中,我想参数化函数调用 RegisterClientCabinMovementDetection(x) 和 RegisterClientOccupantInSeatDetection(x) (在脚本中以粗体显示)等等...有没有办法在 pytest 中参数化函数?
@pytest.mark.asyncio @pytest.mark.parametrize('Qf,预期',[(UNDEFINED,"无效"),(INPROGRESS,"进行中"),(未指定,"NotOk"),(DATAOK,"DataOK")]) @pytest.mark.parametrize('检测,expected_Detection',[(UNDEFINED,"未定义"),(NOTDETECTED,"未检测到"),(DETECTED,"检测到")]) @pytest.mark.parametrize('AvailabilityStatus,expected_availablity',[(UNDEFINED,"未定义"),(NOTAVAILABLE,"不可用"),(AVAILABLE,"可用"),(FAULT,"故障"),(POSITIONNOTPRESENT,"位置不存在")]) 异步def test_occupant_presence(snok,Qf,预期,检测,expected_Detection,可用性状态,expected_availability): """在驾驶员可用的情况下测试乘员存在状况的功能""" #设置 ocupant_presence_mock = snok.get_service(“ocupant_presence_hal”) accupant_presence_service = snok.get_service("OccupantPresenceProvider") #行为 等待 occupant_presence_service。 RegisterClientCabinMovementDetection(0) #await occupant_presence_service。 RegisterClientOccupantInSeatDetection(0) snok.async_sleep(1) Seat_sensor_status = driver_not_Detected_in_seat(占用者_存在_模拟,Qf,检测,可用性状态) 等待 occupant_presence.set_DriverSeatCabinMovementDetection(seat_sensor_status) snok.async_sleep(1) 响应=等待 occupant_presence_service.get_DriverSeatCabinMovementDetection() #response = 等待 habitant_presence_service.get_DriverSeatOccupantInSeatDetection() 响应质量因子=str(响应.质量因子) 质量因子值 = 响应质量因子.split('.') 存在检测值= str(response.存在检测).split('.') detector_status_value = str(response. detector_status).split('.') #断言 断言quality_factor_value[1] ==预期 断言存在_检测_值[1] ==预期_检测 断言 detector_status_value[1] == Expected_availability
无法参数化函数调用,因此代码会变得更大。有没有办法在 pytest 中使用参数化函数来减少代码行数?
可以使用
pytest.mark.parametrize
将函数名称作为字符串传递,然后在测试函数中使用 getattr() 函数调用相应的函数。
以下是修改后的代码示例:
import pytest
@pytest.mark.asyncio
@pytest.mark.parametrize('Qf, expected', [(UNDEFINED, "Invalid"), (INPROGRESS, "InProgress"), (UNSPECIFIED, "NotOk"), (DATAOK, "DataOK")])
@pytest.mark.parametrize('Detection, expected_Detection', [(UNDEFINED, "Undefined"), (NOTDETECTED, "NotDetected"), (DETECTED, "Detected")])
@pytest.mark.parametrize('AvailabilityStatus, expected_availability', [(UNDEFINED, "Undefined"), (NOTAVAILABLE, "NotAvailable"), (AVAILABLE, "Available"), (FAULT, "Fault"), (POSITIONNOTPRESENT, "PositionNotPresent")])
@pytest.mark.parametrize('function_name', ['RegisterClientCabinMovementDetection', 'RegisterClientOccupantInSeatDetection'])
async def test_occupant_presence(snok, Qf, expected, Detection, expected_Detection, AvailabilityStatus, expected_availability, function_name):
"""在驾驶员可用的情况下测试乘员存在状况的功能"""
# 设置
ocupant_presence_mock = snok.get_service("ocupant_presence_hal")
occupant_presence_service = snok.get_service("OccupantPresenceProvider")
# 行为
await occupant_presence_service
# 使用 getattr() 函数调用相应的函数
func = getattr(occupant_presence_service, function_name)
await func(0)
snok.async_sleep(1)
Seat_sensor_status = driver_not_Detected_in_seat(ocupant_presence_mock, Qf, Detection, AvailabilityStatus)
await occupant_presence_service.set_DriverSeatCabinMovementDetection(seat_sensor_status)
snok.async_sleep(1)
if function_name == 'RegisterClientCabinMovementDetection':
response = await occupant_presence_service.get_DriverSeatCabinMovementDetection()
else:
response = await occupant_presence_service.get_DriverSeatOccupantInSeatDetection()
quality_factor_value = str(response.QualityFactor).split('.')
presence_detection_value = str(response.PresenceDetection).split('.')
detector_status_value = str(response.DetectorStatus).split('.')
# 断言
assert quality_factor_value[1] == expected
assert presence_detection_value[1] == expected_Detection
assert detector_status_value[1] == expected_availability
在这个修改后的代码中:
-
我们添加了一个新的
pytest.mark.parametrize
装饰器来传递函数名称字符串列表:['RegisterClientCabinMovementDetection', 'RegisterClientOccupantInSeatDetection']
。 -
在测试函数中,我们使用
getattr(occupant_presence_service, function_name)
获取要调用的函数对象。 -
使用
await func(0)
调用获取的函数对象。 -
我们根据
function_name
的值调用不同的获取响应函数。
这样,就可以使用单个测试函数测试不同的函数调用,从而减少代码重复。
标签:python,pytest,parameterized,parameterized-unit-test,parametrize From: 76182709