Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
jgerigmeyer committed Feb 9, 2024
1 parent 2fb9017 commit 19fb04f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ on our recent experience converting an internal project from Django to FastAPI.
4. How To Use FastAPI Dependency Injection Everywhere (coming soon)

[SQLAlchemy for Django Developers]: /2023/10/23/sqlalchemy-for-django-developers/
[Testing FastAPI Applications]: /2024/02/08/testing-fastapi/
[Testing FastAPI Applications]: /2024/02/09/testing-fastapi/

## Why is FastAPI Worth Considering?

Expand Down
2 changes: 1 addition & 1 deletion content/blog/2023/sqlalchemy-for-django-developers.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ on our recent experience converting an internal project from Django to FastAPI.
4. How To Use FastAPI Dependency Injection Everywhere (coming soon)

[FastAPI Path Operations for Django Developers]: /2023/10/19/fastapi-path-operations-for-django-developers/
[Testing FastAPI Applications]: /2024/02/08/testing-fastapi/
[Testing FastAPI Applications]: /2024/02/09/testing-fastapi/

## About SQLAlchemy

Expand Down
21 changes: 11 additions & 10 deletions content/blog/2024/testing-fastapi.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Testing FastAPI Applications
author: ed
date: 2024-02-08
date: 2024-02-09
tags:
- Article
- Python
Expand Down Expand Up @@ -108,8 +108,8 @@ def fruit_collection():
]
```

All our tests can use the `fruit_collection` fixture to get a list of fruits to
test against:
All of our tests can use the `fruit_collection` fixture to get a list of fruits
to test against:

```python
# File: test_api.py
Expand All @@ -125,7 +125,7 @@ def test_smoothies(fruit_collection):
assert smoothies == ... # Some assertion about the `make_smoothie` function
```

Notice we don't need to import or call the fixture, we get the return value (a
Notice we don't need to import or call the fixture; we get the return value (a
list of `Fruit` instances) by adding a parameter to our tests. This is a simple
example, but in the next sections we will see how to use fixtures to factor out
complex setup logic for our FastAPI tests.
Expand Down Expand Up @@ -217,7 +217,8 @@ def my_endpoint(db: Session = Depends(get_db)):

Notice the chain of dependencies starting from the `my_endpoint` function:

1. `my_endpoint` uses a `db` parameter provided by `get_db` (wrapped in `Depends`).
1. `my_endpoint` uses a `db` parameter provided by `get_db` (wrapped in
`Depends`).
2. `get_db` yields from `SessionLocal`.
3. `SessionLocal` is bound to the `engine` we created.
4. `engine` is created from the `DATABASE_URL` in our settings.
Expand Down Expand Up @@ -292,8 +293,8 @@ statement will run after the test is complete. In this case we make sure to
remove the override and close the connection to the database.

Because the `db` fixture is auto-used, we don't need to add it to our test
functions. However, we can add explicitly to assert the application state after
making requests to our endpoints:
functions. However, we can add it explicitly to assert the application state
after making requests to our endpoints:

```python
# File: test_api.py
Expand All @@ -309,7 +310,7 @@ def test_add_item(db):

The `db` fixture allows us to make queries in the context of the test
transaction, and is automatically rolled back after the test is complete. You
can also use it in other fixtures to create test data.
can also use it in other fixtures to create test data:

```python
# File: conftest.py
Expand Down Expand Up @@ -386,7 +387,7 @@ def client(db):

`client.user = user` is a convenience to access the user instance in tests. It's
useful to make assertions about the user's state after making requests to the
application.
application:

```python
# File: test_api.py
Expand Down Expand Up @@ -454,4 +455,4 @@ To further explore testing in FastAPI applications, we recommend referring to
the [FastAPI testing tutorial] and the [`pytest`] documentation for in-depth
explanations and additional examples.

[FastAPI testing tutorial]: https://fastapi.tiangolo.com/tutorial/testing/
[FastAPI testing tutorial]: https://fastapi.tiangolo.com/tutorial/testing/

0 comments on commit 19fb04f

Please sign in to comment.