-
-
Notifications
You must be signed in to change notification settings - Fork 7
Added docstring as test case text #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jcm-mx
wants to merge
8
commits into
kiwitcms:master
Choose a base branch
from
jcm-mx:feature/docstrings_as_text
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cd8f7b0
Added docstring as test case text
jcm-mx 0f47391
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a8d2983
Apply suggestion from @atodorov
jcm-mx b523603
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 58d2c5d
Moved getting docstring where it makes more logical sense
cm-dynamic 98374e0
Fixed incorrect copyright year
cm-dynamic b11c024
Fixed mock that had no text attribute
cm-dynamic 9d5b476
Improved assertion to make sure we check for updated text for test_a …
cm-dynamic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # Copyright (c) 2026 | ||
| # Licensed under the GPLv3: https://www.gnu.org/licenses/gpl.html | ||
| # | ||
| # pylint: disable=redefined-outer-name | ||
|
|
||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import pytest | ||
|
|
||
| from tcms_pytest_plugin import Backend, KiwiTCMSPlugin | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def plugin(): | ||
| with patch("tcms_pytest_plugin.Backend") as mock_backend_cls: | ||
| instance = MagicMock() | ||
| instance.plan_id = 1 | ||
| instance.run_id = 1 | ||
| instance.test_case_get_or_create.return_value = ({"id": 99, "text": None}, True) | ||
| instance.add_test_case_to_run.return_value = [{"id": 42}] | ||
| mock_backend_cls.return_value = instance | ||
|
|
||
| p = KiwiTCMSPlugin() | ||
| p.backend = instance | ||
| yield p | ||
|
|
||
|
|
||
| def _make_item(nodeid, docstring): | ||
| """Create a minimal mock pytest item with a function docstring.""" | ||
| item = MagicMock() | ||
| item.nodeid = nodeid | ||
| item.function.__doc__ = docstring | ||
| return item | ||
|
|
||
|
|
||
| def _simulate_collection(plugin, items): | ||
| session = MagicMock() | ||
| session.items = items | ||
| plugin.pytest_collection_finish(session) | ||
|
|
||
|
|
||
| def _simulate_logstart(plugin, nodeid): | ||
| plugin.pytest_runtest_logstart(nodeid=nodeid, location=("", 0, "")) | ||
|
|
||
|
|
||
| def test_docstring_is_sent_as_test_case_text(plugin): | ||
| """Test case body must be set from the function docstring when present.""" | ||
| items = [ | ||
| _make_item( | ||
| "tests/test_foo.py::test_intent", "Verify the widget resets on power-up." | ||
| ) | ||
| ] | ||
| _simulate_collection(plugin, items) | ||
| _simulate_logstart(plugin, "tests/test_foo.py::test_intent") | ||
|
|
||
| plugin.backend.update_test_case_text.assert_called_once_with( | ||
| 99, "Verify the widget resets on power-up." | ||
| ) | ||
|
|
||
|
|
||
| def test_no_update_when_docstring_is_absent(plugin): | ||
| """update_test_case_text must not be called when the test has no docstring.""" | ||
| item = MagicMock() | ||
| item.nodeid = "tests/test_foo.py::test_no_doc" | ||
| item.function.__doc__ = None | ||
| _simulate_collection(plugin, [item]) | ||
| _simulate_logstart(plugin, "tests/test_foo.py::test_no_doc") | ||
|
|
||
| plugin.backend.update_test_case_text.assert_not_called() | ||
|
|
||
|
|
||
| def test_no_update_when_docstring_is_empty_string(plugin): | ||
| """update_test_case_text must not be called when the docstring is an empty string.""" | ||
| items = [_make_item("tests/test_foo.py::test_empty_doc", "")] | ||
| _simulate_collection(plugin, items) | ||
| _simulate_logstart(plugin, "tests/test_foo.py::test_empty_doc") | ||
|
|
||
| plugin.backend.update_test_case_text.assert_not_called() | ||
|
|
||
|
|
||
| def test_multiline_docstring_is_cleandoc_normalised(plugin): | ||
| """inspect.cleandoc must be used so internal indentation is normalised.""" | ||
| raw_doc = ( | ||
| "\n Verify the widget resets on power-up." | ||
| "\n\n Precondition: widget is powered.\n " | ||
| ) | ||
| items = [_make_item("tests/test_foo.py::test_multiline", raw_doc)] | ||
| _simulate_collection(plugin, items) | ||
| _simulate_logstart(plugin, "tests/test_foo.py::test_multiline") | ||
|
|
||
| expected = ( | ||
| "Verify the widget resets on power-up.\n\nPrecondition: widget is powered." | ||
| ) | ||
| plugin.backend.update_test_case_text.assert_called_once_with(99, expected) | ||
|
|
||
|
|
||
| def test_item_without_function_attribute_is_skipped(plugin): | ||
| """Items without a .function attribute (e.g. doctest items) must not crash collection.""" | ||
| item = MagicMock(spec=[]) # no attributes at all | ||
| item.nodeid = "tests/test_foo.py::test_synthetic" | ||
| session = MagicMock() | ||
| session.items = [item] | ||
|
|
||
| plugin.pytest_collection_finish(session) # must not raise | ||
|
|
||
| plugin.backend.update_test_case_text.assert_not_called() | ||
|
|
||
|
|
||
| def test_docstring_not_sent_for_different_nodeid(plugin): | ||
| """Docstring for test_a must be sent when test_a runs, but not when test_b runs.""" | ||
| item_a = _make_item("tests/test_foo.py::test_a", "Intention A") | ||
| item_b = _make_item("tests/test_foo.py::test_b", None) | ||
| _simulate_collection(plugin, [item_a, item_b]) | ||
|
|
||
| _simulate_logstart(plugin, "tests/test_foo.py::test_a") | ||
| plugin.backend.update_test_case_text.assert_called_once_with(99, "Intention A") | ||
|
|
||
| plugin.backend.update_test_case_text.reset_mock() | ||
|
|
||
| _simulate_logstart(plugin, "tests/test_foo.py::test_b") | ||
| plugin.backend.update_test_case_text.assert_not_called() | ||
|
|
||
|
|
||
| def test_backend_update_test_case_text_calls_rpc(): | ||
| """Backend.update_test_case_text must call TestCase.update with the text field.""" | ||
| backend = Backend.__new__(Backend) | ||
| backend.rpc = MagicMock() | ||
|
|
||
| backend.update_test_case_text(test_case_id=7, text="Some intention.") | ||
|
|
||
| backend.rpc.TestCase.update.assert_called_once_with(7, {"text": "Some intention."}) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a better assertion here would be that we've updated the text for test_a but not for test_b. Otherwise we may be missing a corner case.