Skip to content

@mocketize as TestCase class decorator #297

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
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

wilhelmklopp
Copy link
Contributor

One nice thing you can do in httpretty is:

@httpretty.activate
class MyTestCase(TestCase):

And then you don't need to do it for every single test method. Code in httpretty that handles this is here: https://github.com/gabrielfalcao/HTTPretty/blob/f9f012711597634d40066d144a36888b3addcc46/httpretty/core.py#L2066

Totally understand if this is a patch you would prefer not to accept, it probably feels a little "invasive" to mocket's core functionality. I'm happy to maintain a patch that works for me in my codebase.

(as with the previous PRs, this change is motivated by the httpretty->mocket migration i'm currently doing where we use the class level decorator quite heavily)

One annoying thing about the @httpretty.activate implementation is that you can't do @httpretty.activate(allow_net_connect=False) with the class decorator version. But with this PR you can do @mocketize(strict_mode=True).

But as I said, I have no issue at all if you'd prefer to close this PR without merging :)

Copy link

Copy link

codecov bot commented Jun 10, 2025

Codecov Report

Attention: Patch coverage is 41.02564% with 23 lines in your changes missing coverage. Please review.

Project coverage is 96.55%. Comparing base (713ccd6) to head (4b45c20).

Files with missing lines Patch % Lines
mocket/decorators/mocketizer.py 41.02% 23 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #297      +/-   ##
==========================================
- Coverage   98.71%   96.55%   -2.16%     
==========================================
  Files          22       22              
  Lines        1010     1046      +36     
==========================================
+ Hits          997     1010      +13     
- Misses         13       36      +23     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mindflayer
Copy link
Owner

mindflayer commented Jun 11, 2025

I am not a very big fan of the "all in" approach. I'd like when there is magic, but only when it was explicitly summoned by the sorcerer. 🪄

I also see reasons for wanting this change. For instance when I am using Mocket in "strict mode", it'd be great if we could define it for the whole test class instead of every little function.

Why don't we create a new decorator under mocket.decorators that implements what you want for classes?

This said, it's also missing tests, please add some meaningful ones (and maybe an example under the README). 🙏

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