VanaAPI

← Writing

A first test file for a Python delivery fee

Daily workEN2026-10-28

A small Python function is a useful place to begin with unit testing. You can understand its inputs, describe its expected output, and run its tests without setting up a whole application. An AI assistant can draft those tests, but the important decisions still belong to you: what the function promises, which examples matter, and whether a passing test means anything useful.

§ Start with a clear rule

Suppose a file named shipping.py contains a function called delivery_fee(subtotal). Its rule is simple: orders below 50 have a delivery fee of 5, and orders of 50 or more have no delivery fee. A negative subtotal should raise ValueError.

These rules are the specification: the behavior you want the code to follow. Write them down before asking for tests. If you provide only the implementation, an assistant may produce tests that repeat its current behavior, including any mistakes.

For example, an implementation might accidentally charge delivery on an order of exactly 50. A useful test should catch that error, not preserve it as the expected result.

§ Ask for a small, readable test file

Give the assistant the function, its filename, and the rules. Ask for pytest tests, since pytest lets beginners write checks using ordinary Python assertions.

A practical prompt is: “Generate beginner-friendly pytest unit tests for delivery_fee in shipping.py. The fee is 5 below 50 and 0 at or above 50. Negative subtotals must raise ValueError. Include ordinary inputs, zero, the threshold, and a negative input. Import the function rather than copying it. Do not change the implementation or invent rules for unsupported input types.”

Paste the function after that request. Remove credentials, private customer data, and unrelated code first. If the function depends on other parts of your project, include only the context needed to understand those dependencies.

You can make this request through an existing coding assistant or an OpenAI SDK connected to Vana API at https://api.vanaapi.com. Vana API provides one OpenAI-compatible endpoint and also supports Anthropic Messages and Gemini native protocols. If you want to use it for this workflow, create a Vana API account and configure your client's API key and base URL.

§ Read the assertions before running them

A unit test checks one small piece of behavior. In this example, the generated file should import delivery_fee from shipping, then define test functions whose names begin with test_.

A simple test could be written as def test_free_delivery_at_threshold(): assert delivery_fee(50) == 0. This is valid Python, although an assistant will usually put the assertion on a separate, indented line.

Check that the proposed cases include:

  • An ordinary subtotal, such as 20, which should return 5.
  • Zero, which should also return 5 under the stated rule.
  • Exactly 50, which should return 0.
  • An amount above 50, such as 75, which should return 0.
  • A negative subtotal, checked with pytest.raises(ValueError).

Do not accept extra expectations automatically. Whether strings or missing values should raise a particular exception is a separate design decision unless your specification already covers it.

§ Run the tests and investigate failures

In your project's virtual environment, install pytest with python -m pip install pytest. Save the generated tests as test_shipping.py beside shipping.py, then run python -m pytest from that directory.

A failure is information, not proof that the implementation is wrong. Compare the failing assertion with your written rule. The problem could be an incorrect expected value, a mistaken import, or a genuine bug in the function.

§ Check that the tests can catch a mistake

Once everything passes, temporarily change the threshold comparison so that exactly 50 is charged a fee. The threshold test should fail. Restore the correct code afterward.

This small experiment helps distinguish useful tests from tests that merely run. Keep the final test file in version control with your code, and rerun it whenever the function changes. The goal is not a large generated suite. It is a short, understandable record of the behavior you intend to protect.