【作业-接口自动化测试实战2】企业微信接口自动化测试实战2作业

通讯录管理-标签管理接口,至少实现前四个

  1. 使用apiobject 进行改造,分层
  2. 使用jsonpath, jsonschema,hamcrest这三个辅助断言工具,进行断言。

作业-20200614

通讯录管理-标签管理接口,至少实现前四个

  1. 使用apiobject 进行改造,分层
  2. 使用jsonpath, jsonschema,hamcrest这三个辅助断言工具,进行断言。

1、代码

  • base_api
    – base_api.py
    – config.yml
    – label_manager.py
    – we_work.py
  • test_cases
    – test_label_manager.py(注释掉了下面三个验证失败的schema代码
    – test_label_manager_schema_fail.py(test_get_label_member,test_add_label_member,test_delete_label_member的schema验证失败,是什么问题?
    – create_label_schema.json
    – update_label_name_schema.json
    – delete_label_schema.json
    – get_label_member_schema.json
    – add_label_member_schema.json
    – delete_label_member_schema.json
    – getlist_label_schema.json

2、执行结果

2.1、test_label_manager.py执行结果

Testing started at 22:33 ...
G:\python\SET_hogwarts\venv\Scripts\python.exe D:\PyCharm\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py --target test_label_manager.py::TestLabelManager
Launching pytest with arguments test_label_manager.py::TestLabelManager in G:\python\SET_hogwarts\python_base\base20\base20_4_20_9\homework\test_cases

============================= test session starts =============================
platform win32 -- Python 3.8.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- G:\python\SET_hogwarts\venv\Scripts\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.8.3', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '5.4.2', 'py': '1.8.1', 'pluggy': '0.13.1'}, 'Plugins': {'allure-pytest': '2.8.13', 'assume': '2.2.1', 'dependency': '0.5.1', 'forked': '1.1.3', 'html': '2.1.1', 'metadata': '1.9.0', 'ordering': '0.6', 'rerunfailures': '9.0', 'xdist': '1.32.0'}, 'JAVA_HOME': 'G:\\Java\\jdk1.8.0_101'}
rootdir: G:\python\SET_hogwarts\python_base, inifile: pytest.ini
plugins: allure-pytest-2.8.13, assume-2.2.1, dependency-0.5.1, forked-1.1.3, html-2.1.1, metadata-1.9.0, ordering-0.6, rerunfailures-9.0, xdist-1.32.0
collecting ... [<Function test_create_label[testdata0]>, <Function test_create_label[testdata1]>, <Function test_create_label[testdata2]>, <Function test_update_label_name>, <Function test_delete_label>, <Function test_get_label_member>, <Function test_add_label_member>, <Function test_delete_label_member>, <Function test_getlist_label>]
<class 'list'>
collected 9 items

test_label_manager.py::TestLabelManager::test_create_label[testdata0] 
test_label_manager.py::TestLabelManager::test_create_label[testdata1] 
test_label_manager.py::TestLabelManager::test_create_label[testdata2] 
test_label_manager.py::TestLabelManager::test_update_label_name 
test_label_manager.py::TestLabelManager::test_delete_label 
test_label_manager.py::TestLabelManager::test_get_label_member 
test_label_manager.py::TestLabelManager::test_add_label_member 
test_label_manager.py::TestLabelManager::test_delete_label_member 
test_label_manager.py::TestLabelManager::test_getlist_label 

============================== 9 passed in 9.22s ==============================

Process finished with exit code 0
PASSED [ 11%]{'errcode': 0, 'errmsg': 'created', 'tagid': 1}
PASSED [ 22%]{'errcode': 0, 'errmsg': 'created', 'tagid': 2}
PASSED [ 33%]{'errcode': 0, 'errmsg': 'created', 'tagid': 3}
PASSED   [ 44%]{'errcode': 0, 'errmsg': 'updated'}
PASSED        [ 55%]{'errcode': 0, 'errmsg': 'deleted'}
PASSED    [ 66%]{'errcode': 0, 'errmsg': 'ok', 'userlist': [], 'partylist': [], 'tagname': 'Testing1'}
PASSED    [ 77%]{'errcode': 0, 'errmsg': 'ok', 'invalidparty': []}
PASSED [ 88%]{'errcode': 0, 'errmsg': 'deleted', 'invalidparty': []}
PASSED       [100%]{'errcode': 0, 'errmsg': 'ok', 'taglist': [{'tagid': 1, 'tagname': 'Testing1'}, {'tagid': 2, 'tagname': 'Testing2'}]}

2.2、test_label_manager_schema_fail.py执行结果

Testing started at 22:46 ...
G:\python\SET_hogwarts\venv\Scripts\python.exe D:\PyCharm\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py --target test_label_manager_schema_fail.py::TestLabelManager
Launching pytest with arguments test_label_manager_schema_fail.py::TestLabelManager in G:\python\SET_hogwarts\python_base\base20\base20_4_20_9\homework\test_cases

============================= test session starts =============================
platform win32 -- Python 3.8.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- G:\python\SET_hogwarts\venv\Scripts\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.8.3', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '5.4.2', 'py': '1.8.1', 'pluggy': '0.13.1'}, 'Plugins': {'allure-pytest': '2.8.13', 'assume': '2.2.1', 'dependency': '0.5.1', 'forked': '1.1.3', 'html': '2.1.1', 'metadata': '1.9.0', 'ordering': '0.6', 'rerunfailures': '9.0', 'xdist': '1.32.0'}, 'JAVA_HOME': 'G:\\Java\\jdk1.8.0_101'}
rootdir: G:\python\SET_hogwarts\python_base, inifile: pytest.ini
plugins: allure-pytest-2.8.13, assume-2.2.1, dependency-0.5.1, forked-1.1.3, html-2.1.1, metadata-1.9.0, ordering-0.6, rerunfailures-9.0, xdist-1.32.0
collecting ... [<Function test_create_label[testdata0]>, <Function test_create_label[testdata1]>, <Function test_create_label[testdata2]>, <Function test_update_label_name>, <Function test_delete_label>, <Function test_get_label_member>, <Function test_add_label_member>, <Function test_delete_label_member>, <Function test_getlist_label>]
<class 'list'>
collected 9 items

test_label_manager_schema_fail.py::TestLabelManager::test_create_label[testdata0] 
test_label_manager_schema_fail.py::TestLabelManager::test_create_label[testdata1] 
test_label_manager_schema_fail.py::TestLabelManager::test_create_label[testdata2] 
test_label_manager_schema_fail.py::TestLabelManager::test_update_label_name 
test_label_manager_schema_fail.py::TestLabelManager::test_delete_label 
test_label_manager_schema_fail.py::TestLabelManager::test_get_label_member PASSED [ 11%]{'errcode': 0, 'errmsg': 'created', 'tagid': 1}
PASSED [ 22%]{'errcode': 0, 'errmsg': 'created', 'tagid': 2}
PASSED [ 33%]{'errcode': 0, 'errmsg': 'created', 'tagid': 3}
PASSED [ 44%]{'errcode': 0, 'errmsg': 'updated'}
PASSED [ 55%]{'errcode': 0, 'errmsg': 'deleted'}
FAILED [ 66%]{'errcode': 0, 'errmsg': 'ok', 'userlist': [], 'partylist': [], 'tagname': 'Testing1'}

base20\base20_4_20_9\homework\test_cases\test_label_manager_schema_fail.py:68 (TestLabelManager.test_get_label_member)
self = <python_base.base20.base20_4_20_9.homework.test_cases.test_label_manager_schema_fail.TestLabelManager object at 0x0000000004D34C40>

    def test_get_label_member(self):
        """
        *获取标签成员
        """
        tagid=1
        r=self.label_manager.get_label_member(tagid)
        print(r.json())
        # assert r.json()['errcode'] == 0
        # assert jsonpath(r.json(),'$.errcode')[0] == 0
        schema = json.load(open("get_label_member_schema.json"))
>       validate(r.json(), schema)

test_label_manager_schema_fail.py:79: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\..\..\..\..\venv\lib\site-packages\jsonschema\validators.py:930: in validate
    cls.check_schema(schema)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

cls = <class 'jsonschema.validators.create.<locals>.Validator'>
schema = {'$id': 'http://example.com/example.json', '$schema': 'http://json-schema.org/draft-07/schema', 'additionalProperties': True, 'default': {}, ...}

    @classmethod
    def check_schema(cls, schema):
        for error in cls(cls.META_SCHEMA).iter_errors(schema):
>           raise exceptions.SchemaError.create_from(error)
E           jsonschema.exceptions.SchemaError: {'anyOf': [], '$id': '#/properties/partylist/items'} is not valid under any of the given schemas
E           
E           Failed validating 'anyOf' in metaschema['properties']['properties']['additionalProperties']['properties']['items']:
E               {'anyOf': [{'$ref': '#'}, {'$ref': '#/definitions/schemaArray'}],
E                'default': True}
E           
E           On schema['properties']['partylist']['items']:
E               {'$id': '#/properties/partylist/items', 'anyOf': []}

..\..\..\..\..\venv\lib\site-packages\jsonschema\validators.py:294: SchemaError
FAILED [ 77%]{'errcode': 0, 'errmsg': 'ok', 'invalidparty': []}

base20\base20_4_20_9\homework\test_cases\test_label_manager_schema_fail.py:81 (TestLabelManager.test_add_label_member)
self = <python_base.base20.base20_4_20_9.homework.test_cases.test_label_manager_schema_fail.TestLabelManager object at 0x0000000004CE2B20>

    def test_add_label_member(self):
        """
        *增加标签成员
        """
        testdata = {
           "tagid": 1,
           "userlist":[ "cjg1","cjg2","cjg3"],
        }
        r=self.label_manager.add_label_member(testdata)
        print(r.json())
        # assert r.json()['errcode'] == 0
        # assert jsonpath(r.json(), '$.errcode')[0] == 0
        schema = json.load(open("add_label_member_schema.json"))
>       validate(r.json(), schema)

test_label_manager_schema_fail.py:95: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\..\..\..\..\venv\lib\site-packages\jsonschema\validators.py:930: in validate
    cls.check_schema(schema)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

cls = <class 'jsonschema.validators.create.<locals>.Validator'>
schema = {'$id': 'http://example.com/example.json', '$schema': 'http://json-schema.org/draft-07/schema', 'additionalProperties': True, 'default': {}, ...}

    @classmethod
    def check_schema(cls, schema):
        for error in cls(cls.META_SCHEMA).iter_errors(schema):
>           raise exceptions.SchemaError.create_from(error)
E           jsonschema.exceptions.SchemaError: {'anyOf': [], '$id': '#/properties/invalidparty/items'} is not valid under any of the given schemas
E           
E           Failed validating 'anyOf' in metaschema['properties']['properties']['additionalProperties']['properties']['items']:
E               {'anyOf': [{'$ref': '#'}, {'$ref': '#/definitions/schemaArray'}],
E                'default': True}
E           
E           On schema['properties']['invalidparty']['items']:
E               {'$id': '#/properties/invalidparty/items', 'anyOf': []}

..\..\..\..\..\venv\lib\site-packages\jsonschema\validators.py:294: SchemaError
FAILED [ 88%]{'errcode': 0, 'errmsg': 'deleted', 'invalidparty': []}

base20\base20_4_20_9\homework\test_cases\test_label_manager_schema_fail.py:97 (TestLabelManager.test_delete_label_member)
self = <python_base.base20.base20_4_20_9.homework.test_cases.test_label_manager_schema_fail.TestLabelManager object at 0x0000000004FE7BB0>

    def test_delete_label_member(self):
        """
        *删除标签成员
        """
        testdata={
            "tagid": 1,
            "userlist": ["cjg3"]
        }
        r=self.label_manager.delete_label_member(testdata)
        print(r.json())
        # assert r.json()['errcode'] == 0
        # assert jsonpath(r.json(), '$.errcode')[0] == 0
        schema = json.load(open("delete_label_member_schema.json"))
>       validate(r.json(), schema)

test_label_manager_schema_fail.py:111: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\..\..\..\..\venv\lib\site-packages\jsonschema\validators.py:930: in validate
    cls.check_schema(schema)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

cls = <class 'jsonschema.validators.create.<locals>.Validator'>
schema = {'$id': 'http://example.com/example.json', '$schema': 'http://json-schema.org/draft-07/schema', 'additionalProperties': True, 'default': {}, ...}

    @classmethod
    def check_schema(cls, schema):
        for error in cls(cls.META_SCHEMA).iter_errors(schema):
>           raise exceptions.SchemaError.create_from(error)
E           jsonschema.exceptions.SchemaError: {'anyOf': [], '$id': '#/properties/invalidparty/items'} is not valid under any of the given schemas
E           
E           Failed validating 'anyOf' in metaschema['properties']['properties']['additionalProperties']['properties']['items']:
E               {'anyOf': [{'$ref': '#'}, {'$ref': '#/definitions/schemaArray'}],
E                'default': True}
E           
E           On schema['properties']['invalidparty']['items']:
E               {'$id': '#/properties/invalidparty/items', 'anyOf': []}

..\..\..\..\..\venv\lib\site-packages\jsonschema\validators.py:294: SchemaError
PASSED [100%]{'errcode': 0, 'errmsg': 'ok', 'taglist': [{'tagid': 1, 'tagname': 'Testing1'}, {'tagid': 2, 'tagname': 'Testing2'}]}

Assertion failed

Assertion failed

Assertion failed

Assertion failed

Assertion failed

test_label_manager_schema_fail.py::TestLabelManager::test_add_label_member 
test_label_manager_schema_fail.py::TestLabelManager::test_delete_label_member 
test_label_manager_schema_fail.py::TestLabelManager::test_getlist_label 

================================== FAILURES ===================================
___________________ TestLabelManager.test_get_label_member ____________________

self = <python_base.base20.base20_4_20_9.homework.test_cases.test_label_manager_schema_fail.TestLabelManager object at 0x0000000004D34C40>

    def test_get_label_member(self):
        """
        *获取标签成员
        """
        tagid=1
        r=self.label_manager.get_label_member(tagid)
        print(r.json())
        # assert r.json()['errcode'] == 0
        # assert jsonpath(r.json(),'$.errcode')[0] == 0
        schema = json.load(open("get_label_member_schema.json"))
>       validate(r.json(), schema)

test_label_manager_schema_fail.py:79: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\..\..\..\..\venv\lib\site-packages\jsonschema\validators.py:930: in validate
    cls.check_schema(schema)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

cls = <class 'jsonschema.validators.create.<locals>.Validator'>
schema = {'$id': 'http://example.com/example.json', '$schema': 'http://json-schema.org/draft-07/schema', 'additionalProperties': True, 'default': {}, ...}

    @classmethod
    def check_schema(cls, schema):
        for error in cls(cls.META_SCHEMA).iter_errors(schema):
>           raise exceptions.SchemaError.create_from(error)
E           jsonschema.exceptions.SchemaError: {'anyOf': [], '$id': '#/properties/partylist/items'} is not valid under any of the given schemas
E           
E           Failed validating 'anyOf' in metaschema['properties']['properties']['additionalProperties']['properties']['items']:
E               {'anyOf': [{'$ref': '#'}, {'$ref': '#/definitions/schemaArray'}],
E                'default': True}
E           
E           On schema['properties']['partylist']['items']:
E               {'$id': '#/properties/partylist/items', 'anyOf': []}

..\..\..\..\..\venv\lib\site-packages\jsonschema\validators.py:294: SchemaError
---------------------------- Captured stdout call -----------------------------
{'errcode': 0, 'errmsg': 'ok', 'userlist': [], 'partylist': [], 'tagname': 'Testing1'}
___________________ TestLabelManager.test_add_label_member ____________________

self = <python_base.base20.base20_4_20_9.homework.test_cases.test_label_manager_schema_fail.TestLabelManager object at 0x0000000004CE2B20>

    def test_add_label_member(self):
        """
        *增加标签成员
        """
        testdata = {
           "tagid": 1,
           "userlist":[ "cjg1","cjg2","cjg3"],
        }
        r=self.label_manager.add_label_member(testdata)
        print(r.json())
        # assert r.json()['errcode'] == 0
        # assert jsonpath(r.json(), '$.errcode')[0] == 0
        schema = json.load(open("add_label_member_schema.json"))
>       validate(r.json(), schema)

test_label_manager_schema_fail.py:95: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\..\..\..\..\venv\lib\site-packages\jsonschema\validators.py:930: in validate
    cls.check_schema(schema)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

cls = <class 'jsonschema.validators.create.<locals>.Validator'>
schema = {'$id': 'http://example.com/example.json', '$schema': 'http://json-schema.org/draft-07/schema', 'additionalProperties': True, 'default': {}, ...}

    @classmethod
    def check_schema(cls, schema):
        for error in cls(cls.META_SCHEMA).iter_errors(schema):
>           raise exceptions.SchemaError.create_from(error)
E           jsonschema.exceptions.SchemaError: {'anyOf': [], '$id': '#/properties/invalidparty/items'} is not valid under any of the given schemas
E           
E           Failed validating 'anyOf' in metaschema['properties']['properties']['additionalProperties']['properties']['items']:
E               {'anyOf': [{'$ref': '#'}, {'$ref': '#/definitions/schemaArray'}],
E                'default': True}
E           
E           On schema['properties']['invalidparty']['items']:
E               {'$id': '#/properties/invalidparty/items', 'anyOf': []}

..\..\..\..\..\venv\lib\site-packages\jsonschema\validators.py:294: SchemaError
---------------------------- Captured stdout call -----------------------------
{'errcode': 0, 'errmsg': 'ok', 'invalidparty': []}
__________________ TestLabelManager.test_delete_label_member __________________

self = <python_base.base20.base20_4_20_9.homework.test_cases.test_label_manager_schema_fail.TestLabelManager object at 0x0000000004FE7BB0>

    def test_delete_label_member(self):
        """
        *删除标签成员
        """
        testdata={
            "tagid": 1,
            "userlist": ["cjg3"]
        }
        r=self.label_manager.delete_label_member(testdata)
        print(r.json())
        # assert r.json()['errcode'] == 0
        # assert jsonpath(r.json(), '$.errcode')[0] == 0
        schema = json.load(open("delete_label_member_schema.json"))
>       validate(r.json(), schema)

test_label_manager_schema_fail.py:111: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\..\..\..\..\venv\lib\site-packages\jsonschema\validators.py:930: in validate
    cls.check_schema(schema)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

cls = <class 'jsonschema.validators.create.<locals>.Validator'>
schema = {'$id': 'http://example.com/example.json', '$schema': 'http://json-schema.org/draft-07/schema', 'additionalProperties': True, 'default': {}, ...}

    @classmethod
    def check_schema(cls, schema):
        for error in cls(cls.META_SCHEMA).iter_errors(schema):
>           raise exceptions.SchemaError.create_from(error)
E           jsonschema.exceptions.SchemaError: {'anyOf': [], '$id': '#/properties/invalidparty/items'} is not valid under any of the given schemas
E           
E           Failed validating 'anyOf' in metaschema['properties']['properties']['additionalProperties']['properties']['items']:
E               {'anyOf': [{'$ref': '#'}, {'$ref': '#/definitions/schemaArray'}],
E                'default': True}
E           
E           On schema['properties']['invalidparty']['items']:
E               {'$id': '#/properties/invalidparty/items', 'anyOf': []}

..\..\..\..\..\venv\lib\site-packages\jsonschema\validators.py:294: SchemaError
---------------------------- Captured stdout call -----------------------------
{'errcode': 0, 'errmsg': 'deleted', 'invalidparty': []}
=========================== short test summary info ===========================
FAILED test_label_manager_schema_fail.py::TestLabelManager::test_get_label_member
FAILED test_label_manager_schema_fail.py::TestLabelManager::test_add_label_member
FAILED test_label_manager_schema_fail.py::TestLabelManager::test_delete_label_member
========================= 3 failed, 6 passed in 7.29s =========================

Process finished with exit code 1

Assertion failed

Assertion failed

image

源码地址:

https://github.com/15823596020/test_requests/tree/master/test_label

疑问:

在某个业务模块和具体业务这一层,我把methon和url弄成了数据驱动,不知道这样子操作是否规范?

req 是个json结构体。可以直接转yaml,你那样的方式有点笨重了。

作业:通讯录管理-标签管理接口

1、使用apiobject 进行改造,分层

2、使用jsonpath, jsonschema,hamcrest这三个辅助断言工具,进行断言。

代码

说明: jsonschema断言未完成,搞定网站后补上

关闭