Upskill tutorial for API unit tests (advanced)

Hud Wahab
4 min readJul 26, 2023

--

Day 24: FastAPI. SQL in-memory db. Coverage.

Hi 👋 I am Hud, a postdoc for engineering data science at the AI Manufacturing Center in Laramie, Wyoming. My funding is running out (AaAaaA !), so while I am actively looking for a new job, instead of doing the 205th coding certificate to prove my worthiness — I thought I’d do design challenges and document how I spend my time upskilling so other engineers can do the same.

Nowadays, certificates are everywhere. Documenting small upskill projects that you can later show off is the best way to get recognition as a professional engineer.

This is day 24 of a 30-day design challenge. Follow along and let me know if you get stuck!

TL;DR tasks

Download the provided code for the challenge.

Write unit test cases to cover the following scenarios for the API:

  1. Test retrieving and deleting an event by an ID that doesn’t exist in the database.
  2. Test buying a ticket when there are no available tickets for the event.
  3. Write additional test cases as needed to cover other edge cases.

The problem

There’s not really much of a problem, just covering more types of tests from our previous challenge. If you have not done that, I recommend you do that first.

Let’s see what other types of tests we can come up with.

The three test methods here are testing different scenarios, which is good. The first two test methods are testing the scenario where an event with a given ID does not exist, which is important to ensure that the API returns the correct HTTP status code (404 in this case) and an appropriate error message. The third test method is testing the scenario where there are no available tickets for an event, which is also important to ensure that the API returns the correct HTTP status code (400 in this case) and an appropriate error message.

However, it is important to also test the scenarios where the event with the given ID does exist, and where the event ID is not valid (e.g. a string instead of an integer). Additionally, it is important to test other endpoints and scenarios, such as creating an event, updating an event, and creating a ticket for an event.

In any case, once we have the in-memory database set up for testing, app.dependency_overrides[get_db] = override_get_db , we can simply add as many as we want. Feel free to add more, and let me know what you’ve added in the comments.

Here are 10 best practices for API testing:

  1. Test all endpoints: Make sure to test all endpoints of the API, including GET, POST, PUT, DELETE, and any other custom endpoints.
  2. Test all scenarios: Test all possible scenarios for each endpoint, including valid and invalid inputs, edge cases, and error conditions.
  3. Use automated testing: Use automated testing tools and frameworks to automate the testing process and ensure consistent and reliable results.
  4. Use mock data: Use mock data to simulate real-world scenarios and ensure that the API behaves correctly under different conditions.
  5. Test performance: Test the performance of the API under different loads and conditions to ensure that it can handle high traffic and usage.
  6. Test security: Test the security of the API to ensure that it is protected against common security threats, such as SQL injection, cross-site scripting (XSS), and cross-site request forgery (CSRF).
  7. Test documentation: Test the documentation of the API to ensure that it is accurate, complete, and up-to-date.
  8. Test compatibility: Test the compatibility of the API with different platforms, devices, and browsers to ensure that it works correctly across different environments.
  9. Test versioning: Test the versioning of the API to ensure that it is backward compatible and that changes to the API do not break existing clients.
  10. Test error handling: Test the error handling of the API to ensure that it returns appropriate error messages and status codes for different error conditions.

Conclusion

Congratulations! You finished Day 24 from the 30-day design challenge.

If you have reached this far, you know how to:

  • Think about more best-practice API tests

Check out the day 25 challenge on how to separate API routing and database interactions!

Also, you can access the full 30-day GitHub repository here.

💡 My goal here is to help engineering data scientists upskill in design. I’d like to hear from you! Was this helpful? Anything I can improve? Connect with me on LinkedIn | Medium

--

--

Hud Wahab

🤖 Senior ML Engineer | Helping machine learning engineers design and productionize ML systems. | Let's connect: https://rb.gy/vb6au