Skip to content

Commit

Permalink
fix: correctly set fixture when using markers
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Albert Tugushev <[email protected]>
Signed-off-by: Matt Callaghan <[email protected]>
Signed-off-by: Mike Fiedler <[email protected]>
  • Loading branch information
miketheman and TripleDogDare committed May 31, 2020
1 parent 840691a commit 81b7490
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
13 changes: 10 additions & 3 deletions pytest_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
10 changes: 8 additions & 2 deletions tests/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand Down

0 comments on commit 81b7490

Please sign in to comment.