【问题标题】:Patterns for save data between tests in a sequence of test用于在测试序列中的测试之间保存数据的模式
【发布时间】:2018-03-30 17:15:13
【问题描述】:

当我需要获取一些数据并在其他请求中使用时,我可以使用哪些模式进行 API 测试?

下一个案例举例:

def test_item_get():
    r = get_json('/item')
    assert r.status_code == 200


def test_item_update():
    r = get_json('/item')
    assert r.status_code == 200

    item_uuid = r.json[0]['uuid']
    assert is_uuid(item_uuid)

    r = put_json('/item/{}'.format(item_uuid), {'description': 'New desc'})
    assert r.status_code == 200


def test_item_manager():
    r = get_json('/item')
    assert r.status_code == 200

    item_uuid = r.json[0]['uuid']
    assert is_uuid(item_uuid)

    r = put_json('/item/{}'.format(item_uuid), {'description': 'New desc'})
    assert r.status_code == 200

    r = get_json('/item/{}'.format(item_uuid))
    assert r.status_code == 200
    assert r.json['description'] = 'New desc'

    r = delete_json('/item/{}'.format(item_uuid))
    assert r.status_code == 200
    assert r.json['result'] == True

    r = delete_json('/item/{}'.format(item_uuid))
    assert r.status_code == 404

看来我应该将test_item_manager 分成更小的部分,但我不确定该选择哪种方式。

完美,如果有pytestunittest 的方法,但其他测试模块甚至是类似任务解决方案的源代码链接都会很好。

【问题讨论】:

    标签: python testing integration-testing pytest python-unittest


    【解决方案1】:

    理想情况下,您必须将test_item_manager 分成多个测试,以分别测试每个 CRUD 操作。您可以拥有一个功能级别的夹具,它将为您提供item_uuid,您可以在其上执行您的操作。当然,您必须确保您的item_uuid 夹具为每个测试返回一个有效的uuid

    对于 e.x

    @pytest.fixture
    def item_uuid():
        r = get_json('/item')
        assert r.status_code == 200
        return r.json[0]['uuid']
    
    def test_item_get():
        # You can keep this test or remove as it gets covered under fixture.
        r = get_json('/item')
        assert r.status_code == 200
    
    
    def test_item_update(item_uuid):
        assert is_uuid(item_uuid)
    
        r = put_json('/item/{}'.format(item_uuid), {'description': 'New desc'})
        assert r.status_code == 200
        assert r.json['description'] = 'New desc'
    
    
    def test_item_delete(item_uuid):
       r = delete_json('/item/{}'.format(item_uuid))
       assert r.status_code == 200
       assert r.json['result'] == True
    
       r = delete_json('/item/{}'.format(item_uuid))
       assert r.status_code == 404
    

    【讨论】:

      猜你喜欢
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 2014-07-27
      • 2020-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多