Skip to content

Commit

Permalink
Add two testing utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
mraspaud committed May 8, 2023
1 parent 908a7eb commit ddfa066
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
24 changes: 24 additions & 0 deletions posttroll/testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Testing utilities."""
from contextlib import contextmanager

@contextmanager
def patched_subscriber_recv(messages):
"""Patch the Subscriber object to return given messages."""
from unittest import mock
with mock.patch("posttroll.subscriber.Subscriber.recv", mock.Mock(return_value=messages)):
yield

@contextmanager
def patched_publisher():
"""Patch the Subscriber object to return given messages."""
from unittest import mock
published = []

def fake_send(self, message):
published.append(message)

def noop(self, *args, **kwargs):
pass

with mock.patch.multiple("posttroll.publisher.Publisher", send=fake_send, start=noop, stop=noop):
yield published
11 changes: 11 additions & 0 deletions posttroll/tests/test_testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from posttroll.testing import patched_publisher

def test_fake_publisher():
from posttroll.publisher import create_publisher_from_dict_config

with patched_publisher() as messages:
pub = create_publisher_from_dict_config(dict(port=1979, nameservers=False))
pub.start()
pub.send("bla")
pub.stop()
assert "bla" in messages

0 comments on commit ddfa066

Please sign in to comment.