willkg / markus

Markus is a Python library for generating metrics
Mozilla Public License 2.0
70 stars 8 forks source link

add pytest fixture #118

Closed willkg closed 4 months ago

willkg commented 1 year ago

Everyone that I've talked to that uses Markus is also using pytest and has to add something like this to their conftest.py:

from markus.testing import MetricsMock

@pytest.fixture
def metricsmock():
    with MetricsMock() as mm:
        yield mm

That seems silly. We should add it to Markus proper and document usage of the fixture.

willkg commented 1 year ago

@jwhitlock suggests I read https://docs.pytest.org/en/7.4.x/how-to/writing_plugins.html .

jwhitlock commented 1 year ago

I like the idea! There's a list of packages providing plugins at https://docs.pytest.org/en/7.4.x/how-to/plugins.html, you can see how they implemented their plugin to provide fixtures.

I think it is as simple as adding that code to https://github.com/willkg/markus/blob/main/src/markus/pytest_plugin.py.