File: test_precedence.py

package info (click to toggle)
python-pytest-socket 0.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 296 kB
  • sloc: python: 663; makefile: 37
file content (162 lines) | stat: -rw-r--r-- 4,074 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"""Test module to express precedence tests between the different
configuration combinations"""

from tests.common import assert_socket_blocked


def test_disable_via_fixture(testdir):
    testdir.makepyfile(
        """
        import socket

        def test_socket(socket_disabled):
            socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        """
    )
    result = testdir.runpytest()
    assert_socket_blocked(result)


def test_disable_via_marker(testdir):
    testdir.makepyfile(
        """
        import socket
        import pytest

        @pytest.mark.disable_socket
        def test_socket():
            socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        """
    )
    result = testdir.runpytest()
    assert_socket_blocked(result)


def test_global_disable_via_cli_flag(testdir):
    testdir.makepyfile(
        """
        import socket

        def test_socket():
            socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        """
    )
    result = testdir.runpytest("--disable-socket")
    assert_socket_blocked(result)


def test_force_enable_socket_via_cli_flag(testdir):
    testdir.makepyfile(
        """
        import socket
        import pytest

        @pytest.mark.disable_socket
        def test_socket():
            socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        """
    )
    result = testdir.runpytest("--force-enable-socket")
    result.assert_outcomes(passed=1)


def test_force_enable_cli_flag_precedence(testdir):
    testdir.makepyfile(
        """
        import socket

        def test_socket():
            socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        """
    )
    result = testdir.runpytest("--disable-socket", "--force-enable-socket")
    result.assert_outcomes(passed=1)


def test_global_disable_via_config(testdir):
    testdir.makepyfile(
        """
        import socket

        def test_socket():
            socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        """
    )
    testdir.makeini(
        """
        [pytest]
        addopts = --disable-socket
        """
    )
    result = testdir.runpytest()
    assert_socket_blocked(result)


def test_enable_via_fixture(testdir):
    testdir.makepyfile(
        """
        import socket

        def test_socket(socket_enabled):
            socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        def test_socket_disabled():
            socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        """
    )
    result = testdir.runpytest("--disable-socket")
    assert_socket_blocked(result, passed=1, failed=1)


def test_enable_via_marker(testdir):
    testdir.makepyfile(
        """
        import socket
        import pytest

        @pytest.mark.enable_socket
        def test_socket():
            socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        def test_socket_disabled():
            socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        """
    )
    result = testdir.runpytest("--disable-socket")
    assert_socket_blocked(result, passed=1, failed=1)


def test_enable_marker_for_test_class(testdir):
    testdir.makepyfile(
        """
        import socket
        import pytest

        @pytest.mark.enable_socket
        class TestSocket:
            def test_socket(self):
                socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        def test_socket_disabled():
            socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        """
    )
    result = testdir.runpytest("--disable-socket")
    assert_socket_blocked(result, passed=1, failed=1)


def test_global_disable_and_allow_host(testdir, httpbin):
    """Disable socket globally, but allow a specific host"""
    testdir.makepyfile(
        f"""
        from urllib.request import urlopen

        def test_urlopen():
            assert urlopen("{httpbin.url}/")

        def test_urlopen_disabled():
            assert urlopen("https://google.com/")
        """
    )
    result = testdir.runpytest("--disable-socket", f"--allow-hosts={httpbin.host}")
    assert_socket_blocked(result, passed=1, failed=1)