getsentry / getsentry/responses

Feedback on responses._recorder / How to make it better

Open
#740 1 comment 1 reaction 0 assignees View on GitHub
enhancement
Dominant language
Python
Stars
4.3k
Forks
378
Avg merge
4d 1h
Merged PRs (30d)
1

Description

This is awesome feature, and I started using it right away.

But, it's tedious to have separate code for creating responses. So, I'm using following plugin:

It allows to run `pytest --update-responses` and they will be saved to a file, and loaded when used without this argument.

Maybe, include this as plugin? I can make PR adding this, so we can make proper code review.

```python
from __future__ import annotations

import pytest
import responses
from _pytest.config.argparsing import Parser
from pytest_cases import fixture
from responses._recorder import recorder

def pytest_addoption(parser: Parser):
group = parser.getgroup("responses")
group.addoption(
"--update-responses",
action="store_true",
dest="update_responses",
default=False,
help="Don't mock responses, instead write actual responses to snapshot files.",
)

@fixture
def mocked_responses():
with responses.RequestsMock() as rsps:
yield rsps

@fixture
def responses_snapshot(request: pytest.FixtureRequest):
"""
Before using response you need to write snapshots.

Run with `pytest --update-responses`.

https://github.com/getsentry/responses#record-responses-to-files
"""

update = request.config.getoption("update_responses")
snapshot = _get_snapshot_file(request)

if update:
with recorder as r:
yield r
recorder.dump_to_file(
file_path=snapshot,
registered=r.get_registry().registered,
)
else:
with responses.RequestsMock() as r:
r._add_from_file(snapshot)
yield r

def _get_snapshot_file(request: pytest.FixtureRequest):
f = request.node.name.replace("[", "-").replace("]", "")
p = request.path.parent / "__responses__/"
p.mkdir(exist_ok=True)
return p / f"{f}.yaml"
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.