Prompt Engineering: Prompting for Tests
A test prompt asks the model to write a test for a function. A test calls the function and checks the answer.
A test for the shipped order
The prompt asks for a test of the normal case.
Write a Python test for this function.
def order_status(order_number):
orders = {1042: "shipped", 1043: "processing", 1044: "delivered"}
return orders.get(order_number, "unknown")
The test must check that order_status(1042) returns "shipped".A test for the unknown order
The prompt asks for a test of the unknown order.
Write a Python test for this function.
def order_status(order_number):
orders = {1042: "shipped", 1043: "processing", 1044: "delivered"}
return orders.get(order_number, "unknown")
The test must check that order_status(1045) returns "unknown".A test for every order
The prompt asks for one test with a check for each order.
Write a Python test for this function.
def order_status(order_number):
orders = {1042: "shipped", 1043: "processing", 1044: "delivered"}
return orders.get(order_number, "unknown")
The test must check order_status(1042) returns "shipped", order_status(1043) returns "processing", order_status(1044) returns "delivered" and order_status(1045) returns "unknown".Example
Write a Python test for this function.
def order_status(order_number):
orders = {1042: "shipped", 1043: "processing", 1044: "delivered"}
return orders.get(order_number, "unknown")
The test must check that order_status(1043) returns "processing" and that order_status(1045) returns "unknown".Key points
- A test is code that calls a function and checks the answer it gives.
- A test prompt names the function and the cases the test must cover.
- A good test covers the normal case and the unknown order. An unknown order number is the case the function must still handle.
- A test that passes is called green. A test that fails shows where the function is wrong.
- The prompt gives the function to the model. The model writes the test from the function.
This example runs the test prompt against the local model and shows the test it wrote.
Note
A test the model writes must be run. A test that is not run proves nothing.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
What does a good test cover?