没有项目描述
pytest-dictsdiff的Python项目详细描述
pytest dictsdiff
安装
$ pip install pytest-dictsdiff
用法
在您的测试中,使用dicts_are_same
fixture并使用它比较两个
对象:
deftest_dicts(dicts_are_same):dict1={'a':1,'b':2,'c':{'d':4,'e':5}}dict2={'a':100,'f':2,'c':{'d':400,'g':5}}assertdicts_are_same(dict1,dict2)
或使用直接函数调用比较对象:
frompytest_dictsdiffimportcheck_objectsdeftest_dicts():dict1={'a':1,'b':2,'c':{'d':4,'e':5}}dict2={'a':100,'f':2,'c':{'d':400,'g':5}}assertcheck_objects(dict1,dict2)
待办事项
- []显示对象时使用
pprint
而不是json
(当前将失败 如果数据包含任何非json可序列化对象)
示例
将以下代码片段另存为dicts.py
,然后运行pytest dicts.py
。
fromcollectionsimportOrderedDictRESULT=OrderedDict({"cell":"(056)-022-8631","dob":{"age":44,"date":"1983-11-04T01:21:14Z"},"email":"zeyneb.elfring@example.com","gender":"female","id":{"name":"BSN","value":"36180866"},"location":{"city":"tholen","coordinates":{"latitude":"46.8823","longitude":"175.8856"},"postcode":64504,"state":"groningen","street":"2074 adriaen van ostadelaan","timezone":{"description":"Adelaide, Darwin","offset":"+9:30"}},"login":{"md5":"bafe8cf9d37806a7b13edc218d5ff762","password":"ontario","salt":"QVBKgEjy","sha1":"cacef09ff61072d1c55732963766fa84e919aa7a","sha256":"cc86af47aedbdbb1de73ff10484996fe9785c47c0fc191b7c67eaf71e0782300","username":"smallgorilla897","uuid":"37e30c59-bc79-4172-aac6-e2c640e165fa"},"name":{"first":"zeyneb","last":"elfring","title":"mrs"},"nat":"NL","phone":"(209)-143-9697","picture":{"large":"https://randomuser.me/api/portraits/women/37.jpg","medium":"https://randomuser.me/api/portraits/med/women/37.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/women/37.jpg"},"registered":{"age":3,"date":"2014-12-07T06:54:14Z"}})EXPECTED_DATA={"cell":"(056)-022-8631","dob":{"age":34,"date":"1953-11-04T01:21:04Z"},"email":"zeyneb.elfring@example.com","gender":"female","id":{"name":"BSN","value":"36180866"},"location":{"city":"Tholen","coordinates":{"latitude":"46.8823","longitude":"175.8856"},"postcode":64509,"state":"groningen","street":"2074 adriaen van ostadelaan","timezone":{"description":"Adelaide, Darwin","offset":"+9:30"}},"login":{"md5":"bafe8cf9d37806a7b13edc218d5ff762","password":"ontario","salt":"QVBKgEjy","sha1":"cacef09ff61072d1c55732963766fa84e919aa7a","sha256":"cc86af47aedbdbb1de73ff10484996fe9785c47c0fc191b7c67eaf71e0782300","username":"smallgorilla897","uuid":"37e30c59-bc79-4172-aac6-e2c640e165fa"},"name":{"first":"Zeyneb","last":"Elfring","title":"mrs"},"nat":"NL","phone":"(209)-143-9697","picture":{"large":"https://randomuser.me/api/portraits/women/37.jpg","medium":"https://randomuser.me/api/portraits/med/women/37.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/women/37.jpg"},"registered":{"age":3,"date":"2014-12-07T06:54:14Z"}}deftest_compare_dicts(dicts_are_same):assertdicts_are_same(RESULT,EXPECTED_DATA)