Search code examples
pythonautomated-testsweb-api-testingpyresttest

Can we define generators inside a test body in pyrestest framework


I am trying to define generators inside a test body instead of config section. Those who are familiar with pyresttest framework, generators are a way to dynamically define variables for your test 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

If you see the example, the generators are defined in the config section, so the variable id is available to all the tests defined below. My requirement is to define the all the generator binds inside the test body, and wondering if it is possible? I will highly appreciate if someone could provide an example. What I am trying to achieve is give below:

- 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

From Documentation

  • Generator output may be bound to a variable with 'generator binds' in the test

  • Generators must be declared by name in the TestSet config for them to be used

  • Generator bindings evaluate once per HTTP call: Only once per Test, and multiple times for a Benchmark Generator bindings only apply to the Test/Benchmark they are declared in. New values are generated only when the binding is evaluated.


Solution

  • Author of PyrestTest here: I'm not sure I understand why you're trying to do this.

    If you're using a sequence that is local to the test, it will always start at the same place, so you can just define a hard-coded variable under the 'variable_binds' test config element.

    If you want to use a generator that shares state between different tests, then you define it at the config element. This is also sensible if you want to generate random values.

    There are some more advanced dynamic variable facilities coming that may be helpful, however: https://github.com/svanoort/pyresttest/issues/101 .