Skip to content

Commit c44604c

Browse files
authored
simplify test structure to be more inline with pytest recommendations (#140)
Signed-off-by: Matthew Peveler <matt.peveler@gmail.com>
1 parent ab3b318 commit c44604c

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

tests/test_tldr.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
import sys
22
import io
33
import types
4-
import unittest
5-
import unittest.mock
4+
from unittest import mock
65
import tldr
76
import pytest
87

98

10-
class TLDRTests(unittest.TestCase):
11-
def test_whole_page(self):
12-
with open("tests/data/gem.md", "rb") as f_original:
13-
with open("tests/data/gem_rendered", "rb") as f_rendered:
14-
old_stdout = sys.stdout
15-
sys.stdout = io.StringIO()
16-
sys.stdout.buffer = types.SimpleNamespace()
17-
sys.stdout.buffer.write = lambda x: sys.stdout.write(x.decode("utf-8"))
18-
tldr.output(f_original)
9+
def test_whole_page():
10+
with open("tests/data/gem.md", "rb") as f_original:
11+
with open("tests/data/gem_rendered", "rb") as f_rendered:
12+
old_stdout = sys.stdout
13+
sys.stdout = io.StringIO()
14+
sys.stdout.buffer = types.SimpleNamespace()
15+
sys.stdout.buffer.write = lambda x: sys.stdout.write(x.decode("utf-8"))
16+
tldr.output(f_original)
1917

20-
sys.stdout.seek(0)
21-
tldr_output = sys.stdout.read().encode("utf-8")
22-
sys.stdout = old_stdout
18+
sys.stdout.seek(0)
19+
tldr_output = sys.stdout.read().encode("utf-8")
20+
sys.stdout = old_stdout
2321

24-
correct_output = f_rendered.read()
25-
self.assertEqual(tldr_output, correct_output)
22+
correct_output = f_rendered.read()
23+
assert tldr_output == correct_output
2624

27-
def test_error_message(self):
28-
with unittest.mock.patch("sys.argv", ["tldr", "73eb6f19cd6f"]):
29-
with pytest.raises(SystemExit) as pytest_wrapped_e:
30-
tldr.main()
31-
correct_output = "`73eb6f19cd6f` documentation is not available. Consider contributing Pull Request to https://github.com/tldr-pages/tldr" # noqa
32-
print("Test {}".format(pytest_wrapped_e))
33-
assert pytest_wrapped_e.type == SystemExit
34-
assert str(pytest_wrapped_e.value) == correct_output
25+
26+
def test_error_message():
27+
with mock.patch("sys.argv", ["tldr", "73eb6f19cd6f"]):
28+
with pytest.raises(SystemExit) as pytest_wrapped_e:
29+
tldr.main()
30+
correct_output = "`73eb6f19cd6f` documentation is not available. Consider contributing Pull Request to https://github.com/tldr-pages/tldr" # noqa
31+
print("Test {}".format(pytest_wrapped_e))
32+
assert pytest_wrapped_e.type == SystemExit
33+
assert str(pytest_wrapped_e.value) == correct_output

0 commit comments

Comments
 (0)