Skip to content
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

Merged
merged 7 commits into from
Feb 9, 2024
Merged

Article: Testing FastAPI Applications #510

merged 7 commits into from
Feb 9, 2024

Conversation

jerivas
Copy link
Member

@jerivas jerivas commented Feb 8, 2024

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.

Copy link

github-actions bot commented Feb 8, 2024

View diff of compiled files (may take a few minutes): https://github.com/oddbird/oddleventy-built/compare/main..testing-fastapi

- Testing
- FastAPI
# image:
# src:
Copy link
Member Author

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@jamesnw jamesnw left a 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
Copy link
Contributor

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.

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
Copy link
Contributor

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?

[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
Copy link
Contributor

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)
Copy link
Contributor

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.

assert item.name == "Item 1"
```

The `db` fixture allows to make queries in the context of the test transaction
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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):
Copy link
Contributor

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?

return client
```

_`client.user = user` is a convenience to access the user instance in tests,
Copy link
Contributor

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.

- Testing
- FastAPI
# image:
# src:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jerivas
Copy link
Member Author

jerivas commented Feb 8, 2024

@jamesnw thanks for the excellent feedback. I realized an introduction to pytest fixtures was necessary, so I added that (and other improvements) to 2fb9017. Please take another look and let me know what you think

@jamesnw
Copy link
Contributor

jamesnw commented Feb 9, 2024

Nice updates! Thanks for clarifying those things for me!

Copy link
Member

@jgerigmeyer jgerigmeyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

@jerivas jerivas merged commit 9412162 into main Feb 9, 2024
10 checks passed
@jerivas jerivas deleted the testing-fastapi branch February 9, 2024 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Testing a FastAPI application
3 participants