Skip to content

Commit

Permalink
test: refactor example pyfile to constant
Browse files Browse the repository at this point in the history
SonarLint detected this as a duplicated section and recommended
extracting to a constant for reuse.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
  • Loading branch information
miketheman committed May 29, 2020
1 parent fed6d89 commit 7bc7e87
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions tests/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
from pytest_socket import enable_socket


PYFILE_SOCKET_USED_IN_TEST = """
import socket
def test_socket():
socket.socket(socket.AF_INET, socket.SOCK_STREAM)
"""


@pytest.fixture(autouse=True)
def reenable_socket():
# The tests can leave the socket disabled in the global scope.
Expand All @@ -19,12 +27,7 @@ def assert_socket_blocked(result):


def test_socket_enabled_by_default(testdir):
testdir.makepyfile("""
import socket
def test_socket():
socket.socket(socket.AF_INET, socket.SOCK_STREAM)
""")
testdir.makepyfile(PYFILE_SOCKET_USED_IN_TEST)
result = testdir.runpytest("--verbose")
result.assert_outcomes(1, 0, 0)
with pytest.raises(BaseException):
Expand All @@ -50,12 +53,7 @@ def test_socket():


def test_global_disable_via_cli_flag(testdir):
testdir.makepyfile("""
import socket
def test_socket():
socket.socket(socket.AF_INET, socket.SOCK_STREAM)
""")
testdir.makepyfile(PYFILE_SOCKET_USED_IN_TEST)
result = testdir.runpytest("--verbose", "--disable-socket")
result.assert_outcomes(0, 0, 1)
assert_socket_blocked(result)
Expand All @@ -72,12 +70,7 @@ def test_help_message(testdir):


def test_global_disable_via_config(testdir):
testdir.makepyfile("""
import socket
def test_socket():
socket.socket(socket.AF_INET, socket.SOCK_STREAM)
""")
testdir.makepyfile(PYFILE_SOCKET_USED_IN_TEST)
testdir.makeini("""
[pytest]
addopts = --disable-socket
Expand Down

0 comments on commit 7bc7e87

Please sign in to comment.