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

Flush mocks when pook.activate used as a wrapper #144

Closed
shift0965 opened this issue Oct 14, 2024 · 4 comments · Fixed by #145
Closed

Flush mocks when pook.activate used as a wrapper #144

shift0965 opened this issue Oct 14, 2024 · 4 comments · Fixed by #145

Comments

@shift0965
Copy link
Contributor

shift0965 commented Oct 14, 2024

Description:

When using pook.activate as a decorator for test functions, existing mocks are not flushed before activating the mocks in each test. This can lead to test cases affecting each other, resulting in non-independent tests, which is a concern in test suites where each test should be isolated.

Expected Behavior:

When pook.activate is used to wrap a test function, it should automatically flush all existing mocks both before activating the new mocks and after the test function completes. This would ensure that each test starts with a clean slate and works independently without being affected by mocks from previous test cases.

Actual Behavior:

Currently, when pook.activate is used as a wrapper, it retains the mocks from previous tests, causing test cases to interfere with each other.

Suggested Solution:

Flush mocks before activation and after the testing function

Example:

import pook

@pook.on
def test_case_1():
    pook.get('http://example.com', reply=200)
    # ... perform some assertions and not hit the endpoint

@pook.on
def test_case_2():
    pook.get('http://example.com', reply=404)
    
    # Sending a request to the same endpoint
    response = some_http_library.get('http://example.com')
    
    # Expecting a 404 response, but this may fail due to leftover mock from test_case_1
    assert response.status_code == 404

In this example, test_case_2 will fails if both test cases are run together but it will pass when run individually. This behavior makes debugging extremely difficult because the tests are not properly isolated, leading to inconsistent results.

@KyleJamesWalker
Copy link
Contributor

Note: You can also check pook.done() at the end of your test which will raise if you have pending mocks that have not been matched.

@sarayourfriend
Copy link
Collaborator

Thanks again @shift0965, I've released your fix in https://github.com/h2non/pook/releases/tag/v2.1.1 🎉

@shift0965
Copy link
Contributor Author

Thanks again @shift0965, I've released your fix in https://github.com/h2non/pook/releases/tag/v2.1.1 🎉

Thanks!! Could you also help me release it on PyPI 🙏

@sarayourfriend
Copy link
Collaborator

Ah, apologies! I thought I had pushed it 🤦 It's there now, thanks very much for the ping!

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 a pull request may close this issue.

3 participants