How can I use attributes in parametrize in pytest with tests organized in classes?
import pytest
class TestA:
@pytest.fixture(autouse=True)
def set_up(self):
self.field1 = "field1"
self.field2 = "field2"
@pytest.mark.parametrize("field", (self.field1, self.field2))
def test_print_field(self, field):
print(field, flush=False)
I'm getting NameError: name 'self' is not defined.
Probably not the best practice but here's something that can do the work:
@pytest.mark.parametrize("field", ("field1", "field2"))
def test_print_field(self, field):
print(eval(f"self.{field}"), flush=False)
You might want to use fixtures instead to set up and tear down.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With