Skip to content
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

chore: Use pytest-mock in tests #847

Merged
merged 9 commits into from
Dec 30, 2023

Conversation

RisingOrange
Copy link
Collaborator

@RisingOrange RisingOrange commented Dec 21, 2023

Replace usages of monkeypatch (+ unittest.mock) or our custom mock_function fixture with pytest-mock. This simplifies the test code.

Related issues

Proposed changes

  • Add pytest-mock to requirements
  • Replace usages of monkeypatch with mocker.patch
  • Replace usages of our mock_function fixture with mocker.patch
  • Replace usages of unittest.Mock() with mocker.stub() or mocker.Mock()

@RisingOrange RisingOrange changed the title chore: Replace monkeypatch with pytest-mock in unit tests chore: Use pytest-mock in unit tests Dec 21, 2023
@RisingOrange RisingOrange marked this pull request as ready for review December 21, 2023 20:35
Copy link

codecov bot commented Dec 21, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (ad16933) 85.12% compared to head (472b3e0) 85.06%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #847      +/-   ##
==========================================
- Coverage   85.12%   85.06%   -0.07%     
==========================================
  Files          65       65              
  Lines        5931     5931              
==========================================
- Hits         5049     5045       -4     
- Misses        882      886       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@treyhunner treyhunner left a comment

Choose a reason for hiding this comment

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

This is looking great.

All the repetition of various mocker.patch.object(AnkiHubClient, "some_method") usages make me think a custom fixture to assist with patching the client in various ways might be useful after this first wave of monkey patch refactoring.

tests/addon/test_unit.py Outdated Show resolved Hide resolved
tests/addon/test_unit.py Outdated Show resolved Hide resolved
@RisingOrange
Copy link
Collaborator Author

@treyhunner

All the repetition of various mocker.patch.object(AnkiHubClient, "some_method") usages make me think a custom fixture to assist with patching the client in various ways might be useful after this first wave of monkey patch refactoring.

Sounds good. I'm curious how this would look like.

treyhunner
treyhunner previously approved these changes Dec 29, 2023
Copy link
Contributor

@treyhunner treyhunner left a comment

Choose a reason for hiding this comment

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

This looks great! I think #849 should be merged into this first and then I think this is ready to go. It looks like there's an minor import difference that needs resolving before merging to main.

* chore: Replaces usages of monkeypatch in test_client.py

* Attempt to use mocker and reset os.remove (#850)

* Attempt to use mocker and reset os.remove

* Use mocker.resetall to reset all mocks

* Use stop (reset is just for the mock call values)

* Fix types: use mocker.stop on remove_mock

---------

Co-authored-by: Trey Hunner <[email protected]>
@RisingOrange RisingOrange changed the title chore: Use pytest-mock in unit tests chore: Use pytest-mock in unit tests and client tests Dec 30, 2023
* Replace some monkeypatch uses with pytest-mock

* Use mocker everywhere in integration tests

Replace all uses of unittest.mock, monkeypatch fixture, and the custom
mock_function fixture with the mocker fixture from pytest-mock.

* Use mocker for mock_function fixture

* Use non-string type annotations for Mock

* Prefer mocker.patch over mocker.patch.object

* Apply suggestions from code review

Co-authored-by: Jakub Fidler <[email protected]>

* Fix variable name typos (mesaage -> message)

* Remove unused mock_function fixture

---------

Co-authored-by: Jakub Fidler <[email protected]>
@RisingOrange RisingOrange changed the title chore: Use pytest-mock in unit tests and client tests chore: Use pytest-mock for add-on tests Dec 30, 2023
@RisingOrange RisingOrange changed the title chore: Use pytest-mock for add-on tests chore: Use pytest-mock in tests Dec 30, 2023
@RisingOrange RisingOrange merged commit 0519834 into main Dec 30, 2023
8 of 9 checks passed
@RisingOrange RisingOrange deleted the chore/use-pytest-mock-for-unit-tests branch December 30, 2023 16:44
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.

2 participants