pytest - is there a way to ignore an autouse fixture?
----- UPDATE for latest versions of pytest-----use request.node.get_closest_marker()
to get the marker. Refer get_closest_marker
You can achieve this in many ways , one way is to use the request fixture along with pytest mark fix.All you need to do is, create a new generic fixture
@pytest.fixture(autouse=True)def browser(request): # _browser = request.node.get_marker('browser') _browser = request.node.get_closest_marker('browser') if _browser: if _browser.kwargs.get("use") == "chrome" : # Do chrome related setup elif _browser.kwargs.get("use") == "phantom" : # Do Phantom.js related setup else: # No mark up ,use default setup
and mark your tests like this
@pytest.mark.browser(use="chrome")def test_some_chrome_test(): # browser here would return chrome driver@pytest.mark.browser(use="phantom")def test_some_phantomjs_test(): # browser here would return phantom driver
There is way to use these function on demand without using usefixtures
decorator like below.If you have used autouse=True
then it would be automatically called as per its scope and I dont think there is a way to skip that in any of the test.
@pytest.fixture(autouse=False)def use_phantomjs(self): self.wd = webdriver.PhantomJS() yield self.close_wd()def test_my_test(use_phantomjs): ... ...