From 25ea24684d187623d659b5b1825cc584a28e8cba Mon Sep 17 00:00:00 2001 From: Mike Fiedler Date: Sun, 18 Jun 2023 14:25:43 -0400 Subject: [PATCH] test: extract common function for reuse Signed-off-by: Mike Fiedler --- tests/common.py | 16 ++++++++++++++++ tests/test_async.py | 2 +- tests/test_precedence.py | 11 +---------- tests/test_socket.py | 8 +------- 4 files changed, 19 insertions(+), 18 deletions(-) create mode 100644 tests/common.py diff --git a/tests/common.py b/tests/common.py new file mode 100644 index 0000000..fe731f0 --- /dev/null +++ b/tests/common.py @@ -0,0 +1,16 @@ +""" +Common test functions. +""" + + +def assert_socket_blocked(result, passed=0, skipped=0, failed=1): + """ + Uses built in methods to test for common failure scenarios. + Usually we only test for a single failure, + but sometimes we want to test for multiple conditions, + so we can pass in the expected counts. + """ + result.assert_outcomes(passed=passed, skipped=skipped, failed=failed) + result.stdout.fnmatch_lines( + "*Socket*Blocked*Error: A test tried to use socket.socket.*" + ) diff --git a/tests/test_async.py b/tests/test_async.py index e272994..78486d0 100644 --- a/tests/test_async.py +++ b/tests/test_async.py @@ -1,5 +1,5 @@ from conftest import unix_sockets_only -from test_socket import assert_socket_blocked +from tests.common import assert_socket_blocked @unix_sockets_only diff --git a/tests/test_precedence.py b/tests/test_precedence.py index afdcb52..7da9e9c 100644 --- a/tests/test_precedence.py +++ b/tests/test_precedence.py @@ -1,16 +1,7 @@ """Test module to express precedence tests between the different configuration combinations""" - -def assert_socket_blocked(result, passed=0, skipped=0, failed=1): - """Uses built in methods to test for common failure scenarios. - Usually we only test for a single failure, - but sometimes we want to test for multiple conditions, - so we can pass in the expected counts.""" - result.assert_outcomes(passed=passed, skipped=skipped, failed=failed) - result.stdout.fnmatch_lines( - "*Socket*Blocked*Error: A test tried to use socket.socket.*" - ) +from tests.common import assert_socket_blocked def test_disable_via_fixture(testdir): diff --git a/tests/test_socket.py b/tests/test_socket.py index 39e371d..6255b60 100644 --- a/tests/test_socket.py +++ b/tests/test_socket.py @@ -1,6 +1,7 @@ import pytest from conftest import unix_sockets_only +from tests.common import assert_socket_blocked PYFILE_SOCKET_USED_IN_TEST_ARGS = """ import socket @@ -26,13 +27,6 @@ def test_socket(): """ -def assert_socket_blocked(result): - result.assert_outcomes(passed=0, skipped=0, failed=1) - result.stdout.fnmatch_lines( - "*SocketBlockedError: A test tried to use socket.socket.*" - ) - - @pytest.mark.parametrize( "pyfile", [