|
4 | 4 |
|
5 | 5 | from pytest_brightest.plugin import ( |
6 | 6 | BrightestPlugin, |
| 7 | + _get_brightest_data, |
7 | 8 | _plugin, |
8 | 9 | pytest_addoption, |
9 | 10 | pytest_collection_modifyitems, |
10 | 11 | pytest_configure, |
| 12 | + pytest_runtest_logreport, |
11 | 13 | pytest_sessionfinish, |
12 | 14 | ) |
13 | 15 |
|
@@ -145,6 +147,22 @@ def test_configure_shuffle_with_direction_warning( |
145 | 147 | ":high_brightness: pytest-brightest: Warning: --reorder-in-direction is ignored when --reorder-by-technique is 'shuffle'" |
146 | 148 | ) |
147 | 149 |
|
| 150 | + def test_configure_json_report_setup_fails(self, mock_config, mocker): |
| 151 | + """Test that a warning is issued when json report setup fails.""" |
| 152 | + mock_console_print = mocker.patch( |
| 153 | + "pytest_brightest.plugin.console.print" |
| 154 | + ) |
| 155 | + mocker.patch( |
| 156 | + "pytest_brightest.plugin.setup_json_report_plugin", |
| 157 | + return_value=False, |
| 158 | + ) |
| 159 | + plugin = BrightestPlugin() |
| 160 | + config = mock_config({"--brightest": True}) |
| 161 | + plugin.configure(config) |
| 162 | + mock_console_print.assert_any_call( |
| 163 | + ":high_brightness: pytest-brightest: pytest-json-report setup failed, reordering features disabled" |
| 164 | + ) |
| 165 | + |
148 | 166 |
|
149 | 167 | def test_brightestplugin_record_test_failure(): |
150 | 168 | """Test recording test failures in BrightestPlugin.""" |
@@ -201,6 +219,20 @@ def test_pytest_collection_modifyitems( |
201 | 219 | mock_plugin.reorder_tests.assert_called_once_with(items) |
202 | 220 | mock_plugin.shuffle_tests.assert_called_once_with(items) |
203 | 221 |
|
| 222 | + def test_pytest_runtest_logreport(self, mocker): |
| 223 | + """Test that pytest_runtest_logreport records failures.""" |
| 224 | + mock_plugin = mocker.patch( |
| 225 | + "pytest_brightest.plugin._plugin", autospec=True |
| 226 | + ) |
| 227 | + mock_plugin.enabled = True |
| 228 | + mock_plugin.technique = "failure" |
| 229 | + report = mocker.MagicMock() |
| 230 | + report.failed = True |
| 231 | + report.when = "call" |
| 232 | + report.nodeid = "test_node" |
| 233 | + pytest_runtest_logreport(report) |
| 234 | + mock_plugin.record_test_failure.assert_called_once_with("test_node") |
| 235 | + |
204 | 236 | def test_pytest_sessionfinish_no_json_file(self, mocker, mock_config): |
205 | 237 | """Test that pytest_sessionfinish handles no JSON file.""" |
206 | 238 | _ = mock_config |
@@ -327,3 +359,37 @@ def test_pytest_sessionfinish_failure_module_counts( |
327 | 359 | "module_b.py": 2, |
328 | 360 | "module_c.py": 0, |
329 | 361 | } |
| 362 | + |
| 363 | + |
| 364 | +def test_get_brightest_data_all_branches(mocker, mock_test_item): |
| 365 | + """Test _get_brightest_data for all branches.""" |
| 366 | + mock_plugin = mocker.patch( |
| 367 | + "pytest_brightest.plugin._plugin", autospec=True |
| 368 | + ) |
| 369 | + mock_session = mocker.MagicMock() |
| 370 | + # technique: cost, Focus: modules-within-suite |
| 371 | + mock_plugin.technique = "cost" |
| 372 | + mock_plugin.focus = "modules-within-suite" |
| 373 | + mock_plugin.reorderer = mocker.MagicMock() |
| 374 | + mock_plugin.reorderer.get_test_total_duration.return_value = 1.0 |
| 375 | + mock_session.items = [mock_test_item("mod1::test1")] |
| 376 | + data = _get_brightest_data(mock_session) |
| 377 | + assert "current_module_costs" in data |
| 378 | + # technique: cost, Focus: tests-within-module |
| 379 | + mock_plugin.focus = "tests-within-module" |
| 380 | + data = _get_brightest_data(mock_session) |
| 381 | + assert "current_test_costs" in data |
| 382 | + # technique: name, Focus: modules-within-suite |
| 383 | + mock_plugin.technique = "name" |
| 384 | + mock_plugin.focus = "modules-within-suite" |
| 385 | + mock_session.items = [mock_test_item("mod1::test1")] |
| 386 | + data = _get_brightest_data(mock_session) |
| 387 | + assert "current_module_order" in data |
| 388 | + # technique: name, Focus: tests-across-modules |
| 389 | + mock_plugin.focus = "tests-across-modules" |
| 390 | + data = _get_brightest_data(mock_session) |
| 391 | + assert "current_test_order" in data |
| 392 | + # technique: name, Focus: tests-within-module |
| 393 | + mock_plugin.focus = "tests-within-module" |
| 394 | + data = _get_brightest_data(mock_session) |
| 395 | + assert "current_module_tests" in data |
0 commit comments