Coverage for tests/test_mod.py: 100%

46 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-10-11 23:51 +0200

1""" 

2Tests of :mod:`pyyc.mod` functions, to be run with :pypi:`pytest`:: 

3 

4 $ pytest -v test_mod.py 

5 

6Ideally, each function and use case should be tested: standard use (as 

7described in documentation), invalid use (handled with documented exceptions), 

8corner cases, etc. 

9 

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`). 

16 

17.. Note:: Some tiny parts of the code are voluntarily left untested to 

18 be used as example in `coverage report`. 

19""" 

20 

21import pytest 

22import pyyc 

23 

24def test_version(): 

25 """ 

26 Simple test. 

27 """ 

28 

29 assert pyyc.mod.version == "top-level module" 

30 

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`. 

43 

44 This test uses parametrization of arguments, see 

45 :doc:`pytest:how-to/parametrize`. 

46 

47 .. Tip:: `addition(*args)` will unpack the arguments on-the-fly and 

48 is similar to `addition(args[0], args[1], ...)`. 

49 """ 

50 

51 assert pyyc.mod.addition(*test_input) == expected_output 

52 

53@pytest.mark.parametrize("test_input", [(1, "toto"), ("toto", 1)]) 

54def test_addition_TypeError(test_input): 

55 """ 

56 Test incompatible argument case. 

57 

58 It should raise :exc:`TypeError`, as mentioned in documentation of 

59 :func:`pyyc.mod.addition`. 

60 """ 

61 

62 with pytest.raises(TypeError): 

63 pyyc.mod.addition(*test_input) 

64 

65def test_addition_empty(): 

66 """ 

67 Test no argument case. 

68 

69 It should raise :exc:`IndexError`, not documented. 

70 """ 

71 

72 with pytest.raises(IndexError): 

73 pyyc.mod.addition() 

74 

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 """ 

81 

82 assert pyyc.mod.addition_int(*test_input) == expected_output 

83 

84@pytest.mark.parametrize("test_input", [['1.2'], ["abc"]]) 

85def test_addition_int_ValueError(test_input): 

86 """ 

87 Test non-int argument case. 

88 

89 It should raise :exc:`ValueError`, as mentioned in documentation of 

90 :func:`pyyc.mod.addition_int`. 

91 """ 

92 

93 with pytest.raises(ValueError): 

94 pyyc.mod.addition_int(*test_input) 

95 

96def test_read_config_version(): 

97 """ 

98 Test a single value in configuration file. 

99 """ 

100 

101 cfg = pyyc.mod.read_config() 

102 assert cfg['DEFAULT']['version'] == 'cfg-1.0' 

103 

104def test_read_config_content(capsys): 

105 """ 

106 Test full content of configuration file. 

107 

108 This test uses stdout capture, see :doc:`pytest:how-to/capture-stdout-stderr`. 

109 """ 

110 

111 import sys 

112 

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" 

118 

119def test_read_config_filename(): 

120 """ 

121 Test explicit configuration filename. 

122 """ 

123 

124 cfg = pyyc.mod.read_config(pyyc.mod.PYYC_PATH.joinpath("default.cfg")) 

125 assert cfg['DEFAULT']['version'] == 'cfg-1.0' 

126 

127def test_read_config_IOError(): 

128 """ 

129 Test non-existing or invalid configuration files. 

130 """ 

131 

132 with pytest.raises(IOError): 

133 pyyc.mod.read_config("nonexisting.cfg") # Non-existing file 

134 

135def test_format_pkg_tree(): 

136 """ 

137 Test standard usage of :func:`pyyc.mod.format_pkg_tree`. 

138 """ 

139 

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'] 

146 

147def test_greetings(capsys, monkeypatch): 

148 """ 

149 This test uses both input monkey patching and stdout capture. 

150 """ 

151 

152 import builtins 

153 

154 # Create a fake 'input' function for the test 

155 monkeypatch.setattr(builtins, 'input', lambda _: "John") 

156 

157 pyyc.mod.greetings() # Use fake input, and print on stdout 

158 

159 captured = capsys.readouterr() # Capture standard & error outputs 

160 assert captured.out == "Hello, John!\n"