我们可以在pyrestest框架中定义测试体内的生成器

时间:2016-04-26 16:31:07

标签: python automated-tests web-api-testing pyresttest

我想在测试体内定义generators而不是config部分。那些熟悉pyresttest框架,生成器的人是一种为测试动态定义变量的方法Documentation

---
- 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

如果您看到示例,则生成器在config部分中定义,因此变量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调用评估一次:         每次测试仅一次,基准测试多次     生成器绑定仅适用于声明它们的测试/基准。仅在评估绑定时生成新值。

1 个答案:

答案 0 :(得分:1)

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

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

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

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