0

In the below script i would like to parametrize functions call RegisterClientCabinMovementDetection(x) and RegisterClientOccupantInSeatDetection(x) (made bold in script)so on... is there any way to parametrize function in pytest ?

@pytest.mark.asyncio
@pytest.mark.parametrize('Qf, expected',[(UNDEFINED,"Invalid"),(INPROGRESS,"InProgress"),(NOTSPECIFIED,"NotOk"),(DATAOK,"DataOK")])
@pytest.mark.parametrize('Detection, expected_Detection',[(UNDEFINED,"Undefined"),(NOTDETECTED,"NotDetected"),(DETECTED,"Detected")])
@pytest.mark.parametrize('AvailabilityStatus,expected_availablity',[(UNDEFINED,"Undefined"),(NOTAVAILABLE,"NotAvailable"),(AVAILABLE,"Available"),(FAULT,"Fault"),(POSITIONNOTPRESENT,"PositionNotPresent")])
async def test_occupant_presence(snok,Qf,expected,Detection,expected_Detection,AvailabilityStatus,expected_availablity):
    """ Function to test occupant presence condition with driver available available scenario"""
    #setup
    occupant_presence_mock = snok.get_service("occupant_presence_hal")
    occupant_presence_service = snok.get_service("OccupantPresenceProvider")
    #Act
    await occupant_presence_service.**RegisterClientCabinMovementDetection(0)**
    #await occupant_presence_service.**RegisterClientOccupantInSeatDetection(0)**
    snok.async_sleep(1)
    seat_sensor_status =driver_not_detected_in_seat(occupant_presence_mock,Qf,Detection,AvailabilityStatus)
    await occupant_presence.set_DriverSeatCabinMovementDetection(seat_sensor_status)
    snok.async_sleep(1)
    response = await occupant_presence_service.get_DriverSeatCabinMovementDetection()
    #response = await occupant_presence_service.get_DriverSeatOccupantInSeatDetection()
    response_quality_factor=str(response.quality_factor)
    quality_factor_value = response_quality_factor.split('.')
    presence_detection_value= str(response.presence_detection).split('.')
    detector_status_value = str(response.detector_status).split('.')
    #assert
    assert quality_factor_value[1] == expected
    assert presence_detection_value[1] == expected_Detection
    assert detector_status_value[1] == expected_availablity

Unable to parametrize function call so code is going much bigger . Is there any way to reduce the lines of code using parametrize function in pytest ?

valentinmk
  • 597
  • 7
  • 18

1 Answers1

0

In your case you need to get method by name via getattr.

You can do this like this:

import pytest

class Some:
    def __init__(self, a=1):
        self.a = a
    def methodA(self, a):
        self.a = a
    def methodB(self, b):
        self.a = b * 10

@pytest.mark.parametrize(
    'method_name, param, expected',
    [('methodA', 2, 2),
     ('methodB', 3, 30),
    ]
)
def test(method_name, param, expected):
    obj = Some()
    getattr(obj, method_name)(param)
    assert obj.a == expected

Resulting of pytest -vs test.py will be like:

pytest -vs test.py
=================================================================================== test session starts ====================================================================================
platform darwin -- Python 3.10.8, pytest-7.3.1, pluggy-1.0.0 -- /opt/homebrew/Caskroom/miniforge/base/bin/python3.10
cachedir: .pytest_cache
rootdir: /Users/
plugins: pgsql-1.1.2, anyio-3.6.1, asyncio-0.21.0, xdoctest-1.1.1, xdist-3.2.1, typeguard-2.13.3, cov-4.0.0, web3-6.2.0, requests-mock-1.6.0
asyncio: mode=strict
collected 2 items                                                                                                                                                                          

test.py::test[methodA-2-2] PASSED
test.py::test[methodB-3-30] PASSED

==================================================================================== 2 passed in 0.01s =====================================================================================
valentinmk
  • 597
  • 7
  • 18