-
-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Article: Testing FastAPI Applications #510
Conversation
View diff of compiled files (may take a few minutes): https://github.com/oddbird/oddleventy-built/compare/main..testing-fastapi |
content/blog/2024/testing-fastapi.md
Outdated
- Testing | ||
- FastAPI | ||
# image: | ||
# src: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't find an image on Unsplash that made sense for this article. Any suggestions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work! A few questions and comments, feel free to disregard if they are things Python devs would be assumed to know already.
``` | ||
|
||
This [pytest fixture] is scoped to the test `"session"`, which means it will be | ||
automatically created once and shared across all tests. We start by obtaining a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You address this later, but sharing a database made me worry about shared state between tests. Maybe add "Don't worry about sharing state between tests- we'll make sure the database is clean for each test in a later step."
Also, I'm wondering if there is something to say about the starting state of the database? For instance, if you are wanting to test a ListDocuments
endpoint, do you add test documents in the test for that endpoint, or to this Session-scoped Fixture? Or maybe that's out of scope for this article.
content/blog/2024/testing-fastapi.md
Outdated
leveraging the powerful testing capabilities provided by FastAPI and pytest. | ||
|
||
To further explore testing in FastAPI applications, we recommend referring to | ||
the [FastAPI] and [`pytest`] documentation for in-depth explanations and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to link to https://fastapi.tiangolo.com/tutorial/testing/ directly?
content/blog/2024/testing-fastapi.md
Outdated
[pytest fixture]: https://docs.pytest.org/en/stable/explanation/fixtures.html | ||
|
||
We then `try` to drop the tables from the test database. If the database doesn't | ||
exist, we create it. Finally, we create all the tables in the test database. We |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of curiosity, if we are creating the database once per session, in what situations would the database already exist?
|
||
from app.main import app, get_db | ||
|
||
@pytest.fixture(autouse=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't a Session fixture, so it runs once for every test, correct? I think it would be worth pointing that out- it wasn't clear to me initially why the database was set up once in engine
but reset everytime here.
content/blog/2024/testing-fastapi.md
Outdated
assert item.name == "Item 1" | ||
``` | ||
|
||
The `db` fixture allows to make queries in the context of the test transaction |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The `db` fixture allows to make queries in the context of the test transaction | |
The `db` fixture allows you to make queries in the context of the test transaction |
```python | ||
# File: test_api.py | ||
|
||
def test_protected_endpoint(client): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly out of curiosity, am I understanding this correctly? You declare a def in conftest.py
called client
. Then, in your test file, you name your parameter the same thing as the def, and then the value gets passed in? In other words, if you wrote def test_protected_endpoint(anon_client)
here, it would pass the anon client? Or if you wrote def test_protected_endpoint(db)
it would insert the db
you added in conftest.py?
content/blog/2024/testing-fastapi.md
Outdated
return client | ||
``` | ||
|
||
_`client.user = user` is a convenience to access the user instance in tests, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add an example of how this could be used? Otherwise, I think this note and the client.user
lines could be removed to narrow the focus in the article.
content/blog/2024/testing-fastapi.md
Outdated
- Testing | ||
- FastAPI | ||
# image: | ||
# src: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice updates! Thanks for clarifying those things for me! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏
Related Issue(s)
Closes #509
Steps to test/reproduce
https://deploy-preview-510--oddleventy.netlify.app/2024/02/09/testing-fastapi/
Show me
Provide screenshots/animated gifs/videos if necessary.