|
22 | 22 | from jsonpath_ng import jsonpath # For setting the global auto_id_field flag |
23 | 23 | from oslotest import base |
24 | 24 | from six import moves |
25 | | -import testscenarios |
26 | 25 |
|
27 | 26 | from jsonpath_ng.ext import parser |
28 | 27 |
|
29 | 28 |
|
30 | | -class Testjsonpath_ng_ext(testscenarios.WithScenarios, |
31 | | - base.BaseTestCase): |
| 29 | +# Example from https://docs.pytest.org/en/7.1.x/example/parametrize.html#a-quick-port-of-testscenarios |
| 30 | +def pytest_generate_tests(metafunc): |
| 31 | + idlist = [] |
| 32 | + argvalues = [] |
| 33 | + for scenario in metafunc.cls.scenarios: |
| 34 | + idlist.append(scenario[0]) |
| 35 | + items = scenario[1].items() |
| 36 | + argnames = [x[0] for x in items] |
| 37 | + argvalues.append([x[1] for x in items]) |
| 38 | + metafunc.parametrize(argnames, argvalues, ids=idlist, scope="class") |
| 39 | + |
| 40 | + |
| 41 | +class Testjsonpath_ng_ext: |
32 | 42 | scenarios = [ |
33 | 43 | ('sorted_list', dict(string='objects.`sorted`', |
34 | 44 | data={'objects': ['alpha', 'gamma', 'beta']}, |
@@ -334,17 +344,17 @@ class Testjsonpath_ng_ext(testscenarios.WithScenarios, |
334 | 344 | )), |
335 | 345 | ] |
336 | 346 |
|
337 | | - def test_fields_value(self): |
| 347 | + def test_fields_value(self, string, data, target): |
338 | 348 | jsonpath.auto_id_field = None |
339 | | - result = parser.parse(self.string, debug=True).find(self.data) |
340 | | - if isinstance(self.target, list): |
341 | | - self.assertEqual(self.target, [r.value for r in result]) |
342 | | - elif isinstance(self.target, set): |
343 | | - self.assertEqual(self.target, set([r.value for r in result])) |
344 | | - elif isinstance(self.target, (int, float)): |
345 | | - self.assertEqual(self.target, result[0].value) |
| 349 | + result = parser.parse(string, debug=True).find(data) |
| 350 | + if isinstance(target, list): |
| 351 | + assert target == [r.value for r in result] |
| 352 | + elif isinstance(target, set): |
| 353 | + assert target == set([r.value for r in result]) |
| 354 | + elif isinstance(target, (int, float)): |
| 355 | + assert target == result[0].value |
346 | 356 | else: |
347 | | - self.assertEqual(self.target, result[0].value) |
| 357 | + assert target == result[0].value |
348 | 358 |
|
349 | 359 | # NOTE(sileht): copy of tests/test_jsonpath.py |
350 | 360 | # to ensure we didn't break jsonpath_ng |
|
0 commit comments