Browse Source

✔ 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
pull/1702/head
Sebastián Ramírez 5 years ago
committed by GitHub
parent
commit
600f15faa0
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      .env
  2. 10
      docs/en/docs/contributing.md
  3. 10
      docs/zh/docs/contributing.md
  4. 2
      tests/test_tutorial/test_extending_openapi/test_tutorial002.py
  5. 2
      tests/test_tutorial/test_sql_databases/test_sql_databases.py
  6. 2
      tests/test_tutorial/test_sql_databases/test_sql_databases_middleware.py
  7. 2
      tests/test_tutorial/test_sql_databases/test_testing_databases.py
  8. 2
      tests/test_tutorial/test_sql_databases_peewee/test_sql_databases_peewee.py
  9. 7
      tests/test_tutorial/test_templates/test_tutorial001.py
  10. 2
      tests/test_tutorial/test_testing/test_main.py
  11. 2
      tests/test_tutorial/test_testing/test_tutorial001.py
  12. 2
      tests/test_tutorial/test_testing/test_tutorial002.py
  13. 2
      tests/test_tutorial/test_testing/test_tutorial003.py
  14. 2
      tests/test_tutorial/test_testing_dependencies/test_tutorial001.py
  15. 3
      tests/test_tutorial/test_websockets/test_tutorial001.py
  16. 3
      tests/test_tutorial/test_websockets/test_tutorial002.py

1
.env

@ -1 +0,0 @@
PYTHONPATH=./docs_src

10
docs/en/docs/contributing.md

@ -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
```

10
docs/zh/docs/contributing.md

@ -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
```

2
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

2
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)

2
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)

2
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)

2
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:

7
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")

2
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",

2
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",

2
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():

2
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():

2
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,

3
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)

3
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)

Loading…
Cancel
Save