From 600f15faa0209704fdf02d69850ca4478b01b52a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Fri, 10 Jul 2020 11:08:19 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=94=20Improve=20support=20for=20tests=20i?= =?UTF-8?q?n=20editor=20(#1699)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ♻️ Remove required extra steps to test in editor * 🎨 Format lint script * 📝 Remove obsolete extra steps required to test in editor from docs * 🐛 Fix coverage --- .env | 1 - docs/en/docs/contributing.md | 10 ---------- docs/zh/docs/contributing.md | 10 ---------- scripts/lint.sh | 2 +- .../test_extending_openapi/test_tutorial002.py | 2 +- .../test_sql_databases/test_sql_databases.py | 2 +- .../test_sql_databases_middleware.py | 2 +- .../test_sql_databases/test_testing_databases.py | 2 +- .../test_sql_databases_peewee.py | 2 +- tests/test_tutorial/test_templates/test_tutorial001.py | 7 ++++++- tests/test_tutorial/test_testing/test_main.py | 2 +- tests/test_tutorial/test_testing/test_tutorial001.py | 2 +- tests/test_tutorial/test_testing/test_tutorial002.py | 2 +- tests/test_tutorial/test_testing/test_tutorial003.py | 2 +- .../test_testing_dependencies/test_tutorial001.py | 2 +- .../test_tutorial/test_websockets/test_tutorial001.py | 3 ++- .../test_tutorial/test_websockets/test_tutorial002.py | 3 ++- 17 files changed, 21 insertions(+), 35 deletions(-) delete mode 100644 .env diff --git a/.env b/.env deleted file mode 100644 index 703242362..000000000 --- a/.env +++ /dev/null @@ -1 +0,0 @@ -PYTHONPATH=./docs_src diff --git a/docs/en/docs/contributing.md b/docs/en/docs/contributing.md index 416bc32f8..cd87faa4e 100644 --- a/docs/en/docs/contributing.md +++ b/docs/en/docs/contributing.md @@ -499,13 +499,3 @@ $ bash scripts/test-cov-html.sh This command generates a directory `./htmlcov/`, if you open the file `./htmlcov/index.html` in your browser, you can explore interactively the regions of code that are covered by the tests, and notice if there is any region missing. - -### Tests in your editor - -If you want to use the integrated tests in your editor add `./docs_src` to your `PYTHONPATH` variable. - -For example, in VS Code you can create a file `.env` with: - -```env -PYTHONPATH=./docs_src -``` diff --git a/docs/zh/docs/contributing.md b/docs/zh/docs/contributing.md index e9645392e..402668c47 100644 --- a/docs/zh/docs/contributing.md +++ b/docs/zh/docs/contributing.md @@ -498,13 +498,3 @@ $ bash scripts/test-cov-html.sh 该命令生成了一个 `./htmlcov/` 目录,如果你在浏览器中打开 `./htmlcov/index.html` 文件,你可以交互式地浏览被测试所覆盖的代码区块,并注意是否缺少了任何区块。 - -### 在编辑器中测试 - -如果你想要在编辑器中运行集成测试,请将 `./docs_src` 加入到你的 `PYTHONPATH` 变量中。 - -例如,在 VS Code 中你可以创建一个包含以下内容的 `.env` 文件: - -```env -PYTHONPATH=./docs_src -``` diff --git a/scripts/lint.sh b/scripts/lint.sh index 43a1b0c1d..12fb359e4 100755 --- a/scripts/lint.sh +++ b/scripts/lint.sh @@ -5,4 +5,4 @@ set -x mypy fastapi black fastapi tests --check -isort fastapi tests docs_src scripts --check-only +isort fastapi tests docs_src scripts --check-only diff --git a/tests/test_tutorial/test_extending_openapi/test_tutorial002.py b/tests/test_tutorial/test_extending_openapi/test_tutorial002.py index 06fb62f85..654db2e4c 100644 --- a/tests/test_tutorial/test_extending_openapi/test_tutorial002.py +++ b/tests/test_tutorial/test_extending_openapi/test_tutorial002.py @@ -10,7 +10,7 @@ def client(): static_dir: Path = Path(os.getcwd()) / "static" print(static_dir) static_dir.mkdir(exist_ok=True) - from extending_openapi.tutorial002 import app + from docs_src.extending_openapi.tutorial002 import app with TestClient(app) as client: yield client diff --git a/tests/test_tutorial/test_sql_databases/test_sql_databases.py b/tests/test_tutorial/test_sql_databases/test_sql_databases.py index 69092889c..cd3f13e7a 100644 --- a/tests/test_tutorial/test_sql_databases/test_sql_databases.py +++ b/tests/test_tutorial/test_sql_databases/test_sql_databases.py @@ -288,7 +288,7 @@ def client(): if test_db.is_file(): # pragma: nocover test_db.unlink() # Import while creating the client to create the DB after starting the test session - from sql_databases.sql_app import main + from docs_src.sql_databases.sql_app import main # Ensure import side effects are re-executed importlib.reload(main) diff --git a/tests/test_tutorial/test_sql_databases/test_sql_databases_middleware.py b/tests/test_tutorial/test_sql_databases/test_sql_databases_middleware.py index 6b4ed567e..b02e1c89e 100644 --- a/tests/test_tutorial/test_sql_databases/test_sql_databases_middleware.py +++ b/tests/test_tutorial/test_sql_databases/test_sql_databases_middleware.py @@ -288,7 +288,7 @@ def client(): if test_db.is_file(): # pragma: nocover test_db.unlink() # Import while creating the client to create the DB after starting the test session - from sql_databases.sql_app import alt_main + from docs_src.sql_databases.sql_app import alt_main # Ensure import side effects are re-executed importlib.reload(alt_main) diff --git a/tests/test_tutorial/test_sql_databases/test_testing_databases.py b/tests/test_tutorial/test_sql_databases/test_testing_databases.py index 83a156b85..9acfd8908 100644 --- a/tests/test_tutorial/test_sql_databases/test_testing_databases.py +++ b/tests/test_tutorial/test_sql_databases/test_testing_databases.py @@ -7,7 +7,7 @@ def test_testing_dbs(): if test_db.is_file(): # pragma: nocover test_db.unlink() # Import while creating the client to create the DB after starting the test session - from sql_databases.sql_app.tests import test_sql_app + from docs_src.sql_databases.sql_app.tests import test_sql_app # Ensure import side effects are re-executed importlib.reload(test_sql_app) diff --git a/tests/test_tutorial/test_sql_databases_peewee/test_sql_databases_peewee.py b/tests/test_tutorial/test_sql_databases_peewee/test_sql_databases_peewee.py index ab6eca450..08cfa5ca9 100644 --- a/tests/test_tutorial/test_sql_databases_peewee/test_sql_databases_peewee.py +++ b/tests/test_tutorial/test_sql_databases_peewee/test_sql_databases_peewee.py @@ -332,7 +332,7 @@ openapi_schema = { @pytest.fixture(scope="module") def client(): # Import while creating the client to create the DB after starting the test session - from sql_databases_peewee.sql_app.main import app + from docs_src.sql_databases_peewee.sql_app.main import app test_db = Path("./test.db") with TestClient(app) as c: diff --git a/tests/test_tutorial/test_templates/test_tutorial001.py b/tests/test_tutorial/test_templates/test_tutorial001.py index 86d61bfc7..bfee5c090 100644 --- a/tests/test_tutorial/test_templates/test_tutorial001.py +++ b/tests/test_tutorial/test_templates/test_tutorial001.py @@ -1,12 +1,17 @@ +import os import shutil from fastapi.testclient import TestClient def test_main(): + if os.path.isdir("./static"): # pragma: nocover + shutil.rmtree("./static") + if os.path.isdir("./templates"): # pragma: nocover + shutil.rmtree("./templates") shutil.copytree("./docs_src/templates/templates/", "./templates") shutil.copytree("./docs_src/templates/static/", "./static") - from templates.tutorial001 import app + from docs_src.templates.tutorial001 import app client = TestClient(app) response = client.get("/items/foo") diff --git a/tests/test_tutorial/test_testing/test_main.py b/tests/test_tutorial/test_testing/test_main.py index 6ef800f6e..e6747fffd 100644 --- a/tests/test_tutorial/test_testing/test_main.py +++ b/tests/test_tutorial/test_testing/test_main.py @@ -1,4 +1,4 @@ -from app_testing.test_main import client, test_read_main +from docs_src.app_testing.test_main import client, test_read_main openapi_schema = { "openapi": "3.0.2", diff --git a/tests/test_tutorial/test_testing/test_tutorial001.py b/tests/test_tutorial/test_testing/test_tutorial001.py index 66863b945..7dea477f0 100644 --- a/tests/test_tutorial/test_testing/test_tutorial001.py +++ b/tests/test_tutorial/test_testing/test_tutorial001.py @@ -1,4 +1,4 @@ -from app_testing.tutorial001 import client, test_read_main +from docs_src.app_testing.tutorial001 import client, test_read_main openapi_schema = { "openapi": "3.0.2", diff --git a/tests/test_tutorial/test_testing/test_tutorial002.py b/tests/test_tutorial/test_testing/test_tutorial002.py index c1e1e4ebf..ec4f91ee7 100644 --- a/tests/test_tutorial/test_testing/test_tutorial002.py +++ b/tests/test_tutorial/test_testing/test_tutorial002.py @@ -1,4 +1,4 @@ -from app_testing.tutorial002 import test_read_main, test_websocket +from docs_src.app_testing.tutorial002 import test_read_main, test_websocket def test_main(): diff --git a/tests/test_tutorial/test_testing/test_tutorial003.py b/tests/test_tutorial/test_testing/test_tutorial003.py index 074c42a4c..d9e16390e 100644 --- a/tests/test_tutorial/test_testing/test_tutorial003.py +++ b/tests/test_tutorial/test_testing/test_tutorial003.py @@ -1,4 +1,4 @@ -from app_testing.tutorial003 import test_read_items +from docs_src.app_testing.tutorial003 import test_read_items def test_main(): diff --git a/tests/test_tutorial/test_testing_dependencies/test_tutorial001.py b/tests/test_tutorial/test_testing_dependencies/test_tutorial001.py index dccace562..af26307f5 100644 --- a/tests/test_tutorial/test_testing_dependencies/test_tutorial001.py +++ b/tests/test_tutorial/test_testing_dependencies/test_tutorial001.py @@ -1,4 +1,4 @@ -from dependency_testing.tutorial001 import ( +from docs_src.dependency_testing.tutorial001 import ( app, client, test_override_in_items, diff --git a/tests/test_tutorial/test_websockets/test_tutorial001.py b/tests/test_tutorial/test_websockets/test_tutorial001.py index 6c715b194..7dbecb265 100644 --- a/tests/test_tutorial/test_websockets/test_tutorial001.py +++ b/tests/test_tutorial/test_websockets/test_tutorial001.py @@ -1,7 +1,8 @@ import pytest from fastapi.testclient import TestClient from fastapi.websockets import WebSocketDisconnect -from websockets.tutorial001 import app + +from docs_src.websockets.tutorial001 import app client = TestClient(app) diff --git a/tests/test_tutorial/test_websockets/test_tutorial002.py b/tests/test_tutorial/test_websockets/test_tutorial002.py index 8619a3cb9..7c56eb260 100644 --- a/tests/test_tutorial/test_websockets/test_tutorial002.py +++ b/tests/test_tutorial/test_websockets/test_tutorial002.py @@ -1,7 +1,8 @@ import pytest from fastapi.testclient import TestClient from fastapi.websockets import WebSocketDisconnect -from websockets.tutorial002 import app + +from docs_src.websockets.tutorial002 import app client = TestClient(app)