Skip to main content
Pytest is a Python testing framework. Braintrust integrates with pytest so you can run marked tests as experiments and inspect each test case as a traced span.

Setup

Install Braintrust alongside pytest:
Set your API key as an environment variable:

Run your first eval

Mark tests with @pytest.mark.braintrust, accept the braintrust_span fixture, and run pytest with --braintrust.
test_my_llm.py
Run the test:
Braintrust creates experiments from the marked tests, logs pass or fail as a score, and prints an experiment summary at the end of the run.

How it works

  • @pytest.mark.braintrust opts a test into Braintrust tracking.
  • braintrust_span gives you a standard Braintrust span for logging input, output, scores, metadata, and errors.
  • --braintrust enables experiment tracking for the session.
  • --braintrust-project and project=... on the marker control how tests are grouped into projects and experiments.
When --braintrust is not provided, braintrust_span becomes a no-op span, so the same tests still run as normal unit tests.

Parametrized tests

Pytest parameters are logged automatically as input unless you override them in the marker.
test_math.py
Each parametrized case becomes its own span in Braintrust.

CLI options

What to log

The braintrust_span fixture supports normal span logging methods. Typical fields to capture are:
  • input for the prompt or test payload
  • output for the model response
  • expected for the target behavior
  • scores for custom metrics beyond pass or fail
  • metadata for model name, environment, or fixture details

Resources