-
Notifications
You must be signed in to change notification settings - Fork 30
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
httpserver: Clear state in function-scoped fixtures before the test #352
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #352 +/- ##
=======================================
Coverage 95.90% 95.90%
=======================================
Files 5 5
Lines 660 660
=======================================
Hits 633 633
Misses 27 27 ☔ View full report in Codecov by Sentry. |
hi @netaneld122 , I think your MR makes sense. I could reproduce it without using threads: import pytest
from pytest_httpserver import HTTPServer
import requests
class Client:
def __init__(self):
self.url: str | None = None
def get(self):
if self.url:
requests.get(self.url)
@pytest.fixture()
def my_fixture():
client = Client()
yield client
client.get()
def test_1(my_fixture: Client, httpserver: HTTPServer):
httpserver.expect_request("/foo").respond_with_data("OK")
my_fixture.url = httpserver.url_for("/foo")
def test_2(httpserver: HTTPServer):
print(httpserver.log) Basically, the issue is the order of destructing the fixtures, if httpserver gets destructed first, then it will clear its state and the remaining still-running fixtures can access it by the URL. If I change def my_fixture(httpserver): Then it will define the order of destructuring (and dependency), so While your PR makes totally sense, I'm thinking about what could go wrong with it (assuming the existing 3rd party codes which use pytest-httpserver). I see no issues at the moment but I want to sleep on it. Thanks, |
Thanks for your reply @csernazs. I only want to point out that I understand your concerns though, perhaps bumping the minor version (rather than the patch version) could be less prone to breakage (for those who depend on I can also suggest clearing the state both before and after the test function, does that feel safer? |
Yeah, I was also thinking about clearing the state both side. But tbh your approach seems also safe. There's no way to access the httpserver fixture without clearing it before, so I'd release your PR. Maybe 1.1.0 as you suggested. Zsolt |
This is fixed in #352, and here is the test for it.
This is fixed in #352, and here is the test for it.
This is fixed in #352, and here is the test for it.
This is fixed in #352, and here is the test for it.
@netaneld122 version 1.1.0 has just been released! Thanks for your fix! Zsolt |
…evelopment-dependencies group (#454) Bumps the development-dependencies group with 1 update: [pytest-httpserver](https://github.com/csernazs/pytest-httpserver). Updates `pytest-httpserver` from 1.0.12 to 1.1.0 <details> <summary>Release notes</summary> <p><em>Sourced from <a href="https://github.com/csernazs/pytest-httpserver/releases">pytest-httpserver's releases</a>.</em></p> <blockquote> <h2>1.1.0</h2> <h2>What's Changed</h2> <ul> <li>httpserver: Clear state in function-scoped fixtures before the test by <a href="https://github.com/netaneld122"><code>@netaneld122</code></a> in <a href="https://redirect.github.com/csernazs/pytest-httpserver/pull/352">csernazs/pytest-httpserver#352</a></li> </ul> <p><strong>Full Changelog</strong>: <a href="https://github.com/csernazs/pytest-httpserver/compare/1.0.12...1.1.0">https://github.com/csernazs/pytest-httpserver/compare/1.0.12...1.1.0</a></p> </blockquote> </details> <details> <summary>Changelog</summary> <p><em>Sourced from <a href="https://github.com/csernazs/pytest-httpserver/blob/master/CHANGES.rst">pytest-httpserver's changelog</a>.</em></p> <blockquote> <h1>1.1.0</h1> <p>.. _Release Notes_1.1.0_Bug Fixes:</p> <h2>Bug Fixes</h2> <ul> <li>Fixed an issue related to the leak of httpserver state between the tests when httpserver is destructed before the other fixtures. <code>[#352](csernazs/pytest-httpserver#352) <https://github.com/csernazs/pytest-httpserver/issues/352></code>_</li> </ul> <p>.. _Release Notes_1.0.12:</p> </blockquote> </details> <details> <summary>Commits</summary> <ul> <li><a href="https://github.com/csernazs/pytest-httpserver/commit/4f19faf89bb1fc6d2b637d6824bc4df90be027be"><code>4f19faf</code></a> CHANGES.rst: add release notes for 1.1.0</li> <li><a href="https://github.com/csernazs/pytest-httpserver/commit/1b01241affebad9d806646bcecbf9134e3d2877c"><code>1b01241</code></a> Version bump to 1.1.0</li> <li><a href="https://github.com/csernazs/pytest-httpserver/commit/86c96670220af73fdb2b146418275f50c6304225"><code>86c9667</code></a> releasenotes: add release note for <a href="https://redirect.github.com/csernazs/pytest-httpserver/issues/352">#352</a></li> <li><a href="https://github.com/csernazs/pytest-httpserver/commit/2fed2b6fd0b3fbeb47a47bf9721c4567eeb3785e"><code>2fed2b6</code></a> tests: add test for log leak</li> <li><a href="https://github.com/csernazs/pytest-httpserver/commit/862d4afe489d3ebf6e5e57585ec530bc9df1e3e0"><code>862d4af</code></a> httpserver: Cleanup state in function scoped fixtures before the test</li> <li><a href="https://github.com/csernazs/pytest-httpserver/commit/0eb610bc87e3b6cd4f47dad574691a430b95282c"><code>0eb610b</code></a> [pre-commit.ci] pre-commit autoupdate</li> <li><a href="https://github.com/csernazs/pytest-httpserver/commit/a178b96dac5821ad47ca1722f8a6108261267b36"><code>a178b96</code></a> build(deps-dev): bump the deps group with 3 updates</li> <li><a href="https://github.com/csernazs/pytest-httpserver/commit/7df2d1cede92144e648049505695bc88251fa206"><code>7df2d1c</code></a> [pre-commit.ci] pre-commit autoupdate</li> <li>See full diff in <a href="https://github.com/csernazs/pytest-httpserver/compare/1.0.12...1.1.0">compare view</a></li> </ul> </details> <br /> [](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) --- <details> <summary>Dependabot commands and options</summary> <br /> You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot show <dependency name> ignore conditions` will show all of the ignore conditions of the specified dependency - `@dependabot ignore <dependency name> major version` will close this group update PR and stop Dependabot creating any more for the specific dependency's major version (unless you unignore this specific dependency's major version or upgrade to it yourself) - `@dependabot ignore <dependency name> minor version` will close this group update PR and stop Dependabot creating any more for the specific dependency's minor version (unless you unignore this specific dependency's minor version or upgrade to it yourself) - `@dependabot ignore <dependency name>` will close this group update PR and stop Dependabot creating any more for the specific dependency (unless you unignore this specific dependency or upgrade to it yourself) - `@dependabot unignore <dependency name>` will remove all of the ignore conditions of the specified dependency - `@dependabot unignore <dependency name> <ignore condition>` will remove the ignore condition of the specified dependency and ignore conditions </details> Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Bumps [pytest-httpserver](https://github.com/csernazs/pytest-httpserver) from 1.0.12 to 1.1.0. <details> <summary>Release notes</summary> <p><em>Sourced from <a href="https://github.com/csernazs/pytest-httpserver/releases">pytest-httpserver's releases</a>.</em></p> <blockquote> <h2>1.1.0</h2> <h2>What's Changed</h2> <ul> <li>httpserver: Clear state in function-scoped fixtures before the test by <a href="https://github.com/netaneld122"><code>@netaneld122</code></a> in <a href="https://redirect.github.com/csernazs/pytest-httpserver/pull/352">csernazs/pytest-httpserver#352</a></li> </ul> <p><strong>Full Changelog</strong>: <a href="https://github.com/csernazs/pytest-httpserver/compare/1.0.12...1.1.0">https://github.com/csernazs/pytest-httpserver/compare/1.0.12...1.1.0</a></p> </blockquote> </details> <details> <summary>Changelog</summary> <p><em>Sourced from <a href="https://github.com/csernazs/pytest-httpserver/blob/master/CHANGES.rst">pytest-httpserver's changelog</a>.</em></p> <blockquote> <h1>1.1.0</h1> <p>.. _Release Notes_1.1.0_Bug Fixes:</p> <h2>Bug Fixes</h2> <ul> <li>Fixed an issue related to the leak of httpserver state between the tests when httpserver is destructed before the other fixtures. <code>[#352](csernazs/pytest-httpserver#352) <https://github.com/csernazs/pytest-httpserver/issues/352></code>_</li> </ul> <p>.. _Release Notes_1.0.12:</p> </blockquote> </details> <details> <summary>Commits</summary> <ul> <li><a href="https://github.com/csernazs/pytest-httpserver/commit/4f19faf89bb1fc6d2b637d6824bc4df90be027be"><code>4f19faf</code></a> CHANGES.rst: add release notes for 1.1.0</li> <li><a href="https://github.com/csernazs/pytest-httpserver/commit/1b01241affebad9d806646bcecbf9134e3d2877c"><code>1b01241</code></a> Version bump to 1.1.0</li> <li><a href="https://github.com/csernazs/pytest-httpserver/commit/86c96670220af73fdb2b146418275f50c6304225"><code>86c9667</code></a> releasenotes: add release note for <a href="https://redirect.github.com/csernazs/pytest-httpserver/issues/352">#352</a></li> <li><a href="https://github.com/csernazs/pytest-httpserver/commit/2fed2b6fd0b3fbeb47a47bf9721c4567eeb3785e"><code>2fed2b6</code></a> tests: add test for log leak</li> <li><a href="https://github.com/csernazs/pytest-httpserver/commit/862d4afe489d3ebf6e5e57585ec530bc9df1e3e0"><code>862d4af</code></a> httpserver: Cleanup state in function scoped fixtures before the test</li> <li><a href="https://github.com/csernazs/pytest-httpserver/commit/0eb610bc87e3b6cd4f47dad574691a430b95282c"><code>0eb610b</code></a> [pre-commit.ci] pre-commit autoupdate</li> <li><a href="https://github.com/csernazs/pytest-httpserver/commit/a178b96dac5821ad47ca1722f8a6108261267b36"><code>a178b96</code></a> build(deps-dev): bump the deps group with 3 updates</li> <li><a href="https://github.com/csernazs/pytest-httpserver/commit/7df2d1cede92144e648049505695bc88251fa206"><code>7df2d1c</code></a> [pre-commit.ci] pre-commit autoupdate</li> <li>See full diff in <a href="https://github.com/csernazs/pytest-httpserver/compare/1.0.12...1.1.0">compare view</a></li> </ul> </details> <br /> [](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) --- <details> <summary>Dependabot commands and options</summary> <br /> You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot show <dependency name> ignore conditions` will show all of the ignore conditions of the specified dependency - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) </details> Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
I believe it makes sense to clear the server state before performing a test rather than after, starting each test in a fresh state.
Consider the following case:
In that case -
test_function_2
may receive leaked logs fromtest_function_1
sincemy_fixture
is destructed only afterhttpserver
and therefore the fixture would be able to send requests to the server after the state is cleared.One could argue that swapping the fixtures order would solve this, but in real world scenarios fixtures can be nested and far too complex to have a well defined order for cases like that. I think that clearing the state before the test is a simple and less error prone solution.