Skip to content

Conversation

Copy link

Copilot AI commented Jan 13, 2026

Description

Converted tests/unit_tests/test_cloud.py from unittest-style mocking to pytest-style using the mocker fixture from pytest-mock, and incremented the package version.

Changes Made

  • Removed mock import and @mock.patch decorators: Replaced all 7 test methods using @mock.patch decorators with mocker.patch() calls inside test functions
  • Updated test signatures: Added mocker fixture parameter to affected tests, removed decorator-injected mock parameters
  • Fixed docstrings: Updated to comply with pydocstyle requirements (D400, D102)
  • Version bump: Incremented semantic PATCH version from 1!10.16.0 to 1!10.16.1

Example

Before:

@mock.patch(MPATH + "getpass.getuser", return_value="root")
@mock.patch("os.path.expanduser", side_effect=lambda x: x.replace("~", "/root"))
def test_init_sets_key_pair_based_on_getuser(self, _m_expanduser, _m_getuser):
    mycloud = CloudSubclass(tag="tag", timestamp_suffix=False, config_file=StringIO(CONFIG))
    assert mycloud.key_pair.name == "root"

After:

def test_init_sets_key_pair_based_on_getuser(self, mocker):
    mocker.patch("os.path.exists", side_effect=lambda x: "/.ssh/id_rsa" in x)
    mocker.patch("os.path.expanduser", side_effect=lambda x: x.replace("~", "/root"))
    mocker.patch(MPATH + "getpass.getuser", return_value="root")
    mycloud = CloudSubclass(tag="tag", timestamp_suffix=False, config_file=StringIO(CONFIG))
    assert mycloud.key_pair.name == "root"

Additional Context and Relevant Issues

Part of broader effort to standardize test patterns across the codebase. No behavior changes—all 319 unit tests pass. All GitHub workflows validated (ruff, mypy, pytest, docs, version_check).

Test Steps

tox -e pytest -- tests/unit_tests/test_cloud.py -v
tox -e format
tox -e pytest
tox -e ruff,mypy
Original prompt

This section details on the original issue you should resolve

<issue_title>Refactor: Convert unittest to pytest in tests/unit_tests/test_helpers.py</issue_title>
<issue_description>This task involves converting the tests within tests/unit_tests/test_cloud.py from unittest.TestCase-based tests to the pytest functional style.

Instructions for Copilot/Assignee:

  1. Remove TestCase Inheritance: Convert test classes inheriting from unittest.TestCase into regular Python classes or simply remove the classes and use standalone functions.
  2. Convert Assertions: Replace self.assertEqual(...), self.assertTrue(...), etc., with standard assert statements. pytest automatically handles assertion rewriting.
    3.Refactor Setup/Teardown:
  • Move setUp() methods to pytest.fixture functions.
  • Move tearDown() methods to finalizers in fixtures using yield or addfinalizer.
  1. Use mocker instead or mock.patch or mock.call
    5.Handle Exceptions: Replace self.assertRaises(...) with pytest.raises(...) as a context manager.

Acceptance criteria:

  • no remaining self.assert*, mock.call or mock.patch uses
  • no setUp or teadDown methods
  • If common mocks are used for most tests in a class, ensure the test class has a common setup_mocks fixture
  • Ensure proper format is applied by running tox -e format
  • Run Tests: Ensure all tests with tox -e pytest in the root directory</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: blackboxsw <14128326+blackboxsw@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor tests from unittest to pytest in test_helpers.py refactor: Convert test_cloud.py from unittest mock.patch to pytest mocker Jan 13, 2026
Copilot AI requested a review from blackboxsw January 13, 2026 16:28
@blackboxsw
Copy link
Collaborator

@copilot Please increment to pyclodulib semantic PATCH version in pycloudlib/VERSION and ensure all github workflows pass

Co-authored-by: blackboxsw <14128326+blackboxsw@users.noreply.github.com>
Copy link
Author

Copilot AI commented Jan 13, 2026

@copilot Please increment to pyclodulib semantic PATCH version in pycloudlib/VERSION and ensure all github workflows pass

Done. Version incremented to 1!10.16.1 in commit 2a683f1. All GitHub workflows pass:

  • ✅ ruff (linting)
  • ✅ mypy (type checking)
  • ✅ pytest (all 319 unit tests)
  • ✅ docs (documentation build)
  • ✅ version_check (valid semantic PATCH version bump)

@blackboxsw blackboxsw marked this pull request as ready for review January 13, 2026 20:17
Copy link
Collaborator

@blackboxsw blackboxsw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Looks good to me, in the future, we'd like to see a single squashed commit for unittest changes for this type of refactor.

Acceptance criteria:

  • Each commit must use conventional commits for the commit summary
  • Each test commit shall contain a footer Fixes GH-<issue_num>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor: Convert unittest to pytest in tests/unit_tests/test_helpers.py

3 participants