Best or proper way to pass python object into pytest #9117
-
Hi, I'm invoking pytest via the
As you can see, the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
The simplest way to pass a object in is a plugin which provides a fixture Plugins can be a instance of a class Methods can be marked as fixtures |
Beta Was this translation helpful? Give feedback.
-
To exemplify what @RonnyPfannschmidt said, something like this: # then: in main test code
import pytest
import object_reference
@attr.define
class MyPlugin:
_ref: object_reference.ObjRef
@pytest.fixture
def ref(self) -> object_reference.ObjRef:
return self._ref
class TestExecutor:
# has lots of methods and properties
def __init__(self):
self.comm = SomeCommThing()
def execute(self):
ref = object_reference.ObjRef(self.comm)
pytest.main([...], plugins = [MyPlugin(ref)]) Now your tests have access to a |
Beta Was this translation helpful? Give feedback.
To exemplify what @RonnyPfannschmidt said, something like this:
Now your tests have access to a
ref
fixture.