respx icon indicating copy to clipboard operation
respx copied to clipboard

Add docs on module level or global pytest fixture configuration

Open JacobHayes opened this issue 11 months ago • 4 comments

I wanted to override assert_all_called=True for my entire test suite, but couldn't quite see how from the docs. This PR adds info on how to override the respx_mock fixture at both the module level and globally.

JacobHayes avatar Jan 11 '25 17:01 JacobHayes

How to globally enable respx is a great suggestion to the docs.

Normally I use an auto-used session scoped fixture for this, e.g. @pytest.fixture(scope="session", autouse=True). An autouse fixture may be simpler a more common test pattern (?).

Maybe the docs on custom fixtures would be a better place to include an example at .. possibly with a link from the guide.

lundberg avatar Jan 21 '25 08:01 lundberg

Hmm yeah an autouse fixture is probably better for most uses.

In my case, I didn't want to affect httpx in tests that weren't otherwise mocking, so an autouse fixture (which would add mocking) wasn't ideal.

JacobHayes avatar Jan 24 '25 16:01 JacobHayes

Well, mocking httpx for the test suite globally is preferred, preventing any external unwanted/accidental calls to be made .. at least IMO 😉 i.e. it's a good workflow when the test suite is able to run anywhere, without remote access.

lundberg avatar Jan 27 '25 09:01 lundberg

BTW, the idea behind #227 is to allow the user to configure respx's default response/sideeffect e.g. NetworkError, to optionally simulating lack of network/remote access, and enforce the user to mock suitable responses for the endpoints called.

lundberg avatar Jan 27 '25 09:01 lundberg