Skip to content

Commit 096ae7b

Browse files
committed
test: Add more tests to increase coverage in the tests/test_plugin.py file.
1 parent f35c81a commit 096ae7b

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

tests/test_plugin.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
from pytest_brightest.plugin import (
66
BrightestPlugin,
7+
_get_brightest_data,
78
_plugin,
89
pytest_addoption,
910
pytest_collection_modifyitems,
1011
pytest_configure,
12+
pytest_runtest_logreport,
1113
pytest_sessionfinish,
1214
)
1315

@@ -145,6 +147,22 @@ def test_configure_shuffle_with_direction_warning(
145147
":high_brightness: pytest-brightest: Warning: --reorder-in-direction is ignored when --reorder-by-technique is 'shuffle'"
146148
)
147149

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+
148166

149167
def test_brightestplugin_record_test_failure():
150168
"""Test recording test failures in BrightestPlugin."""
@@ -201,6 +219,20 @@ def test_pytest_collection_modifyitems(
201219
mock_plugin.reorder_tests.assert_called_once_with(items)
202220
mock_plugin.shuffle_tests.assert_called_once_with(items)
203221

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+
204236
def test_pytest_sessionfinish_no_json_file(self, mocker, mock_config):
205237
"""Test that pytest_sessionfinish handles no JSON file."""
206238
_ = mock_config
@@ -327,3 +359,37 @@ def test_pytest_sessionfinish_failure_module_counts(
327359
"module_b.py": 2,
328360
"module_c.py": 0,
329361
}
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

Comments
 (0)