This article provides a guide to setting up and running tests for voting functionality in applications, covering various scenarios and edge cases.
In this article, we set up and run tests for the voting functionality in your application. Our tests cover various scenarios including successful voting, duplicate vote attempts, vote deletions, and edge cases such as voting on non-existent posts or by unauthorized users. Before running these tests, ensure you have an authorized client and properly configured test posts in your test environment.
The initial test function retrieves all posts and validates them using the defined schema. This ensures that the posts returned by the API match the expected structure.
venv\lib\site-packages\aiofiles\os.py:10venv\lib\site-packages\aiofiles\os.py:10venv\lib\site-packages\aiofiles\os.py:10venv\lib\site-packages\aiofiles\os.py:10C:\Users\sanje\Documents\courses\fastapi\venv\lib\site-packages\aiofiles\os.py:10: DeprecationWarning: "@coroutine" decorator is deprecated since Python 3.8, use "async def" instead=========== 4 passed, 5 warnings in 2.17s ===========
After setting up the endpoint URL and required data (post ID and vote direction), we update the test to include the correct post ID:
Copy
Ask AI
def test_vote_on_post(authorized_client, test_posts): # Vote on the first post by sending the post's id authorized_client.post("/vote/", json={"post_id": test_posts[0].id})
Since a vote direction is needed (with 1 representing a like/upvote), the function is modified accordingly:
Copy
Ask AI
def test_vote_on_post(authorized_client, test_posts): # Vote on the first post with direction set to 1 (like/upvote) authorized_client.post("/vote/", json={"post_id": test_posts[0].id, "dir": 1})
In our final version, we simulate voting on a post owned by another user by selecting the fourth post from the list:
C:\users\sanje\documents\courses\fastapi\venv\lib\site-packages\aiofiles\os.py:10: DeprecationWarning: "@coroutine" decorator is deprecated since Python 3.8, use "async def" instead
Running the command:
Copy
Ask AI
pytest tests/test_posts.py -v -s
yields:
Copy
Ask AI
=================================== test session starts ====================================...4 passed, 5 warnings in 2.17s
To ensure proper error handling, the following test verifies that deleting a non-existent vote returns a 404 status code. Here, the test_vote fixture is not used so no vote exists for the given post:
When a user attempts to vote on a post that does not exist (using an ID that isn’t in the database), the API should return a 404 status code. The following test case ensures this behavior:
To verify security measures, the final voting test checks that an unauthorized user (i.e., using an unauthenticated client) cannot vote. This test should return a 401 status code:
Although not directly related to voting tests, the repository also includes tests for bank account operations. An example from the bank account test file is given below:
Copy
Ask AI
def test_bank_default_amount(zero_bank_account): print("Testing my bank account") assert zero_bank_account.balance == 0def test_withdraw(bank_account): bank_account.withdraw(20) assert bank_account.balance == 30
Before deleting the database.py file, ensure that all tests pass by running your complete test suite. The majority of functionality has now been moved to conftest.py.
An example of the test_posts fixture used throughout the tests is shown below:
When writing tests, consider additional scenarios that could potentially break the application by creating individual test cases for each situation. Comprehensive testing is essential for ensuring the stability and reliability of your application.
By following this guide, you have now set up a robust test suite covering various edge cases and scenarios for voting and other functionality. This comprehensive approach not only helps in maintaining functionality but also enhances the resilience of your application.