mirror of https://github.com/tiangolo/fastapi.git
✔ Improve support for tests in editor (#1699)
* ♻️ Remove required extra steps to test in editor * 🎨 Format lint script * 📝 Remove obsolete extra steps required to test in editor from docs * 🐛 Fix coverage
This commit is contained in:
parent
250fa519f9
commit
600f15faa0
|
|
@ -499,13 +499,3 @@ $ bash scripts/test-cov-html.sh
|
||||||
</div>
|
</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.
|
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
|
|
||||||
```
|
|
||||||
|
|
|
||||||
|
|
@ -498,13 +498,3 @@ $ bash scripts/test-cov-html.sh
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
该命令生成了一个 `./htmlcov/` 目录,如果你在浏览器中打开 `./htmlcov/index.html` 文件,你可以交互式地浏览被测试所覆盖的代码区块,并注意是否缺少了任何区块。
|
该命令生成了一个 `./htmlcov/` 目录,如果你在浏览器中打开 `./htmlcov/index.html` 文件,你可以交互式地浏览被测试所覆盖的代码区块,并注意是否缺少了任何区块。
|
||||||
|
|
||||||
### 在编辑器中测试
|
|
||||||
|
|
||||||
如果你想要在编辑器中运行集成测试,请将 `./docs_src` 加入到你的 `PYTHONPATH` 变量中。
|
|
||||||
|
|
||||||
例如,在 VS Code 中你可以创建一个包含以下内容的 `.env` 文件:
|
|
||||||
|
|
||||||
```env
|
|
||||||
PYTHONPATH=./docs_src
|
|
||||||
```
|
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ def client():
|
||||||
static_dir: Path = Path(os.getcwd()) / "static"
|
static_dir: Path = Path(os.getcwd()) / "static"
|
||||||
print(static_dir)
|
print(static_dir)
|
||||||
static_dir.mkdir(exist_ok=True)
|
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:
|
with TestClient(app) as client:
|
||||||
yield client
|
yield client
|
||||||
|
|
|
||||||
|
|
@ -288,7 +288,7 @@ def client():
|
||||||
if test_db.is_file(): # pragma: nocover
|
if test_db.is_file(): # pragma: nocover
|
||||||
test_db.unlink()
|
test_db.unlink()
|
||||||
# Import while creating the client to create the DB after starting the test session
|
# 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
|
# Ensure import side effects are re-executed
|
||||||
importlib.reload(main)
|
importlib.reload(main)
|
||||||
|
|
|
||||||
|
|
@ -288,7 +288,7 @@ def client():
|
||||||
if test_db.is_file(): # pragma: nocover
|
if test_db.is_file(): # pragma: nocover
|
||||||
test_db.unlink()
|
test_db.unlink()
|
||||||
# Import while creating the client to create the DB after starting the test session
|
# 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
|
# Ensure import side effects are re-executed
|
||||||
importlib.reload(alt_main)
|
importlib.reload(alt_main)
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ def test_testing_dbs():
|
||||||
if test_db.is_file(): # pragma: nocover
|
if test_db.is_file(): # pragma: nocover
|
||||||
test_db.unlink()
|
test_db.unlink()
|
||||||
# Import while creating the client to create the DB after starting the test session
|
# 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
|
# Ensure import side effects are re-executed
|
||||||
importlib.reload(test_sql_app)
|
importlib.reload(test_sql_app)
|
||||||
|
|
|
||||||
|
|
@ -332,7 +332,7 @@ openapi_schema = {
|
||||||
@pytest.fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
def client():
|
def client():
|
||||||
# Import while creating the client to create the DB after starting the test session
|
# 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")
|
test_db = Path("./test.db")
|
||||||
with TestClient(app) as c:
|
with TestClient(app) as c:
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,17 @@
|
||||||
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
|
||||||
def test_main():
|
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/templates/", "./templates")
|
||||||
shutil.copytree("./docs_src/templates/static/", "./static")
|
shutil.copytree("./docs_src/templates/static/", "./static")
|
||||||
from templates.tutorial001 import app
|
from docs_src.templates.tutorial001 import app
|
||||||
|
|
||||||
client = TestClient(app)
|
client = TestClient(app)
|
||||||
response = client.get("/items/foo")
|
response = client.get("/items/foo")
|
||||||
|
|
|
||||||
|
|
@ -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_schema = {
|
||||||
"openapi": "3.0.2",
|
"openapi": "3.0.2",
|
||||||
|
|
|
||||||
|
|
@ -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_schema = {
|
||||||
"openapi": "3.0.2",
|
"openapi": "3.0.2",
|
||||||
|
|
|
||||||
|
|
@ -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():
|
def test_main():
|
||||||
|
|
|
||||||
|
|
@ -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():
|
def test_main():
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
from dependency_testing.tutorial001 import (
|
from docs_src.dependency_testing.tutorial001 import (
|
||||||
app,
|
app,
|
||||||
client,
|
client,
|
||||||
test_override_in_items,
|
test_override_in_items,
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
import pytest
|
import pytest
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
from fastapi.websockets import WebSocketDisconnect
|
from fastapi.websockets import WebSocketDisconnect
|
||||||
from websockets.tutorial001 import app
|
|
||||||
|
from docs_src.websockets.tutorial001 import app
|
||||||
|
|
||||||
client = TestClient(app)
|
client = TestClient(app)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
import pytest
|
import pytest
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
from fastapi.websockets import WebSocketDisconnect
|
from fastapi.websockets import WebSocketDisconnect
|
||||||
from websockets.tutorial002 import app
|
|
||||||
|
from docs_src.websockets.tutorial002 import app
|
||||||
|
|
||||||
client = TestClient(app)
|
client = TestClient(app)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue