@@ -971,106 +971,6 @@ def dec(f):
971
971
_network_error_classes = IOError , httplib .HTTPException
972
972
973
973
974
- @optional_args
975
- def network (t , raise_on_error = _RAISE_NETWORK_ERROR_DEFAULT ,
976
- error_classes = _network_error_classes , num_runs = 2 ):
977
- """
978
- Label a test as requiring network connection and skip test if it encounters a ``URLError``.
979
-
980
- In some cases it is not possible to assume network presence (e.g. Debian
981
- build hosts).
982
-
983
- You can pass an optional ``raise_on_error`` argument to the decorator, in
984
- which case it will always raise an error even if it's not a subclass of
985
- ``error_classes``.
986
-
987
- Parameters
988
- ----------
989
- t : callable
990
- The test requiring network connectivity.
991
- raise_on_error : bool, optional
992
- If True, never catches errors.
993
- error_classes : tuple, optional
994
- error classes to ignore. If not in ``error_classes``, raises the error.
995
- defaults to URLError. Be careful about changing the error classes here,
996
- it may result in undefined behavior.
997
- num_runs : int, optional
998
- Number of times to run test. If fails on last try, will raise. Default
999
- is 2 runs.
1000
-
1001
- Returns
1002
- -------
1003
- t : callable
1004
- The decorated test `t`.
1005
-
1006
- Examples
1007
- --------
1008
- A test can be decorated as requiring network like this::
1009
-
1010
- >>> from pandas.util.testing import network
1011
- >>> from pandas.io.common import urlopen
1012
- >>> import nose
1013
- >>> @network
1014
- ... def test_network():
1015
- ... with urlopen("rabbit://bonanza.com") as f:
1016
- ... pass
1017
- ...
1018
- >>> try:
1019
- ... test_network()
1020
- ... except nose.SkipTest:
1021
- ... print("SKIPPING!")
1022
- ...
1023
- SKIPPING!
1024
-
1025
- Alternatively, you can use set ``raise_on_error`` in order to get
1026
- the error to bubble up, e.g.::
1027
-
1028
- >>> @network(raise_on_error=True)
1029
- ... def test_network():
1030
- ... with urlopen("complaint://deadparrot.com") as f:
1031
- ... pass
1032
- ...
1033
- >>> test_network()
1034
- Traceback (most recent call last):
1035
- ...
1036
- URLError: <urlopen error unknown url type: complaint>
1037
-
1038
- And use ``nosetests -a '!network'`` to exclude running tests requiring
1039
- network connectivity. ``_RAISE_NETWORK_ERROR_DEFAULT`` in
1040
- ``pandas/util/testing.py`` sets the default behavior (currently False).
1041
- """
1042
- from nose import SkipTest
1043
-
1044
- if num_runs < 1 :
1045
- raise ValueError ("Must set at least 1 run" )
1046
- t .network = True
1047
-
1048
- @wraps (t )
1049
- def network_wrapper (* args , ** kwargs ):
1050
- if raise_on_error :
1051
- return t (* args , ** kwargs )
1052
- else :
1053
- runs = 0
1054
-
1055
- for _ in range (num_runs ):
1056
- try :
1057
- try :
1058
- return t (* args , ** kwargs )
1059
- except error_classes as e :
1060
- raise SkipTest ("Skipping test %s" % e )
1061
- except SkipTest :
1062
- raise
1063
- except Exception as e :
1064
- if runs < num_runs - 1 :
1065
- print ("Failed: %r" % e )
1066
- else :
1067
- raise
1068
-
1069
- runs += 1
1070
-
1071
- return network_wrapper
1072
-
1073
-
1074
974
def can_connect (url , error_classes = _network_error_classes ):
1075
975
"""Try to connect to the given url. True if succeeds, False if IOError
1076
976
raised
@@ -1096,10 +996,9 @@ def can_connect(url, error_classes=_network_error_classes):
1096
996
1097
997
1098
998
@optional_args
1099
- def with_connectivity_check (t , url = "http://www.google.com" ,
1100
- raise_on_error = _RAISE_NETWORK_ERROR_DEFAULT ,
1101
- check_before_test = False ,
1102
- error_classes = _network_error_classes ):
999
+ def network (t , url = "http://www.google.com" ,
1000
+ raise_on_error = _RAISE_NETWORK_ERROR_DEFAULT ,
1001
+ check_before_test = False , error_classes = _network_error_classes ):
1103
1002
"""
1104
1003
Label a test as requiring network connection and, if an error is
1105
1004
encountered, only raise if it does not find a network connection.
@@ -1138,25 +1037,42 @@ def with_connectivity_check(t, url="http://www.google.com",
1138
1037
Example
1139
1038
-------
1140
1039
1141
- In this example, you see how it will raise the error if it can connect to
1142
- the url::
1143
- >>> @with_connectivity_check("http://www.yahoo.com")
1040
+ Tests decorated with @network will fail if it's possible to make a network
1041
+ connection to another URL (defaults to google.com)::
1042
+
1043
+ >>> from pandas.util.testing import network
1044
+ >>> from pandas.io.common import urlopen
1045
+ >>> @network
1046
+ ... def test_network():
1047
+ ... with urlopen("rabbit://bonanza.com"):
1048
+ ... pass
1049
+ Traceback
1050
+ ...
1051
+ URLError: <urlopen error unknown url type: rabit>
1052
+
1053
+ You can specify alternative URLs::
1054
+
1055
+ >>> @network("http://www.yahoo.com")
1144
1056
... def test_something_with_yahoo():
1145
1057
... raise IOError("Failure Message")
1146
1058
>>> test_something_with_yahoo()
1147
1059
Traceback (most recent call last):
1148
1060
...
1149
1061
IOError: Failure Message
1150
1062
1151
- I you set check_before_test, it will check the url first and not run the test on failure::
1152
- >>> @with_connectivity_check("failing://url.blaher", check_before_test=True)
1063
+ If you set check_before_test, it will check the url first and not run the
1064
+ test on failure::
1065
+
1066
+ >>> @network("failing://url.blaher", check_before_test=True)
1153
1067
... def test_something():
1154
1068
... print("I ran!")
1155
1069
... raise ValueError("Failure")
1156
1070
>>> test_something()
1157
1071
Traceback (most recent call last):
1158
1072
...
1159
1073
SkipTest
1074
+
1075
+ Errors not related to networking will always be raised.
1160
1076
"""
1161
1077
from nose import SkipTest
1162
1078
t .network = True
@@ -1178,6 +1094,9 @@ def wrapper(*args, **kwargs):
1178
1094
return wrapper
1179
1095
1180
1096
1097
+ with_connectivity_check = network
1098
+
1099
+
1181
1100
class SimpleMock (object ):
1182
1101
1183
1102
"""
0 commit comments