Skip to content

Commit 961b82d

Browse files
committed
Make the tests use the pytest fixtures and assert system
Signed-off-by: Aurélien Bompard <[email protected]>
1 parent bdf7da9 commit 961b82d

14 files changed

+501
-523
lines changed

Diff for: tests/__init__.py

-26
Original file line numberDiff line numberDiff line change
@@ -1,26 +0,0 @@
1-
# This file is part of fedora_messaging.
2-
# Copyright (C) 2018 Red Hat, Inc.
3-
#
4-
# This program is free software; you can redistribute it and/or modify
5-
# it under the terms of the GNU General Public License as published by
6-
# the Free Software Foundation; either version 2 of the License, or
7-
# (at your option) any later version.
8-
#
9-
# This program is distributed in the hope that it will be useful,
10-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12-
# GNU General Public License for more details.
13-
#
14-
# You should have received a copy of the GNU General Public License along
15-
# with this program; if not, write to the Free Software Foundation, Inc.,
16-
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17-
import os
18-
19-
import crochet
20-
21-
22-
FIXTURES_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "fixtures/"))
23-
24-
# Tests use pytest-twisted, which manages the Twisted reactor for us. This stops
25-
# crochet from starting the reactor in its own thread.
26-
crochet.no_setup()

Diff for: tests/conftest.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# This file is part of fedora_messaging.
2+
# Copyright (C) 2018 Red Hat, Inc.
3+
#
4+
# This program is free software; you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation; either version 2 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License along
15+
# with this program; if not, write to the Free Software Foundation, Inc.,
16+
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17+
18+
import os
19+
20+
import crochet
21+
import pytest
22+
23+
24+
@pytest.fixture(autouse=True, scope="session")
25+
def crochet_no_setup():
26+
crochet.no_setup()
27+
28+
29+
@pytest.fixture
30+
def fixtures_dir():
31+
return os.path.abspath(os.path.join(os.path.dirname(__file__), "fixtures/"))

Diff for: tests/fixtures/good_conf.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
callback = "fedora_messaging.tests.unit.test_cli:echo"
1+
callback = "tests.unit.test_cli:echo"
22

33
[[bindings]]
44
exchange = "e"

Diff for: tests/integration/test_cli.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525
from twisted.internet import threads
2626

2727
from fedora_messaging import api, exceptions, message
28-
from fedora_messaging.tests import FIXTURES_DIR
2928

3029
from .utils import sleep
3130

3231

33-
CLI_CONF = os.path.join(FIXTURES_DIR, "cli_integration.toml")
32+
@pytest.fixture
33+
def cli_conf(fixtures_dir):
34+
return os.path.join(fixtures_dir, "cli_integration.toml")
3435

3536

