私は依存ライブラリが少なめのpython_wrap_casesを好んで使っています。
少しトリッキーですが、_INVALID_DATA
次第でいくらでもケースを増やせて、なおかつ実行時にいい感じの出力をしてくれる様にしてみました。
python
1from itertools import chain
2from itertools import repeat
3from functools import partial
4from functools import update_wrapper
5import json
6from unittest import TestCase
7
8from python_wrap_cases import wrap_case
9
10
11def partial_with_doc(fn, *args, **kwargs):
12 return update_wrapper(partial(fn, *args, **kwargs), fn)
13
14
15def post(path, data, content_type="application/json", **kwargs):
16 # return 400
17 return client.post(
18 path,
19 data=json.dumps(data),
20 content_type=content_type,
21 **kwargs,
22 ).status_code
23
24
25@wrap_case
26class PostTestCase(TestCase):
27 def assertBadRequest(self, verb, path, **kwargs):
28 """Check if request ends up with 400"""
29 self.assertEqual(400, verb(path, **kwargs))
30
31 _INVALID_DATA = chain(
32 zip([True, 1, [1], {"a": 1}], repeat("taro")),
33 zip(repeat("Tokyo"), [True, 1, [1], {"a": 1}]),
34 )
35 for _i, (_area, _person) in enumerate(_INVALID_DATA):
36 locals()["test_post_validation_err_%d" % _i] = wrap_case(
37 path="/api/hoge/fuga/1",
38 data=dict(area=_area, person=_person, age=23, id=13),
39 )(partial_with_doc(assertBadRequest, verb=post))
40 del _INVALID_DATA, _i, _area, _person
41
42
43if __name__ == "__main__":
44 from unittest import main
45 main(verbosity=2)
46
実行時の出力例
test_post_validation_err_0_data({'area': True, 'person': 'taro', 'age': 23, 'id': 13})_path(/api/hoge/fuga/1) (__main__.PostTestCase)
Check if request ends up with 400 ... ERROR
test_post_validation_err_1_data({'area': 1, 'person': 'taro', 'age': 23, 'id': 13})_path(/api/hoge/fuga/1) (__main__.PostTestCase)
Check if request ends up with 400 ... ERROR
test_post_validation_err_2_data({'area': [1], 'person': 'taro', 'age': 23, 'id': 13})_path(/api/hoge/fuga/1) (__main__.PostTestCase)
Check if request ends up with 400 ... ERROR
test_post_validation_err_3_data({'area': {'a': 1}, 'person': 'taro', 'age': 23, 'id': 13})_path(/api/hoge/fuga/1) (__main__.PostTestCase)
Check if request ends up with 400 ... ERROR
test_post_validation_err_4_data({'area': 'Tokyo', 'person': True, 'age': 23, 'id': 13})_path(/api/hoge/fuga/1) (__main__.PostTestCase)
Check if request ends up with 400 ... ERROR
test_post_validation_err_5_data({'area': 'Tokyo', 'person': 1, 'age': 23, 'id': 13})_path(/api/hoge/fuga/1) (__main__.PostTestCase)
Check if request ends up with 400 ... ERROR
test_post_validation_err_6_data({'area': 'Tokyo', 'person': [1], 'age': 23, 'id': 13})_path(/api/hoge/fuga/1) (__main__.PostTestCase)
Check if request ends up with 400 ... ERROR
test_post_validation_err_7_data({'area': 'Tokyo', 'person': {'a': 1}, 'age': 23, 'id': 13})_path(/api/hoge/fuga/1) (__main__.PostTestCase)
Check if request ends up with 400 ... ERROR
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/21 15:51