Skip to main content
In this lesson, we explore how fixture scopes affect tests for user creation and login in a FastAPI application. We will start with testing user creation, move on to login functionality, and then examine how different fixture scopes (function, module, and session) impact test behavior.

Testing User Creation

The following test case demonstrates how to create a new user. We send a POST request to the “/users/” endpoint with an email and password. The response is then deserialized into a UserOut schema and validated:
Earlier versions of this test might have only checked the status code:
If there’s an issue with the assertions, for example:
feel free to add additional assertions as needed for more robust testing.

Transitioning to Login

Next, we address the login functionality with the test_login_user test case. This test depends on the client fixture to send requests to the login endpoint. Note that the login route is defined as /login (without a trailing slash), so our test request must reflect that configuration. Initially, the code snippet for login testing might have been incomplete (missing the client parameter):
After including the client injection and adapting the route, the updated test code becomes:
For authentication, the login endpoint does not accept JSON. Instead, form data should be sent. Additionally, the field name should be “username” (not “email”).
To simulate a proper login, update the test to send form data as follows:
If you run this test and encounter a 422 error, review the error message to ensure that the correct field (“username”) is being used. Adjusting the payload accordingly should resolve the issue.

Debugging Login Issues

If the login test returns a 403 error with the detail “Invalid Credentials,” it may indicate one of the following:
  • The user does not exist in the database.
  • The provided password does not match the record in the database.
Review the login route in your authentication module (auth.py) for clarity:
Ensure the test payload uses the field name “username” to match this implementation.

Understanding Fixture Scopes

Our tests make use of a client fixture, which in turn relies on a session fixture to interact with the database. Consider the following session fixture:
And the corresponding client fixture:
By default, these fixtures use the function scope, meaning they are recreated for each test. This behavior explains why a user created in test_create_user does not exist when test_login_user is run independently—each test starts with a fresh database.

Fixture Scopes Explained

Changing to module or session scope can cause tests to pass or fail based on their order. Reliable tests should always set up their own data independently without relying on state changes from other tests.

Final Test Code Example

Below is the final test code with proper scopes and correct data handling:
While it might be tempting to tweak fixture scopes (e.g., set them to module or session scopes) to share state between tests, isolating each test is best practice. This prevents cascading failures and ensures that each test validates only its own functionality.

Final Thoughts

This lesson demonstrated how to create independent and reliable tests for user creation and login in a FastAPI application by correctly handling fixture scopes. In the next part of the lesson, we will explore strategies for generating independent test data for login without relying on previously created users. Happy Testing!

Watch Video