Testing with Markus

Markus comes with a MetricsMock that makes it easier to write tests and assert things about generated metrics.

class markus.testing.MetricsMock

Mock for recording metrics events and testing them.

Mimics a metrics backend as a context manager. Keeps records of what got metricfied so that you can print them out, filter them, assert various things about them, etc.

To use:

from markus.testing import MetricsMock
from markus import INCR, GAUGE, TIMING, HISTOGRAM


def test_something():
    with MetricsMock() as mm:
        # do things that might record metrics

        # Print the metrics recorded (helps with debugging)
        mm.print_records()

        # Assert something about the metrics recorded
        assert mm.has_record(INCR, stat='some.random.key', value=1)
assert_gauge(stat, value=None, tags=None)

Asserts a gauge was emitted at least once.

assert_gauge_once(stat, value=None, tags=None)

Asserts a gauge was emitted exactly once.

assert_histogram(stat, value=None, tags=None)

Asserts a histogram was emitted at least once.

assert_histogram_once(stat, value=None, tags=None)

Asserts a histogram was emitted exactly once.

assert_incr(stat, value=1, tags=None)

Asserts an incr was emitted at least once.

assert_incr_once(stat, value=1, tags=None)

Asserts an incr was emitted exactly once.

assert_not_gauge(stat, value=None, tags=None)

Asserts a gauge was not emitted.

assert_not_histogram(stat, value=None, tags=None)

Asserts a histogram was not emitted.

assert_not_incr(stat, value=1, tags=None)

Asserts an incr was not emitted.

assert_not_timing(stat, value=None, tags=None)

Asserts a timing was not emitted.

assert_timing(stat, value=None, tags=None)

Asserts a timing was emitted at least once.

assert_timing_once(stat, value=None, tags=None)

Asserts a timing was emitted exactly once.

clear_records()

Clear the records list.

filter_records(fun_name=None, stat=None, value=None, tags=None)

Filter collected metircs records for ones that match specified criteria.

get_records()

Return set of collected metrics records.

has_record(fun_name=None, stat=None, value=None, tags=None)

Return True/False regarding whether collected metrics match criteria.

print_records()

Print all the collected metrics.