Coverage for tests/test_mod.py: 100%
46 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-10-25 11:05 +0200
« prev ^ index » next coverage.py v6.5.0, created at 2022-10-25 11:05 +0200
1"""
2Tests of :mod:`pyyc.mod` functions, to be run with :pypi:`pytest`::
4 $ pytest -v test_mod.py
6Ideally, each function and use case should be tested: standard use (as
7described in documentation), invalid use (handled with documented exceptions),
8corner cases, etc.
10* :doc:`@pytest.mark.parametrize <pytest:how-to/parametrize>` decorator allows
11 to test different inputs/outputs;
12* :doc:`capsys <pytest:how-to/capture-stdout-stderr>` captures standard
13 and error outputs (e.g. from :func:`print`);
14* :doc:`monkeypatch <pytest:how-to/monkeypatch>` overrides modules and
15 environments (e.g. :func:`input`).
17.. Note:: Some tiny parts of the code are voluntarily left untested to
18 be used as example in `coverage report`.
19"""
21import pytest
22import pyyc
24def test_version():
25 """
26 Simple test.
27 """
29 assert pyyc.mod.version == "top-level module"
31@pytest.mark.parametrize("test_input, expected_output",
32 [
33 # Test on int args
34 ([1], 1), ([1, 2], 3), ([1, 2, 3], 6),
35 # Test on str args
36 (['abc',], 'abc'), (['abc', 'def'], 'abcdef'),
37 # Test on list args
38 ([[1, 2]], [1, 2]), ([[1, 2], [3, 4]], [1, 2, 3, 4]),
39 ])
40def test_addition(test_input, expected_output):
41 """
42 Test standard usage of :func:`pyyc.mod.addition`.
44 This test uses parametrization of arguments, see
45 :doc:`pytest:how-to/parametrize`.
47 .. Tip:: `addition(*args)` will unpack the arguments on-the-fly and
48 is similar to `addition(args[0], args[1], ...)`.
49 """
51 assert pyyc.mod.addition(*test_input) == expected_output
53@pytest.mark.parametrize("test_input", [(1, "toto"), ("toto", 1)])
54def test_addition_TypeError(test_input):
55 """
56 Test incompatible argument case.
58 It should raise :exc:`TypeError`, as mentioned in documentation of
59 :func:`pyyc.mod.addition`.
60 """
62 with pytest.raises(TypeError):
63 pyyc.mod.addition(*test_input)
65def test_addition_empty():
66 """
67 Test no argument case.
69 It should raise :exc:`IndexError`, not documented.
70 """
72 with pytest.raises(IndexError):
73 pyyc.mod.addition()
75@pytest.mark.parametrize("test_input, expected_output",
76 [([1], 1), ([1.2, 2.3], 3), ([1, 2, 3], 6)])
77def test_addition_int(test_input, expected_output):
78 """
79 Test standard usage of :func:`pyyc.mod.addition_int`.
80 """
82 assert pyyc.mod.addition_int(*test_input) == expected_output
84@pytest.mark.parametrize("test_input", [['1.2'], ["abc"]])
85def test_addition_int_ValueError(test_input):
86 """
87 Test non-int argument case.
89 It should raise :exc:`ValueError`, as mentioned in documentation of
90 :func:`pyyc.mod.addition_int`.
91 """
93 with pytest.raises(ValueError):
94 pyyc.mod.addition_int(*test_input)
96def test_read_config_version():
97 """
98 Test a single value in configuration file.
99 """
101 cfg = pyyc.mod.read_config()
102 assert cfg['DEFAULT']['version'] == 'cfg-1.0'
104def test_read_config_content(capsys):
105 """
106 Test full content of configuration file.
108 This test uses stdout capture, see :doc:`pytest:how-to/capture-stdout-stderr`.
109 """
111 import sys
113 cfg = pyyc.mod.read_config()
114 captured = capsys.readouterr() # Capture standard & error outputs (not used)
115 cfg.write(sys.stdout) # Write config to stdout
116 captured = capsys.readouterr() # Capture standard & error outputs
117 assert captured.out == "[DEFAULT]\nversion = cfg-1.0\n\n"
119def test_read_config_filename():
120 """
121 Test explicit configuration filename.
122 """
124 cfg = pyyc.mod.read_config(pyyc.mod.PYYC_PATH.joinpath("default.cfg"))
125 assert cfg['DEFAULT']['version'] == 'cfg-1.0'
127def test_read_config_IOError():
128 """
129 Test non-existing or invalid configuration files.
130 """
132 with pytest.raises(IOError):
133 pyyc.mod.read_config("nonexisting.cfg") # Non-existing file
135def test_format_pkg_tree():
136 """
137 Test standard usage of :func:`pyyc.mod.format_pkg_tree`.
138 """
140 s = pyyc.mod.format_pkg_tree(pyyc, max_depth=1)
141 assert s == ['pyyc',
142 ' pyyc.config',
143 ' pyyc.mod',
144 ' pyyc.subpkgA',
145 ' pyyc.subpkgB']
147def test_greetings(capsys, monkeypatch):
148 """
149 This test uses both input monkey patching and stdout capture.
150 """
152 import builtins
154 # Create a fake 'input' function for the test
155 monkeypatch.setattr(builtins, 'input', lambda _: "John")
157 pyyc.mod.greetings() # Use fake input, and print on stdout
159 captured = capsys.readouterr() # Capture standard & error outputs
160 assert captured.out == "Hello, John!\n"