Skip to content

Commit a0cf4de

Browse files
author
Fabien Coelho
committed
simplify app selection
1 parent 50b2692 commit a0cf4de

File tree

7 files changed

+26
-30
lines changed

7 files changed

+26
-30
lines changed

FlaskTester.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -532,15 +532,14 @@ def _ft_client(authenticator):
532532
default_login = os.environ.get("FLASK_TESTER_DEFAULT", None)
533533
client: Client
534534

535-
if "FLASK_TESTER_URL" in os.environ:
536-
537-
app_url = os.environ["FLASK_TESTER_URL"]
538-
client = RequestClient(authenticator, app_url, default_login)
539-
540-
elif "FLASK_TESTER_APP" in os.environ:
535+
app_def = os.environ.get("FLASK_TESTER_URL", "app")
536+
app_def = os.environ.get("FLASK_TESTER_APP", app_def)
541537

538+
if app_def.startswith("http://") or app_def.startswith("https://"):
539+
client = RequestClient(authenticator, app_def, default_login)
540+
else:
542541
# load app package
543-
pkg_name, app = os.environ["FLASK_TESTER_APP"], None
542+
pkg_name, app = app_def, None
544543
app_names = ["app", "application", "create_app", "make_app"]
545544
if ":" in pkg_name: # override defaults
546545
pkg_name, app_name = pkg_name.split(":", 1)
@@ -557,20 +556,17 @@ def _ft_client(authenticator):
557556
raise FlaskTesterError(f"cannot find Flask app in {pkg_name}")
558557
client = FlaskClient(authenticator, app.test_client(), default_login)
559558

560-
else:
561-
562-
raise FlaskTesterError("no Flask application to test")
563-
564559
return client
565560

566561
@pytest.fixture
567562
def ft_client(ft_authenticator):
568563
"""Pytest Fixture: ft_client.
569564
570-
Target environment variable, one **must** be defined:
565+
Target environment variable:
571566
572-
- ``FLASK_TESTER_URL``: application HTTP base URL, eg ``http://localhost:5000``.
573-
- ``FLASK_TESTER_APP``: Flask application, eg ``app:create_app``.
567+
- ``FLASK_TESTER_APP``: find the Flask application, eg
568+
``app:create_app`` for an internal test, or
569+
``http://localhost:5000`` for an external test.
574570
575571
Other environment variable:
576572

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ This can be run against a (local) server:
6464
export TEST_SEED="some-random-data" # shared test seed
6565
flask --app app:app run & # start flask app
6666
pid=$! # keep pid
67-
export FLASK_TESTER_URL="http://localhost:5000" # set app local url
67+
export FLASK_TESTER_APP="http://localhost:5000" # set app local url
6868
pytest test.py # run external tests
6969
kill $pid # stop app with pid
7070
```
7171

7272
Or locally with the Flask internal test infrastructure:
7373

7474
```shell
75-
export FLASK_TESTER_APP="app:app" # set app module
75+
export FLASK_TESTER_APP="app:app" # set app package
7676
pytest test.py # run internal tests
7777
```
7878

docs/documentation.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,20 @@ The package provides two fixtures:
3838
- `setAuth` to add authentication data to a request `kwargs` and `cookies`.
3939
This method is called automatically for adding credentials to a request.
4040

41-
- `ft_client` for app testing, which depends on the previous fixture, plus
42-
environment variables which allow to find the application, at least one must
43-
be defined:
41+
- `ft_client` for app testing, which depends on the previous fixture, plus the
42+
`FLASK_TESTER_APP` environment variables which allow to find the application,
43+
which contains either:
4444

45-
- `FLASK_TESTER_URL` URL of the running application for external tests.
45+
- The URL of the running application for external tests.
4646
The application is expected to be already running when the test is started.
4747

48-
- `FLASK_TESTER_APP` package (filename without `.py`) to be imported for the application.
48+
- The package (filename without `.py`) to be imported for the application.
4949
- for `pkg:name`, `name` is the application in `pkg`.
5050
- for `pkg` only, look for app as `app`, `application`, `create_app`, `make_app`.
5151
- in both cases, `name` is called if callable and not a Flask application.
5252

53+
If not set, the default is `app`, which is to behave like Flask.
54+
5355
Moreover:
5456
- `FLASK_TESTER_DEFAULT` default login for authentication, default is _None_.
5557

docs/versions.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ Packages are distributed from [PyPI](https://pypi.org/project/FlaskTester/),
55
see also the [documentation](https://zx80.github.io/flask-tester/),
66
please report any [issues](https://github.com/zx80/flask-tester/issues).
77

8-
## ? on ?
8+
## 3.6 on ?
99

10+
Only use `FLASK_TESTER_APP`, hide `FLASK_TESTER_URL`, which is only kept for
11+
upward compatibility and is deprecated.
1012
Improved documentation, including a working `app2`.
1113

1214
## 3.5 on 2024-03-30

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "FlaskTester"
7-
version = "3.5"
7+
version = "3.6"
88
authors = [ { name = "Fabien Coelho", email = "[email protected]" } ]
99
description = "Pytest fixtures for Flask internal and external authenticated tests"
1010
readme = "README.md"

tests/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ check.external:
2929
$(PYTEST) $(PYTOPT) test_app.py
3030
kill $$flask_pid
3131
# app2
32+
export FLASK_TESTER_APP="http://localhost:$(PORT)"
3233
flask --app app2 run --port=$(PORT) &
3334
flask_pid=$$!
3435
sleep $(SLEEP)

tests/test_app.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ def test_client_fixture():
309309
# ft_client coverage
310310
auth = ft._ft_authenticator()
311311
# check and save env
312-
# assert "FLASK_TESTER_URL" not in os.environ
313312
url = None
314313
if "FLASK_TESTER_URL" in os.environ: # pragma: no cover
315314
url = os.environ["FLASK_TESTER_URL"]
@@ -318,12 +317,8 @@ def test_client_fixture():
318317
if "FLASK_TESTER_APP" in os.environ:
319318
app = os.environ["FLASK_TESTER_APP"]
320319
del os.environ["FLASK_TESTER_APP"]
321-
# no url nor app
322-
try:
323-
init = ft._ft_client(auth)
324-
pytest.fail("must fail without environment variables") # pragma: no cover
325-
except ft.FlaskTesterError:
326-
assert True, "expected error raised"
320+
# no url nor app, defaults to "app"
321+
init = ft._ft_client(auth)
327322
# url
328323
os.environ["FLASK_TESTER_URL"] = "http://localhost:5000"
329324
init = ft._ft_client(auth)

0 commit comments

Comments
 (0)