Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
rev: v6.0.0
hooks:
- id: check-byte-order-marker
- id: trailing-whitespace
Expand All @@ -9,16 +9,16 @@ repos:
args: [--remove]
- id: check-yaml
- repo: https://github.com/asottile/reorder-python-imports
rev: v3.14.0
rev: v3.16.0
hooks:
- id: reorder-python-imports
args: ['--application-directories=.:src', --py3-plus]
- repo: https://github.com/psf/black
rev: 24.10.0
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 26.1.0
hooks:
- id: black
- repo: https://github.com/PyCQA/flake8
rev: 7.1.1
rev: 7.3.0
hooks:
- id: flake8
additional_dependencies: [flake8-bugbear]
Expand All @@ -35,7 +35,7 @@ repos:
hooks:
- id: autoflake
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.14.1
rev: v1.19.1
hooks:
- id: mypy
files: ^(src/|tests/)
1 change: 0 additions & 1 deletion src/pytest_flask/_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from pytest import Config as _PytestConfig


_PytestScopeName = Literal["session", "package", "module", "class", "function"]


Expand Down
8 changes: 4 additions & 4 deletions src/pytest_flask/plugin.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env python
"""
A py.test plugin which helps testing Flask applications.
A py.test plugin which helps testing Flask applications.

:copyright: (c) by Vital Kudzelka
:license: MIT
:copyright: (c) by Vital Kudzelka
:license: MIT
"""

from typing import Any
from typing import List
from typing import Protocol
Expand All @@ -25,7 +26,6 @@
from .fixtures import live_server
from .pytest_compat import getfixturevalue


_Response = TypeVar("_Response")


Expand Down
57 changes: 18 additions & 39 deletions tests/test_live_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import pytest
from flask import url_for


pytestmark = pytest.mark.skipif(not hasattr(os, "fork"), reason="needs fork")


Expand Down Expand Up @@ -35,44 +34,38 @@ def test_set_application_server_name(self, live_server):
)

def test_rewrite_application_server_name(self, appdir):
appdir.create_test_module(
"""
appdir.create_test_module("""
import pytest
@pytest.mark.options(server_name='example.com:5000')
def test_a(live_server):
assert live_server.app.config['SERVER_NAME'] == \\
'example.com:%d' % live_server.port
"""
)
""")

result = appdir.runpytest("-v", "-o", "live_server_scope=function")
result.stdout.fnmatch_lines(["*PASSED*"])
assert result.ret == 0

def test_prevent_starting_live_server(self, appdir):
appdir.create_test_module(
"""
appdir.create_test_module("""
import pytest

def test_a(live_server):
assert live_server._process is None
"""
)
""")

result = appdir.runpytest("-v", "--no-start-live-server")
result.stdout.fnmatch_lines(["*passed*"])
assert result.ret == 0

def test_start_live_server(self, appdir):
appdir.create_test_module(
"""
appdir.create_test_module("""
import pytest

def test_a(live_server):
assert live_server._process
assert live_server._process.is_alive()
"""
)
""")
result = appdir.runpytest("-v", "--start-live-server")
result.stdout.fnmatch_lines(["*passed*"])
assert result.ret == 0
Expand Down Expand Up @@ -103,8 +96,7 @@ def mocked_stop_cleanly(*args, **kwargs):

monkeypatch.setattr(LiveServer, "_stop_cleanly", mocked_stop_cleanly)

appdir.create_test_module(
"""
appdir.create_test_module("""
import pytest

from flask import url_for
Expand All @@ -119,8 +111,7 @@ def index():
res = client.get(url_for('index', _external=True))
assert res.status_code == 200
assert b'got it' in res.data
"""
)
""")
args = [] if clean_stop else ["--no-live-server-clean-stop"]
result = appdir.runpytest_inprocess("-v", "--no-start-live-server", *args)
result.stdout.fnmatch_lines("*1 passed*")
Expand All @@ -130,8 +121,7 @@ def index():
assert stop_cleanly_result == []

def test_add_endpoint_to_live_server(self, appdir):
appdir.create_test_module(
"""
appdir.create_test_module("""
import pytest

from flask import url_for
Expand All @@ -146,16 +136,14 @@ def new_endpoint():
res = client.get(url_for('new_endpoint', _external=True))
assert res.status_code == 200
assert b'got it' in res.data
"""
)
""")
result = appdir.runpytest("-v", "--no-start-live-server")
result.stdout.fnmatch_lines(["*passed*"])
assert result.ret == 0

@pytest.mark.skip("this test hangs in the original code")
def test_concurrent_requests_to_live_server(self, appdir):
appdir.create_test_module(
"""
appdir.create_test_module("""
import pytest

from flask import url_for
Expand All @@ -175,51 +163,42 @@ def two():
res = client.get(url_for('one', _external=True))
assert res.status_code == 200
assert b'42' in res.data
"""
)
""")
result = appdir.runpytest("-v", "--no-start-live-server")
result.stdout.fnmatch_lines(["*passed*"])
assert result.ret == 0

@pytest.mark.parametrize("port", [5000, 5001])
def test_live_server_fixed_port(self, port, appdir):
appdir.create_test_module(
"""
appdir.create_test_module("""
import pytest

def test_port(live_server):
assert live_server.port == %d
"""
% port
)
""" % port)
result = appdir.runpytest("-v", "--live-server-port", str(port))
result.stdout.fnmatch_lines(["*PASSED*"])
assert result.ret == 0

@pytest.mark.parametrize("host", ["127.0.0.1", "0.0.0.0"])
def test_live_server_fixed_host(self, host, appdir):
appdir.create_test_module(
"""
appdir.create_test_module("""
import pytest

def test_port(live_server):
assert live_server.host == '%s'
"""
% host
)
""" % host)
result = appdir.runpytest("-v", "--live-server-host", str(host))
result.stdout.fnmatch_lines(["*PASSED*"])
assert result.ret == 0

def test_respect_wait_timeout(self, appdir):
appdir.create_test_module(
"""
appdir.create_test_module("""
import pytest

def test_should_fail(live_server):
assert live_server._process.is_alive()
"""
)
""")
result = appdir.runpytest("-v", "--live-server-wait=0.00000001")
result.stdout.fnmatch_lines(["**ERROR**"])
assert result.ret == 1
Loading