1

I have some fixtures that just return the objects of a class. The objects contain a property of that class which I am trying to verify in pytest, so the code looks something like this:

from main import My_class
import pytest

@pytest.fixture()
def fixt1():
    object1 = My_class("atr1", "atr2")
    return object1

@pytest.fixture()
def fixt2():
    object2 = My_class("atr3", "atr4")
    return object2

@pytest.mark.parametrize('inputs, results',
                         [
                            (fixt1, 10.0),
                            (fixt2, 22.5)
                         ]
                         )
def test_fixts(inputs, results):
    assert inputs.price == results

This code will always return Attribute error:

AttributeError: 'function' object has no attribute 'price'

However, attempting to test these attributes with simple tests like this will work:

def test_simple(fixt1):
    assert fixt1.price == 10.0

Can I get some advice on this issue, as I couldn't find instructions on how to properly call objects attributes in parametrize anywhere?

Much appreciated!

MrBean Bremen
  • 14,916
  • 3
  • 26
  • 46
Massina_CF
  • 13
  • 3
  • You cannot use a fixture as a parameter value - just use a function instead. Fixtures have to be used as test arguments like in your `test_simple` example. – MrBean Bremen Sep 17 '20 at 17:17
  • Thanks for that @MrBeanBremen! I noticed that fixtures don't work with parametrize as you mentioned, but the strange thing is that functions don't work either. It does work if I just initiate the object that I want to use outside the body a function/fixture, like this: `object1 = My_class("atr1", "atr2")`, but not if it's returned by a function – Massina_CF Sep 18 '20 at 14:12
  • Functions should work if they have all the needed information at load time (not methods, because they are not known at load time). – MrBean Bremen Sep 18 '20 at 14:23
  • you are right! It was my bad, I was not naming my functions properly. – Massina_CF Sep 20 '20 at 20:30
  • Sorry @MrBeanBremen, I am a noob, I want to mark your answer as the solution but the "mark beside the answer" simply doesn't appear for me to toggle it. – Massina_CF Sep 20 '20 at 20:38

1 Answers1

1

Fixtures cannot be used like functions - they have to be used as arguments in test functions or in other fixtures. In the parameters of mark.parametrize you can use normal functions instead:

def param1():
    return My_class("atr1", "atr2")

def param2():
    return My_class("atr3", "atr4")

@pytest.mark.parametrize('inputs, results',
                         [
                            (param1(), 10.0),
                            (param2(), 22.5)
                         ]
                         )
def test_fixts(inputs, results):
    assert inputs.price == results

Note that you can only use functions that can be executed at load time, because the mark.parametrize decorator is evaluated at load time, so it cannot depend on any run-time changes.

MrBean Bremen
  • 14,916
  • 3
  • 26
  • 46