Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable autouse fixtures on specific pytest marks

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?

like image 412
Mattew Whitt Avatar asked Aug 03 '16 15:08

Mattew Whitt


1 Answers

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 
like image 65
The Compiler Avatar answered Sep 21 '22 06:09

The Compiler