3637
def halt_exit_0(message):
@@ -64,13 +65,13 @@ def queue(scope="function"):
6465
],
6566
)
6667
@pytest_twisted.inlineCallbacks
67-
def test_consume_halt_with_exitcode(callback, exit_code, msg, queue):
68+
def test_consume_halt_with_exitcode(callback, exit_code, msg, queue, cli_conf):
6869
"""Assert user execution halt with reason and exit_code is reported."""
6970
args = [
7071
"fedora-messaging",
71-
f"--conf={CLI_CONF}",
72+
f"--conf={cli_conf}",
7273
"consume",
73-
f"--callback=fedora_messaging.tests.integration.test_cli:{callback}",
74+
f"--callback=tests.integration.test_cli:{callback}",
7475
f"--queue-name={queue}",
7576
"--exchange=amq.topic",
7677
"--routing-key=#",

Diff for: tests/unit/test_api.py

+21-42
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def callback(self):
4747

4848
callback = api._check_callback(class_instance.callback)
4949

50-
self.assertIs(callback, callback)
50+
assert callback == class_instance.callback
5151

5252
def test_func(self):
5353
"""Assert functions are valid."""
@@ -57,7 +57,7 @@ def callback(message):
5757

5858
cb = api._check_callback(callback)
5959

60-
self.assertIs(cb, callback)
60+
assert cb is callback
6161

6262
def test_class(self):
6363
"""Assert classes are instantiated."""
@@ -68,18 +68,16 @@ def __call__(self, message):
6868

6969
callback = api._check_callback(Callback)
7070

71-
self.assertEqual(callback(None), "It worked")
71+
assert callback(None) == "It worked"
7272

7373
def test_class_no_call(self):
7474
"""Assert classes are instantiated."""
7575

7676
class Callback:
7777
pass
7878

79-
try:
79+
with pytest.raises(ValueError):
8080
api._check_callback(Callback)
81-
self.fail("_check_callback failed to raise an exception")
82-
except ValueError:
8381
pass
8482

8583
def test_class_init_args(self):
@@ -89,19 +87,13 @@ class Callback:
8987
def __init__(self, args):
9088
self.args = args
9189

92-
try:
90+
with pytest.raises(TypeError):
9391
api._check_callback(Callback)
94-
self.fail("_check_callback failed to raise a TypeError")
95-
except TypeError:
96-
pass
9792

9893
def test_not_callable(self):
9994
"""Assert a ValueError is raised for things that can't be called."""
100-
try:
95+
with pytest.raises(ValueError):
10196
api._check_callback("")
102-
self.fail("_check_callback failed to raise an ValueError")
103-
except ValueError:
104-
pass
10597

10698

10799
@mock.patch("fedora_messaging.api._twisted_service")
@@ -144,9 +136,8 @@ def test_bindings_dict(self, mock_service):
144136

145137
def test_bindings_invalid_type(self, mock_service):
146138
"""Assert bindings are validated and result in a value error if they are invalid."""
147-
self.assertRaises(
148-
ValueError, api.twisted_consume, self.dummy_callback, "test_bindings"
149-
)
139+
with pytest.raises(ValueError):
140+
api.twisted_consume(self.dummy_callback, "test_bindings")
150141

151142
def test_bindings_list_of_dict(self, mock_service):
152143
"""Assert consume is working(bindings type is dict)"""
@@ -160,13 +151,8 @@ def test_bindings_list_of_dict(self, mock_service):
160151

161152
def test_queues_invalid_type(self, mock_service):
162153
"""Assert queues are validated and result in a value error if they are invalid."""
163-
self.assertRaises(
164-
ValueError,
165-
api.twisted_consume,
166-
self.dummy_callback,
167-
None,
168-
"should be a dict",
169-
)
154+
with pytest.raises(ValueError):
155+
api.twisted_consume(self.dummy_callback, None, "should be a dict")
170156

171157
def test_with_queues(self, mock_service):
172158
"""Assert queues is used over the config if provided."""
@@ -264,11 +250,9 @@ def test_publish_to_exchange(self, mock_twisted_publish):
264250

265251
mock_twisted_publish.assert_called_once_with(message, exchange)
266252
mock_twisted_publish.return_value.wait.assert_called_once_with(timeout=30)
267-
self.assertEqual(self.pre_publish_signal_data, expected_pre_publish_signal_data)
268-
self.assertEqual(self.publish_signal_data, expected_publish_signal_data)
269-
self.assertEqual(
270-
self.publish_failed_signal_data, expected_publish_failed_signal_data
271-
)
253+
assert self.pre_publish_signal_data == expected_pre_publish_signal_data
254+
assert self.publish_signal_data == expected_publish_signal_data
255+
assert self.publish_failed_signal_data == expected_publish_failed_signal_data
272256

273257
@mock.patch.dict(
274258
"fedora_messaging.config.conf", {"publish_exchange": "test_public_exchange"}
@@ -295,11 +279,9 @@ def test_publish_to_config_exchange(self, mock_twisted_publish):
295279
api.publish(message)
296280

297281
mock_twisted_publish.assert_called_once_with(message, "test_public_exchange")
298-
self.assertEqual(self.pre_publish_signal_data, expected_pre_publish_signal_data)
299-
self.assertEqual(self.publish_signal_data, expected_publish_signal_data)
300-
self.assertEqual(
301-
self.publish_failed_signal_data, expected_publish_failed_signal_data
302-
)
282+
assert self.pre_publish_signal_data == expected_pre_publish_signal_data
283+
assert self.publish_signal_data == expected_publish_signal_data
284+
assert self.publish_failed_signal_data == expected_publish_failed_signal_data
303285

304286
def test_publish_failed(self, mock_twisted_publish):
305287
"""Assert an exception is raised when message can't be published."""
@@ -319,16 +301,13 @@ def test_publish_failed(self, mock_twisted_publish):
319301
}
320302
mock_twisted_publish.return_value.wait.side_effect = expected_exception
321303

322-
self.assertRaises(
323-
type(expected_exception), api.publish, message, exchange=exchange
324-
)
304+
with pytest.raises(type(expected_exception)):
305+
api.publish(message, exchange=exchange)
325306

326307
mock_twisted_publish.assert_called_once_with(message, exchange)
327-
self.assertEqual(self.pre_publish_signal_data, expected_pre_publish_signal_data)
328-
self.assertEqual(self.publish_signal_data, expected_publish_signal_data)
329-
self.assertEqual(
330-
self.publish_failed_signal_data, expected_publish_failed_signal_data
331-
)
308+
assert self.pre_publish_signal_data == expected_pre_publish_signal_data
309+
assert self.publish_signal_data == expected_publish_signal_data
310+
assert self.publish_failed_signal_data == expected_publish_failed_signal_data
332311

333312

334313
@pytest_twisted.inlineCallbacks

0 commit comments

Comments
 (0)