|
12 | 12 | from testing.auto_namedtuple import auto_namedtuple |
13 | 13 |
|
14 | 14 |
|
| 15 | +FNS = ( |
| 16 | + 'autoupdate', 'clean', 'install', 'install_hooks', 'run', 'sample_config', |
| 17 | + 'uninstall', |
| 18 | +) |
| 19 | +CMDS = tuple(fn.replace('_', '-') for fn in FNS) |
| 20 | + |
| 21 | + |
15 | 22 | @pytest.yield_fixture |
16 | 23 | def mock_commands(): |
17 | | - with mock.patch.object(main, 'autoupdate') as autoupdate_mock: |
18 | | - with mock.patch.object(main, 'install_hooks') as install_hooks_mock: |
19 | | - with mock.patch.object(main, 'install') as install_mock: |
20 | | - with mock.patch.object(main, 'uninstall') as uninstall_mock: |
21 | | - with mock.patch.object(main, 'run') as run_mock: |
22 | | - with mock.patch.object(main, 'clean') as clean_mock: |
23 | | - yield auto_namedtuple( |
24 | | - autoupdate_mock=autoupdate_mock, |
25 | | - clean_mock=clean_mock, |
26 | | - install_mock=install_mock, |
27 | | - install_hooks_mock=install_hooks_mock, |
28 | | - uninstall_mock=uninstall_mock, |
29 | | - run_mock=run_mock, |
30 | | - ) |
| 24 | + mcks = {fn: mock.patch.object(main, fn).start() for fn in FNS} |
| 25 | + ret = auto_namedtuple(**mcks) |
| 26 | + yield ret |
| 27 | + for mck in ret: |
| 28 | + mck.stop() |
31 | 29 |
|
32 | 30 |
|
33 | 31 | class CalledExit(Exception): |
@@ -93,45 +91,10 @@ def test_help_other_command( |
93 | 91 | ]) |
94 | 92 |
|
95 | 93 |
|
96 | | -def test_install_command(mock_commands): |
97 | | - main.main(['install']) |
98 | | - assert mock_commands.install_mock.call_count == 1 |
99 | | - assert_only_one_mock_called(mock_commands) |
100 | | - |
101 | | - |
102 | | -def test_uninstall_command(mock_commands): |
103 | | - main.main(['uninstall']) |
104 | | - assert mock_commands.uninstall_mock.call_count == 1 |
105 | | - assert_only_one_mock_called(mock_commands) |
106 | | - |
107 | | - |
108 | | -def test_clean_command(mock_commands): |
109 | | - main.main(['clean']) |
110 | | - assert mock_commands.clean_mock.call_count == 1 |
111 | | - assert_only_one_mock_called(mock_commands) |
112 | | - |
113 | | - |
114 | | -def test_autoupdate_command(mock_commands): |
115 | | - main.main(['autoupdate']) |
116 | | - assert mock_commands.autoupdate_mock.call_count == 1 |
117 | | - assert_only_one_mock_called(mock_commands) |
118 | | - |
119 | | - |
120 | | -def test_run_command(mock_commands): |
121 | | - main.main(['run']) |
122 | | - assert mock_commands.run_mock.call_count == 1 |
123 | | - assert_only_one_mock_called(mock_commands) |
124 | | - |
125 | | - |
126 | | -def test_install_hooks_command(mock_commands): |
127 | | - main.main(('install-hooks',)) |
128 | | - assert mock_commands.install_hooks_mock.call_count == 1 |
129 | | - assert_only_one_mock_called(mock_commands) |
130 | | - |
131 | | - |
132 | | -def test_no_commands_run_command(mock_commands): |
133 | | - main.main([]) |
134 | | - assert mock_commands.run_mock.call_count == 1 |
| 94 | +@pytest.mark.parametrize('command', CMDS) |
| 95 | +def test_install_command(command, mock_commands): |
| 96 | + main.main((command,)) |
| 97 | + assert getattr(mock_commands, command.replace('-', '_')).call_count == 1 |
135 | 98 | assert_only_one_mock_called(mock_commands) |
136 | 99 |
|
137 | 100 |
|
|
0 commit comments