2016-01-20 21 views
0

我正在使用pyresttest来测试我的api端点。我想将一个测试的输出绑定到一个可以在后续测试中使用的变量。我需要它来测试端点/object/{id},其中ID是由MongoDB生成的哈希。这个散列是随机的,每次数据库播种时都会发生变化。我没有看到任何测试这个端点的方法,没有为数据库播种,搜索一个特定的条目,绑定该条目的ID,然后使用它来测试端点。如何在pyresttest的后续测试中绑定请求的输出以用于后续测试?

回答

1

使用extract_binds元素保存一个测试的输出。在pyresttest/examples/miniapp-extract-validate.yamlpyrestest存储库中有一个使用示例:

- config: 
    - testset: "Demonstrate use of extract after creating a person" 
- test: # create entity by POST 
    - name: "Create person" 
    - url: "/api/person/" 
    - method: "POST" 
    - body: '{"first_name": "Test","last_name": "User","login": "testuser"}' 
    - headers: {Content-Type: application/json} 
    - expected_status: [201] 
    - extract_binds: 
     - 'id': {'jsonpath_mini': 'id'} 
- test: 
    - name: "Get person you just created and validate them" 
    - url: {'template': "/api/person/$id/"} 
    - validators: 
     - compare: {jsonpath_mini: 'id', comparator: 'str_eq', expected: {template: '$id'}} 
     - extract_test: {jsonpath_mini: 'login', test: 'exists'}