getsentry / getsentry/responses
Inconsistent behaviour of assert_all_requests_are_fired between decorator and context manager
- Dominant language
- Python
- Stars
- 4.3k
- Forks
- 378
- Avg merge
- 4d 1h
- Merged PRs (30d)
- 1
Description
### Describe the bug
When a `RequestsMock()` is used as a context manager, `assert_all_requests_are_fired` is defaulted to True and an exception is raised at the end of the context block if a response is unused. However, using the `@responses.activate` decorator on a test function defaults `assert_all_requests_are_fired` to False leading to inconsistent behaviour.
### Additional context
As a workaround, I have tried setting `responses.mock.assert_all_requests_are_fired = True` in conftest.py or just after an import of requests. However, this does not change the behaviour. The issue appears to be that the value in the default mock is never used as it gets defaulted by a default parameter in `activate()`, see: [code](https://github.com/getsentry/responses/blob/f7a7945991efc7cb8133b9d6ec75761b459fe75c/responses/__init__.py#L1017).
### Version of `responses`
0.25.3
### Steps to Reproduce
```python
import responses
import requests
def test_my_api():
with responses.RequestsMock() as rsps:
rsps.add(
responses.GET,
"http://twitter.com/api/1/foobar",
body="{}",
status=200,
content_type="application/json",
)
@responses.activate
def test_my_api_2():
responses.add(
responses.GET,
"http://twitter.com/api/1/foobar",
body="{}",
status=200,
content_type="application/json",
)
```
### Expected Result
Both test cases to fail with:
```
AssertionError: Not all requests have been executed [('GET', 'http://twitter.com/api/1/foobar')]
```
### Actual Result
First test case fails.
Second test case passes.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.