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
Change scope of live server to session #113
Conversation
@nicoddemus Maybe it will be best to continue the conversation here. I'm worried we won't be able to use dynamic scoping as monkey patching isn't available outside of the |
pytest_flask/fixtures.py
Outdated
@@ -148,10 +151,10 @@ def test_server_is_up_and_running(live_server): | |||
host = pytestconfig.getvalue('live_server_host') | |||
|
|||
# Explicitly set application ``SERVER_NAME`` for test suite | |||
# and restore original value on test teardown. | |||
server_name = app.config['SERVER_NAME'] or 'localhost' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this simple case we can implement the "monkeypatch" functionality ourselves:
original_server_name = app.config.get('SERVER_NAME', None)
final_server_name = _rewrite_server_name(original_server_name or 'localhost', str(port))
app.config['SERVER_NAME'] = final_server_name
Then later, instead of return server
, we write:
yield server
# restore original value on teardown
if original_server_name is not None:
app.config['SERVER_NAME'] = original_server_name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, this has been updated and now the test passes again. Thanks
…tion' to maintain backwards compatibility
@nicoddemus I was able to make the scope dynamic with my latest commit. There was confusion around this when I initially did it as the documentation is misleading, I've created this issue: It looks like this functionality isn't supported by 2.7. Do you know of an easy fix? Or did you have any thoughts of dropping 2.7 support since its officially no longer maintained? |
Thanks for the report!
If 2.7 support is now a problem, then I think it is fair to drop support for it and only keep 35+ working. I will do that in another PR. |
Python 2.7 support has been dropped in #114, could you please merge |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Besides my comments, could you also please update the documentation in docs/features.rst
, specially mentioning the new option and updating the examples?
Thanks again for the work on this. 👍
pytest_flask/plugin.py
Outdated
@@ -165,6 +165,8 @@ def pytest_addoption(parser): | |||
help='use a host where to listen (default localhost).') | |||
group.addoption('--live-server-port', action='store', default=0, type=int, | |||
help='use a fixed port for the live_server fixture.') | |||
group.addoption('--live-server-scope', action='store', default='function', type=str, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I suggested earlier, let's make this an ini
option instead. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The next release will be 1.0
, and given that @havok2063 mentioned he has been using live_server
as "session" for a long time, how about we change the default to session
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good! I wasn't sure if we wanted to support cli
as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The good thing is that you can change ini options directly in the command-line: pytest -o live_server_scope=function
for example.
pytest_flask/fixtures.py
Outdated
@pytest.fixture(scope='function') | ||
def live_server(request, app, monkeypatch, pytestconfig): | ||
def determine_scope(fixture_name, config): | ||
if fixture_name is 'live_server': |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need this if
given determine_scope
is used only by live_server
, so let's remove it:
def determine_scope(fixture_name, config):
return config.getoption('--live-server-scope', 'function')
I'm happy to update #63. I've been syncing this every now and then but not regularly as there has been no movement, or comments, on integrating my PR in the last few years. |
A bit of an oversight here @nicoddemus. Changing the scope of the live server to Otherwise this test will continue to fail when the scope is anything but I'm hoping this is the only fixture that's affected, but I'll keep digging |
Ah yeah, looking at |
Hi @TWood67,
Actually
You mean because of |
@nicoddemus Thanks for all your help, tests are passing now |
Thanks a ton @TWood67 and @havok2063! I've squased and merged the changes, keeping a |
This was brought up in pytest-dev#113, but was missed.
Hey @TWood67, while preparing the release I noticed you probably missed #113 (comment), so I changed it to an ini option in #116. 👍 |
This was created from #112. I forked #63 as this had become several years out of date with master.
In addition to changes from #63, I had to skip one of the tests as I couldn't find a solution. I was unable to monkey patch the server name as monkey patch is scoped to function, and not session. Please let me know if there's a good solution to this as its currently failing.