Is it possible to prevent the execution of "function scoped" fixtures with autouse=True on specific marks only?
I have the following fixture set to autouse so that all outgoing requests are automatically mocked out:
@pytest.fixture(autouse=True) def no_requests(monkeypatch): monkeypatch.setattr("requests.sessions.Session.request", MagicMock()) But I have a mark called endtoend that I use to define a series of tests that are allowed to make external requests for more robust end to end testing. I would like to inject no_requests in all tests (the vast majority), but not in tests like the following:
@pytest.mark.endtoend def test_api_returns_ok(): assert make_request().status_code == 200 Is this possible?
You can also use the request object in your fixture to check the markers used on the test, and don't do anything if a specific marker is set:
import pytest @pytest.fixture(autouse=True) def autofixt(request): if 'noautofixt' in request.keywords: return print("patching stuff") def test1(): pass @pytest.mark.noautofixt def test2(): pass Output with -vs:
x.py::test1 patching stuff PASSED x.py::test2 PASSED
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With