14
14
15
15
16
16
"""User Agent Parser Unit Tests.
17
- Run:
18
- # python -m user_agent_parser_test (runs all the tests, takes awhile)
19
- or like:
20
- # python -m user_agent_parser_test ParseTest.testBrowserscopeStrings
21
17
"""
22
18
23
19
30
26
import platform
31
27
import re
32
28
import sys
33
- import unittest
34
29
import warnings
30
+
31
+ import pytest
35
32
import yaml
36
33
37
34
if platform .python_implementation () == "PyPy" :
54
51
)
55
52
56
53
57
- class ParseTest ( unittest . TestCase ) :
54
+ class ParseTest :
58
55
def testBrowserscopeStrings (self ):
59
56
self .runUserAgentTestsFromYAML (
60
57
os .path .join (TEST_RESOURCES_DIR , "tests/test_ua.yaml" )
@@ -112,12 +109,8 @@ def testParseAll(self):
112
109
}
113
110
114
111
result = user_agent_parser .Parse (user_agent_string )
115
- self .assertEqual (
116
- result ,
117
- expected ,
118
- "UA: {0}\n expected<{1}> != actual<{2}>" .format (
119
- user_agent_string , expected , result
120
- ),
112
+ assert result == expected , "UA: {0}\n expected<{1}> != actual<{2}>" .format (
113
+ user_agent_string , expected , result
121
114
)
122
115
123
116
# Run a set of test cases from a YAML file
@@ -140,26 +133,22 @@ def runUserAgentTestsFromYAML(self, file_name):
140
133
141
134
result = {}
142
135
result = user_agent_parser .ParseUserAgent (user_agent_string )
143
- self .assertEqual (
144
- result ,
145
- expected ,
146
- "UA: {0}\n expected<{1}, {2}, {3}, {4}> != actual<{5}, {6}, {7}, {8}>" .format (
147
- user_agent_string ,
148
- expected ["family" ],
149
- expected ["major" ],
150
- expected ["minor" ],
151
- expected ["patch" ],
152
- result ["family" ],
153
- result ["major" ],
154
- result ["minor" ],
155
- result ["patch" ],
156
- ),
157
- )
158
- self .assertLessEqual (
159
- len (user_agent_parser ._PARSE_CACHE ),
160
- user_agent_parser .MAX_CACHE_SIZE ,
161
- "verify that the cache size never exceeds the configured setting" ,
136
+ assert (
137
+ result == expected
138
+ ), "UA: {0}\n expected<{1}, {2}, {3}, {4}> != actual<{5}, {6}, {7}, {8}>" .format (
139
+ user_agent_string ,
140
+ expected ["family" ],
141
+ expected ["major" ],
142
+ expected ["minor" ],
143
+ expected ["patch" ],
144
+ result ["family" ],
145
+ result ["major" ],
146
+ result ["minor" ],
147
+ result ["patch" ],
162
148
)
149
+ assert (
150
+ len (user_agent_parser ._PARSE_CACHE ) <= user_agent_parser .MAX_CACHE_SIZE
151
+ ), "verify that the cache size never exceeds the configured setting"
163
152
164
153
def runOSTestsFromYAML (self , file_name ):
165
154
yamlFile = open (os .path .join (TEST_RESOURCES_DIR , file_name ))
@@ -180,22 +169,20 @@ def runOSTestsFromYAML(self, file_name):
180
169
}
181
170
182
171
result = user_agent_parser .ParseOS (user_agent_string )
183
- self .assertEqual (
184
- result ,
185
- expected ,
186
- "UA: {0}\n expected<{1} {2} {3} {4} {5}> != actual<{6} {7} {8} {9} {10}>" .format (
187
- user_agent_string ,
188
- expected ["family" ],
189
- expected ["major" ],
190
- expected ["minor" ],
191
- expected ["patch" ],
192
- expected ["patch_minor" ],
193
- result ["family" ],
194
- result ["major" ],
195
- result ["minor" ],
196
- result ["patch" ],
197
- result ["patch_minor" ],
198
- ),
172
+ assert (
173
+ result == expected
174
+ ), "UA: {0}\n expected<{1} {2} {3} {4} {5}> != actual<{6} {7} {8} {9} {10}>" .format (
175
+ user_agent_string ,
176
+ expected ["family" ],
177
+ expected ["major" ],
178
+ expected ["minor" ],
179
+ expected ["patch" ],
180
+ expected ["patch_minor" ],
181
+ result ["family" ],
182
+ result ["major" ],
183
+ result ["minor" ],
184
+ result ["patch" ],
185
+ result ["patch_minor" ],
199
186
)
200
187
201
188
def runDeviceTestsFromYAML (self , file_name ):
@@ -215,35 +202,33 @@ def runDeviceTestsFromYAML(self, file_name):
215
202
}
216
203
217
204
result = user_agent_parser .ParseDevice (user_agent_string )
218
- self .assertEqual (
219
- result ,
220
- expected ,
221
- "UA: {0}\n expected<{1} {2} {3}> != actual<{4} {5} {6}>" .format (
222
- user_agent_string ,
223
- expected ["family" ],
224
- expected ["brand" ],
225
- expected ["model" ],
226
- result ["family" ],
227
- result ["brand" ],
228
- result ["model" ],
229
- ),
205
+ assert (
206
+ result == expected
207
+ ), "UA: {0}\n expected<{1} {2} {3}> != actual<{4} {5} {6}>" .format (
208
+ user_agent_string ,
209
+ expected ["family" ],
210
+ expected ["brand" ],
211
+ expected ["model" ],
212
+ result ["family" ],
213
+ result ["brand" ],
214
+ result ["model" ],
230
215
)
231
216
232
217
233
- class GetFiltersTest ( unittest . TestCase ) :
218
+ class GetFiltersTest :
234
219
def testGetFiltersNoMatchesGiveEmptyDict (self ):
235
220
user_agent_string = "foo"
236
221
filters = user_agent_parser .GetFilters (
237
222
user_agent_string , js_user_agent_string = None
238
223
)
239
- self . assertEqual ({}, filters )
224
+ assert {} == filters
240
225
241
226
def testGetFiltersJsUaPassedThrough (self ):
242
227
user_agent_string = "foo"
243
228
filters = user_agent_parser .GetFilters (
244
229
user_agent_string , js_user_agent_string = "bar"
245
230
)
246
- self . assertEqual ( {"js_user_agent_string" : "bar" }, filters )
231
+ assert {"js_user_agent_string" : "bar" } == filters
247
232
248
233
def testGetFiltersJsUserAgentFamilyAndVersions (self ):
249
234
user_agent_string = (
@@ -254,38 +239,32 @@ def testGetFiltersJsUserAgentFamilyAndVersions(self):
254
239
filters = user_agent_parser .GetFilters (
255
240
user_agent_string , js_user_agent_string = "bar" , js_user_agent_family = "foo"
256
241
)
257
- self .assertEqual (
258
- {"js_user_agent_string" : "bar" , "js_user_agent_family" : "foo" }, filters
259
- )
242
+ assert {"js_user_agent_string" : "bar" , "js_user_agent_family" : "foo" } == filters
260
243
261
244
262
- class TestDeprecationWarnings ( unittest . TestCase ) :
245
+ class TestDeprecationWarnings :
263
246
def setUp (self ):
264
247
"""In Python 2.7, catch_warnings apparently does not do anything if
265
248
the warning category is not active, whereas in 3(.6 and up) it
266
249
seems to work out of the box.
267
250
"""
268
- super (TestDeprecationWarnings , self ).setUp ()
269
251
warnings .simplefilter ("always" , DeprecationWarning )
270
252
271
253
def tearDown (self ):
272
254
# not ideal as it discards all other warnings updates from the
273
255
# process, should really copy the contents of
274
256
# `warnings.filters`, then reset-it.
275
257
warnings .resetwarnings ()
276
- super (TestDeprecationWarnings , self ).tearDown ()
277
258
278
259
def test_parser_deprecation (self ):
279
- with warnings . catch_warnings ( record = True ) as ws :
260
+ with pytest . warns ( DeprecationWarning ) as ws :
280
261
user_agent_parser .ParseWithJSOverrides ("" )
281
- self .assertEqual (len (ws ), 1 )
282
- self .assertEqual (ws [0 ].category , DeprecationWarning )
262
+ assert len (ws ) == 1
283
263
284
264
def test_printer_deprecation (self ):
285
- with warnings . catch_warnings ( record = True ) as ws :
265
+ with pytest . warns ( DeprecationWarning ) as ws :
286
266
user_agent_parser .Pretty ("" )
287
- self .assertEqual (len (ws ), 1 )
288
- self .assertEqual (ws [0 ].category , DeprecationWarning )
267
+ assert len (ws ) == 1
289
268
290
269
def test_js_bits_deprecation (self ):
291
270
for parser , count in [
@@ -295,33 +274,28 @@ def test_js_bits_deprecation(self):
295
274
(user_agent_parser .ParseDevice , 1 ),
296
275
]:
297
276
user_agent_parser ._PARSE_CACHE .clear ()
298
- with warnings . catch_warnings ( record = True ) as ws :
277
+ with pytest . warns ( DeprecationWarning ) as ws :
299
278
parser ("some random thing" , js_attribute = True )
300
- self .assertEqual (len (ws ), count )
301
- for w in ws :
302
- self .assertEqual (w .category , DeprecationWarning )
279
+ assert len (ws ) == count
303
280
304
281
305
- class ErrTest (unittest .TestCase ):
306
- @unittest .skipIf (
307
- sys .version_info < (3 ,), "bytes and str are not differentiated in P2"
282
+ class ErrTest :
283
+ @pytest .mark .skipif (
284
+ sys .version_info < (3 ,),
285
+ reason = "bytes and str are not differentiated in P2" ,
308
286
)
309
287
def test_bytes (self ):
310
- with self . assertRaises (TypeError ):
288
+ with pytest . raises (TypeError ):
311
289
user_agent_parser .Parse (b"" )
312
290
313
291
def test_int (self ):
314
- with self . assertRaises (TypeError ):
292
+ with pytest . raises (TypeError ):
315
293
user_agent_parser .Parse (0 )
316
294
317
295
def test_list (self ):
318
- with self . assertRaises (TypeError ):
296
+ with pytest . raises (TypeError ):
319
297
user_agent_parser .Parse ([])
320
298
321
299
def test_tuple (self ):
322
- with self . assertRaises (TypeError ):
300
+ with pytest . raises (TypeError ):
323
301
user_agent_parser .Parse (())
324
-
325
-
326
- if __name__ == "__main__" :
327
- unittest .main ()
0 commit comments