]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
πŸ“ Update docs for testing, fix examples with relative imports (#5302)
authorSebastiΓ‘n RamΓ­rez <tiangolo@gmail.com>
Wed, 24 Aug 2022 14:18:11 +0000 (16:18 +0200)
committerGitHub <noreply@github.com>
Wed, 24 Aug 2022 14:18:11 +0000 (16:18 +0200)
docs/en/docs/advanced/async-tests.md
docs/en/docs/tutorial/testing.md

index d5116233f8c555a089734f30589c6d24f624742f..e34f48f1732faa8ec04cdd7ec91e00596163fe02 100644 (file)
@@ -26,13 +26,23 @@ The important difference for us is that with HTTPX we are not limited to synchro
 
 ## Example
 
-For a simple example, let's consider the following `main.py` module:
+For a simple example, let's consider a file structure similar to the one described in [Bigger Applications](../tutorial/bigger-applications.md){.internal-link target=_blank} and [Testing](../tutorial/testing.md){.internal-link target=_blank}:
+
+```
+.
+β”œβ”€β”€ app
+β”‚Β Β  β”œβ”€β”€ __init__.py
+β”‚Β Β  β”œβ”€β”€ main.py
+β”‚Β Β  β””── test_main.py
+```
+
+The file `main.py` would have:
 
 ```Python
 {!../../../docs_src/async_tests/main.py!}
 ```
 
-The `test_main.py` module that contains the tests for `main.py` could look like this now:
+The file `test_main.py` would have the tests for `main.py`, it could look like this now:
 
 ```Python
 {!../../../docs_src/async_tests/test_main.py!}
index 79ea2b1ab58b8499c2ce95d05b13ab929e4bb167..d2ccd7dc77d45a8098a97cb98b502ff3efa8b120 100644 (file)
@@ -50,7 +50,17 @@ And your **FastAPI** application might also be composed of several files/modules
 
 ### **FastAPI** app file
 
-Let's say you have a file `main.py` with your **FastAPI** app:
+Let's say you have a file structure as described in [Bigger Applications](./bigger-applications.md){.internal-link target=_blank}:
+
+```
+.
+β”œβ”€β”€ app
+β”‚Β Β  β”œβ”€β”€ __init__.py
+β”‚Β Β  β””── main.py
+```
+
+In the file `main.py` you have your **FastAPI** app:
+
 
 ```Python
 {!../../../docs_src/app_testing/main.py!}
@@ -58,18 +68,40 @@ Let's say you have a file `main.py` with your **FastAPI** app:
 
 ### Testing file
 
-Then you could have a file `test_main.py` with your tests, and import your `app` from the `main` module (`main.py`):
+Then you could have a file `test_main.py` with your tests. It could live on the same Python package (the same directory with a `__init__.py` file):
 
-```Python
+``` hl_lines="5"
+.
+β”œβ”€β”€ app
+β”‚Β Β  β”œβ”€β”€ __init__.py
+β”‚Β Β  β”œβ”€β”€ main.py
+β”‚Β Β  β””── test_main.py
+```
+
+Because this file is in the same package, you can use relative imports to import the object `app` from the `main` module (`main.py`):
+
+```Python hl_lines="3"
 {!../../../docs_src/app_testing/test_main.py!}
 ```
 
+...and have the code for the tests just like before.
+
 ## Testing: extended example
 
 Now let's extend this example and add more details to see how to test different parts.
 
 ### Extended **FastAPI** app file
 
+Let's continue with the same file structure as before:
+
+```
+.
+β”œβ”€β”€ app
+β”‚Β Β  β”œβ”€β”€ __init__.py
+β”‚Β Β  β”œβ”€β”€ main.py
+β”‚Β Β  β””── test_main.py
+```
+
 Let's say that now the file `main.py` with your **FastAPI** app has some other **path operations**.
 
 It has a `GET` operation that could return an error.