Skip to main content
In this article, we explore tests for failed login scenarios. After confirming that valid login attempts work as intended, it is crucial to verify that incorrect or incomplete credentials are properly handled by the API.

Basic Incorrect Login Test

The following initial test implementation verifies that providing an incorrect password results in the expected response from the API.
The login endpoint is implemented as shown below:
When executing the tests, you might see output similar to:

Parameterized Tests for Multiple Scenarios

To thoroughly test the login functionality, we extend the tests to cover various failure cases. These include scenarios with a wrong email, wrong password, both credentials wrong, or even missing credentials. Pytest’s parameterization feature allows us to efficiently test these different cases.
Before adding parameterized tests, make sure you have imported Pytest in your test module:import pytest
Consider the following parameterized test implementation:
Running the tests with this approach should yield an output similar to:
Any warnings, such as deprecation warnings from aiofiles, will be listed after the test summaries.

Summary

In summary, we now have a comprehensive set of tests that ensure the API responds appropriately when invalid credentials are provided. Whether the issue stems from an incorrect password, a wrong email, or missing fields, the API should return the correct status code and error details. These tests provide a robust baseline for your application’s authentication logic, and they can be extended with additional assertions to verify other response fields as needed. Happy testing!

Watch Video