Production API Standards

The Standard for Production APIs

Up to this point, we have focused on building features using smart AI agents. We've learned how to organize tasks and run development tracks in parallel. However, in the professional world, writing code that just works is only half the battle. To make an API "production-ready," we must ensure it can handle mistakes, block hackers, and stay fast when many people use it at once.

We do this using a Quality Pipeline. This is a series of checks that every piece of code must pass before it reaches our users. Think of it like a safety inspection for a car. It doesn't matter how fast the car is if the brakes don't work or the doors don't lock.

The Quality Pipeline focuses on four main areas:

  • Coverage: Do our tests check every single line of code, including the parts where things go wrong?
  • Security: Can a user access or delete data that belongs to someone else?
  • Performance: Does the API stay fast when 50 people use it at the same time?
  • Documentation: Is the instruction manual (OpenAPI) up to date?

In this lesson, we will move through each of these stages to finish our Task Comments feature.

Reaching 95% Test Coverage

Test coverage tells us what percentage of our code is actually executed during our tests. If you have 90% coverage, it means 10% of your code has never been tested. Usually, that 10% contains the error paths — the code that runs when a user makes a mistake. Our goal for production is usually 95% or higher.

First, we check our current status using a tool called pytest-cov. On CodeSignal, this is already set up for you. You can run this command in your terminal:

pytest --cov=src/services/comment_service.py --cov-report=term tests/

The output might look like this:

Name                            Stmts   Miss  Cover
---------------------------------------------------
src/services/comment_service.py    50      5    90%
---------------------------------------------------
TOTAL                              50      5    90%

This tells us we are missing 5 lines. To fix this, we need to add tests for edge cases. Let's start by testing if our service correctly rejects a comment that is too long.

# tests/unit/test_comment_service.py

def test_create_comment_too_long():
    service = CommentService()
    # Create a string that is 5001 characters long
    long_content = "a" * 5001
    
    # We expect this to raise a ValueError
    with pytest.raises(ValueError):
        service.create_comment(task_id=1, content=long_content, user_id=1)

In this snippet, we use pytest.raises(ValueError) to tell our test that we expect an error. If the code doesn't crash, the test fails. This checks the boundary of our input limits.

Next, we can add a test for a race condition — what happens if two comments are created at the exact same time? While we won't write the full complex logic here, we add tests that try to trigger these specific scenarios. After adding these missing pieces (like empty content or unauthorized users), we run our coverage again.

pytest --cov=src/services/comment_service.py --cov-report=term tests/
Name                            Stmts   Miss  Cover
---------------------------------------------------
src/services/comment_service.py    50      2    96%
---------------------------------------------------
TOTAL                              50      2    96%

By identifying the gaps and writing specific tests for them, we've moved from "pretty good" to "production-ready" coverage.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal