]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
✔ Improve support for tests in editor (#1699)
authorSebastián Ramírez <tiangolo@gmail.com>
Fri, 10 Jul 2020 09:08:19 +0000 (11:08 +0200)
committerGitHub <noreply@github.com>
Fri, 10 Jul 2020 09:08:19 +0000 (11:08 +0200)
* ♻️ Remove required extra steps to test in editor

* 🎨 Format lint script

* 📝 Remove obsolete extra steps required to test in editor from docs

* 🐛 Fix coverage

17 files changed:
.env [deleted file]
docs/en/docs/contributing.md
docs/zh/docs/contributing.md
scripts/lint.sh
tests/test_tutorial/test_extending_openapi/test_tutorial002.py
tests/test_tutorial/test_sql_databases/test_sql_databases.py
tests/test_tutorial/test_sql_databases/test_sql_databases_middleware.py
tests/test_tutorial/test_sql_databases/test_testing_databases.py
tests/test_tutorial/test_sql_databases_peewee/test_sql_databases_peewee.py
tests/test_tutorial/test_templates/test_tutorial001.py
tests/test_tutorial/test_testing/test_main.py
tests/test_tutorial/test_testing/test_tutorial001.py
tests/test_tutorial/test_testing/test_tutorial002.py
tests/test_tutorial/test_testing/test_tutorial003.py
tests/test_tutorial/test_testing_dependencies/test_tutorial001.py
tests/test_tutorial/test_websockets/test_tutorial001.py
tests/test_tutorial/test_websockets/test_tutorial002.py

diff --git a/.env b/.env
deleted file mode 100644 (file)
index 7032423..0000000
--- a/.env
+++ /dev/null
@@ -1 +0,0 @@
-PYTHONPATH=./docs_src
index 416bc32f84838b099196ef2de7e6e760fe4622f4..cd87faa4eb77e2a28a1b6d124e161aecf8cdd5ef 100644 (file)
@@ -499,13 +499,3 @@ $ bash scripts/test-cov-html.sh
 </div>
 
 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
-```
index e9645392e7736b27615bd00bb7ae0237b432b85d..402668c47e4b6b43309f8ef1cdd761ca447dff36 100644 (file)
@@ -498,13 +498,3 @@ $ bash scripts/test-cov-html.sh
 </div>
 
 该命令生成了一个 `./htmlcov/` 目录,如果你在浏览器中打开 `./htmlcov/index.html` 文件,你可以交互式地浏览被测试所覆盖的代码区块,并注意是否缺少了任何区块。 
-
-### 在编辑器中测试
-
-如果你想要在编辑器中运行集成测试,请将 `./docs_src` 加入到你的 `PYTHONPATH` 变量中。
-
-例如,在 VS Code 中你可以创建一个包含以下内容的 `.env` 文件:
-
-```env
-PYTHONPATH=./docs_src
-```
index 43a1b0c1db544797addd42fc38159fc935cda41b..12fb359e439b8a095d8ad0ac3cd61c051df3336e 100755 (executable)
@@ -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
index 06fb62f85249f8a437876a18f9b7251a1e9536d8..654db2e4c8ab8efc21ff227d6763c52874bc023e 100644 (file)
@@ -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
index 69092889ca32f444c7b223200ea922bd9c192a84..cd3f13e7a9987a60745f29e24c0f24ae4b045262 100644 (file)
@@ -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)
index 6b4ed567ed35376b6e0cf5c0e84c44b849e4f3b4..b02e1c89ed9226b0595376384b7c0140d7f3ec92 100644 (file)
@@ -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)
index 83a156b8509bde0e6fb6f556407a04c5a9d5ee25..9acfd8908ff1f5c96db860861d2542a1153b465f 100644 (file)
@@ -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)
index ab6eca45094b4b1450e50310708a78dde4aa8dde..08cfa5ca9278cbed57256ad401ce28f5e1a79eeb 100644 (file)
@@ -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:
index 86d61bfc7ae9485c3822cab6dba47c34a42f2614..bfee5c0902dbc30fdb43749599bf9f625ee7a62a 100644 (file)
@@ -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")
index 6ef800f6ed44ef2477120e94db9b1215fc9c188b..e6747fffd0a1ba79bb1747cc133c47238ca451a3 100644 (file)
@@ -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",
index 66863b94512f5f96b2c78ef89c64eba9c482d05e..7dea477f0e1fd971400b25727e8940b7d821c933 100644 (file)
@@ -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",
index c1e1e4ebf1a6c0956ee3420cd135c9d9ec86ad48..ec4f91ee7f04793a81297a0b3bb217c2c170deec 100644 (file)
@@ -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():
index 074c42a4cb3865f798cc87b524335dc1766f7aed..d9e16390ed4ecc67302bb25ab09df48af0250569 100644 (file)
@@ -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():
index dccace562e2d0c71f2aece22fa698501c7834230..af26307f54c41edbc807fcd56703e313fc390c56 100644 (file)
@@ -1,4 +1,4 @@
-from dependency_testing.tutorial001 import (
+from docs_src.dependency_testing.tutorial001 import (
     app,
     client,
     test_override_in_items,
index 6c715b19495a2e5e6ebe05ab80f2d8bf2e38fa2c..7dbecb26502eebdcca01326dc7bb4ee6c1f93723 100644 (file)
@@ -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)
 
index 8619a3cb945a067800fdd2756e801b051f7c926a..7c56eb26057e9aae4ab8cfb3742ebb38cdc4307a 100644 (file)
@@ -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)