How do you unit test alerting rules?

AdvancedConceptPromQL & Recording Rules
Answer

With promtool test rules and synthetic series:

rule_files: [api-alerts.yml]
evaluation_interval: 1m
tests:
  - interval: 1m
    input_series:
      - series: 'job:http_errors:ratio_rate5m{job="api"}'
        values: '0.01 0.08x15'
    alert_rule_test:
      - eval_time: 12m
        alertname: APIHighErrorRate
        exp_alerts:
          - exp_labels: { job: api, severity: page, team: payments }

0.08x15 means 0.08 repeated 15 more times. We run this in CI so threshold and label changes are reviewed with proof.

All Monitoring & Observability questions Roadmap: Monitoring & alerting

More on PromQL & Recording Rules