Skip to content

Best or proper way to pass python object into pytest #9117

Answered by nicoddemus
AndrewFalanga asked this question in Q&A
Discussion options

You must be logged in to vote

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 ref fixture.

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@tilak-trinity
Comment options

Answer selected by AndrewFalanga
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants