29
29
Series ,
30
30
)
31
31
32
- _RAISE_NETWORK_ERROR_DEFAULT = False
33
-
34
32
# skip tests on exceptions with these messages
35
33
_network_error_messages = (
36
34
# 'urlopen error timed out',
70
68
71
69
72
70
def _get_default_network_errors ():
73
- # Lazy import for http.client because it imports many things from the stdlib
71
+ # Lazy import for http.client & urllib.error
72
+ # because it imports many things from the stdlib
74
73
import http .client
74
+ import urllib .error
75
75
76
- return (OSError , http .client .HTTPException , TimeoutError )
76
+ return (OSError , http .client .HTTPException , TimeoutError , urllib . error . URLError )
77
77
78
78
79
79
def optional_args (decorator ):
@@ -108,7 +108,7 @@ def dec(f):
108
108
def network (
109
109
t ,
110
110
url = "https://www.google.com" ,
111
- raise_on_error = _RAISE_NETWORK_ERROR_DEFAULT ,
111
+ raise_on_error = False ,
112
112
check_before_test = False ,
113
113
error_classes = None ,
114
114
skip_errnos = _network_errno_vals ,
@@ -163,8 +163,8 @@ def network(
163
163
Tests decorated with @network will fail if it's possible to make a network
164
164
connection to another URL (defaults to google.com)::
165
165
166
- >>> from pandas import _testing as ts
167
- >>> @ts .network
166
+ >>> from pandas import _testing as tm
167
+ >>> @tm .network
168
168
... def test_network():
169
169
... with pd.io.common.urlopen("rabbit://bonanza.com"):
170
170
... pass
@@ -175,18 +175,18 @@ def network(
175
175
176
176
You can specify alternative URLs::
177
177
178
- >>> @ts .network("https://www.yahoo.com")
178
+ >>> @tm .network("https://www.yahoo.com")
179
179
... def test_something_with_yahoo():
180
180
... raise OSError("Failure Message")
181
- >>> test_something_with_yahoo()
181
+ >>> test_something_with_yahoo() # doctest: +SKIP
182
182
Traceback (most recent call last):
183
183
...
184
184
OSError: Failure Message
185
185
186
186
If you set check_before_test, it will check the url first and not run the
187
187
test on failure::
188
188
189
- >>> @ts .network("failing://url.blaher", check_before_test=True)
189
+ >>> @tm .network("failing://url.blaher", check_before_test=True)
190
190
... def test_something():
191
191
... print("I ran!")
192
192
... raise ValueError("Failure")
@@ -196,7 +196,7 @@ def network(
196
196
197
197
Errors not related to networking will always be raised.
198
198
"""
199
- from pytest import skip
199
+ import pytest
200
200
201
201
if error_classes is None :
202
202
error_classes = _get_default_network_errors ()
@@ -210,7 +210,9 @@ def wrapper(*args, **kwargs):
210
210
and not raise_on_error
211
211
and not can_connect (url , error_classes )
212
212
):
213
- skip ()
213
+ pytest .skip (
214
+ f"May not have network connectivity because cannot connect to { url } "
215
+ )
214
216
try :
215
217
return t (* args , ** kwargs )
216
218
except Exception as err :
@@ -220,22 +222,21 @@ def wrapper(*args, **kwargs):
220
222
errno = getattr (err .reason , "errno" , None ) # type: ignore[attr-defined]
221
223
222
224
if errno in skip_errnos :
223
- skip (f"Skipping test due to known errno and error { err } " )
225
+ pytest . skip (f"Skipping test due to known errno and error { err } " )
224
226
225
227
e_str = str (err )
226
228
227
229
if any (m .lower () in e_str .lower () for m in _skip_on_messages ):
228
- skip (
230
+ pytest . skip (
229
231
f"Skipping test because exception message is known and error { err } "
230
232
)
231
233
232
- if not isinstance (err , error_classes ):
233
- raise
234
-
235
- if raise_on_error or can_connect (url , error_classes ):
234
+ if not isinstance (err , error_classes ) or raise_on_error :
236
235
raise
237
236
else :
238
- skip (f"Skipping test due to lack of connectivity and error { err } " )
237
+ pytest .skip (
238
+ f"Skipping test due to lack of connectivity and error { err } "
239
+ )
239
240
240
241
return wrapper
241
242
0 commit comments