From 81b7490671ee32b8c45708b885b837f86e8a44d8 Mon Sep 17 00:00:00 2001 From: Mike Fiedler Date: Sun, 31 May 2020 09:26:43 -0400 Subject: [PATCH] fix: correctly set fixture when using markers As reported in a couple of issues, the usage of markers (not fixtures) was misbehaving, as the order in which the `_socket_marker` evaluation was occurring left the command-line option disabled. Fixes #15 Closes #28 Co-authored-by: Calvin Behling Signed-off-by: Albert Tugushev Signed-off-by: Matt Callaghan Signed-off-by: Mike Fiedler --- pytest_socket.py | 13 ++++++++++--- tests/test_socket.py | 10 ++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/pytest_socket.py b/pytest_socket.py index 53e39d2..296f730 100644 --- a/pytest_socket.py +++ b/pytest_socket.py @@ -38,14 +38,21 @@ def pytest_addoption(parser): @pytest.fixture(autouse=True) def _socket_marker(request): + """ + Create an automatically-used fixture that every test function will load. + + The last option to set the fixture wins priority. + The expected behavior is that higher granularity options should override + lower granularity options. + """ + if request.config.getoption('--disable-socket'): + request.getfixturevalue('socket_disabled') + if request.node.get_closest_marker('disable_socket'): request.getfixturevalue('socket_disabled') if request.node.get_closest_marker('enable_socket'): request.getfixturevalue('socket_enabled') - if request.config.getoption('--disable-socket'): - request.getfixturevalue('socket_disabled') - @pytest.fixture def socket_disabled(): diff --git a/tests/test_socket.py b/tests/test_socket.py index 028dc5e..9c13d4d 100644 --- a/tests/test_socket.py +++ b/tests/test_socket.py @@ -101,7 +101,9 @@ def test_socket(): socket.socket(socket.AF_INET, socket.SOCK_STREAM) """) result = testdir.runpytest("--verbose", "--disable-socket") - assert_socket_blocked(result) + result.assert_outcomes(1, 0, 0) + with pytest.raises(BaseException): + assert_socket_blocked(result) def test_urllib_succeeds_by_default(testdir): @@ -116,6 +118,8 @@ def test_disable_socket_urllib(): """) result = testdir.runpytest("--verbose") result.assert_outcomes(1, 0, 0) + with pytest.raises(BaseException): + assert_socket_blocked(result) def test_enabled_urllib_succeeds(testdir): @@ -132,7 +136,9 @@ def test_disable_socket_urllib(): assert urlopen('http://httpbin.org/get').getcode() == 200 """) result = testdir.runpytest("--verbose", "--disable-socket") - assert_socket_blocked(result) + result.assert_outcomes(1, 0, 0) + with pytest.raises(BaseException): + assert_socket_blocked(result) def test_disabled_urllib_fails(testdir):