Issue
I am learning how to use pytest by testing a simple event emitter implementation.
Basically, it looks like this
class EventEmitter():
def __init__(self):
...
def subscribe(self, event_map):
# adds listeners to provided in event_map events
def emit(self, event, *args):
# emits event with given args
For convenience, I created a Listener
class that is used in tests
class Listener():
def __init__(self):
...
def operation(self):
# actual listener
Currently, test looks the following way
@pytest.fixture
def event():
ee = EventEmitter()
lstr = Listener()
ee.subscribe({"event" : [lstr.operation]})
return lstr, ee
def test_emitter(event):
lstr = event[0]
ee = event[1]
ee.emit("event")
assert lstr.result == 7 # for example
In order to test event emitter, I need to check whether the inner state of the listener has changed after event propagation. Thus, I need two objects and I wonder if there is a better way to do this (maybe use two fixtures instead of one somehow) because this looks kinda ugly to me.
Solution
You will probably need two fixtures in this case.
You can try the @pytest.yield_fixture
like:
@pytest.yield_fixture
def event():
...
yield <event_properties>
@pytest.yield_fixture
def listener(event):
...
yield <listener_properties>
Note: this is now deprecated https://docs.pytest.org/en/latest/yieldfixture.html
Answered By - Ederson Badeca
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.