0

我正在尝试generators在测试主体而不是config部分内部定义。熟悉pyresttest框架的人,生成器是为您的测试动态定义变量的一种方式文档

---
- config:
    - testset: "Benchmark tests using test app"
    # Variables to use in the test set
    - variable_binds: {firstname: 'Gaius-Test', lastname: 'Baltar-Test'}
    # Generators to use in the test set
    - generators:  
        # Generator named 'id' that counts up from 10
        - 'id': {type: 'number_sequence', start: 10}

- benchmark: # create new entities
    - generator_binds: {user_id: id}
    - name: "Create person"
    - url: {template: "/api/person/$user_id/"}
    - warmup_runs: 0
    - method: 'PUT'
    - headers: {'Content-Type': 'application/json'}
    - body: {template: '{"first_name": "$firstname","id": "$user_id","last_name": "$lastname","login": "test-login-$id"}'}
    - 'benchmark_runs': '1000'
    - output_format: csv
    - metrics:
        - total_time: total
        - total_time: mean

如果您看到该示例,则生成器是在配置部分中定义的,因此该变量id可用于下面定义的所有测试。我的要求是在测试体内定义所有生成器绑定,并想知道是否可能?如果有人能提供一个例子,我将不胜感激。我想要达到的目标如下:

- test: # create new entities
    - generators:
      # Generator named 'id' that counts up from 10
      - 'id': {type: 'number_sequence', start: 100}
    - generator_binds: {user_id: id}
    - name: "Create person"
    - url: {template: "/api/person/$user_id/"}
    - warmup_runs: 0
    - method: 'PUT'
    - headers: {'Content-Type': 'application/json'}
    - body: {template: '{"first_name": "$firstname","id": "$user_id","last_name": "$lastname","login": "test-login-$id"}'}
    - 'benchmark_runs': '1000'
    - output_format: csv
    - metrics:
        - total_time: total
        - total_time: mean

从文档

  • 生成器输出可能会在测试中使用“生成器绑定”绑定到变量

  • 生成器必须在 TestSet 配置中按名称声明才能使用

  • 每个 HTTP 调用生成器绑定评估一次:每个测试仅评估一次,对于基准测试生成器绑定多次仅适用于声明它们的测试/基准。仅在评估绑定时生成新值。

4

1 回答 1

1

PyrestTest 的作者在这里:我不确定我理解你为什么要这样做。

如果您使用的是测试本地的序列,它将始终从同一位置开始,因此您只需在“variable_binds”测试配置元素下定义一个硬编码变量。

如果您想使用在不同测试之间共享状态的生成器,那么您可以在 config 元素中定义它。如果您想生成随机值,这也是明智的。

然而,有一些更高级的动态变量工具可能会有所帮助:https ://github.com/svanoort/pyresttest/issues/101 。

于 2016-05-18T17:03:26.710 回答