pythonpytestparametrized-testing

Parametrizing with class method, then calling that method with object


(pseudo-code) I have class Node with properties:

class Node(WebElement):

    def __init__(self, element):
        super().__init__(element)
        ...

    @property
    def name(self):
        <returns name>

    @property
    def node_id(self):
        <returns node_id>

    @property
    def device_type(self):
        <returns device type>

and so on... And i need to create test, that will check if Node property is not None (has to be parametrized, management is quantity biased)

Easy way is to use getattr

@pytest.mark.parametrize("property", ["name" ,"node_id" ,"device_type"])
def test_01_device_information(self, property):
    property = request.param
    ...
    for node in nodes:
         assert device.getattr(node, property) is not None

but drawback is, that this is dynamic reflection, and test will know if property exist in runtime. IDE syntaxer won't figure out missing property. Tests run trough night...

I can make a list of properties like (and syntaxer will find if any is gone):

        @pytest.mark.parametrize("property", [Node.name ,Node.node_id ,Node.device_type])

Is there a way to call that on object 'node' in that for loop? I know, I can call __name__ on property, but also I think there might be better ,or different solution. I'd be thankful for suggestions! :)


Solution

  • You can do:

    for node in nodes:
        assert property.fget(node) is not None