svanoort / pyresttest

Python Rest Testing
Apache License 2.0
1.15k stars 325 forks source link

Request: Loop a test #244

Open cniackz opened 7 years ago

cniackz commented 7 years ago

https://stackoverflow.com/questions/43811310/how-to-create-a-loop-in-pyresttest

I am trying to create a loop in PyRestTest, this is my code:

# Login
- test:
  - group: 'Login'
  - name: 'Test login post'
  - url: {template: 'api/login/'}
  - method: 'POST'
  - body: {template:'{"username":"$username",
                      "email":"$username",
                      "password":"$password"}'}
  - headers: {Content-Type: application/json}
  - extract_binds:
    - 'token': {'jsonpath_mini': "key"}

# Logout
- test:
  - group: 'Logout'
  - name: 'Test logout post'
  - url: {template: 'api/logout/'}
  - method: 'POST'
  - headers: {Content-Type: application/json}

How can I execute this code over and over?. I am expecting something like:

loop(10):
    # Login
    ...
    # Logout
    ...

In order to execute 10 times these tests. Is it possible?

BTW, I don't want to use a benchmark because it does not throw if the test passed or failed

mravitej commented 7 years ago

Hi @cniackz. Can i know the command to pass parameters from outside. i.e how did you passed username and password while executing the command? Thanks in advance, Ravi Teja

cniackz commented 7 years ago

Hello @mravitej

In the top of the file I have these lines below:

- config:
  - testset: 'Basic Tests'
  - variable_binds: {'username': 'admin@company.com', 'password': 'admin',
                     'espuser_url': 'api/user/v0.1',
                     'authbkend_url': 'api/authentication/v0.1'}

Regards, -Cesar

nitzanav commented 6 years ago

@cniackz So did you do it? I will soon, but wanted to ask if you have any input of code.

nitzanav commented 6 years ago

I am willing to pay for development hours for developing a PT of "foreach: sequence" that will repeat a test for each element in the array.

It will need to get array from a json response in test1 and loop on test2 for element in that array. it will need to add the variable name and value to the test name

svanoort commented 6 years ago

@nitzanav I think you may have hunted me down on LinkedIn a while back. While I don't have a ton of outside-of-work time (which is why side projects such as this have languished), I would consider doing some smaller features like this on a contract basis for the right amount. Drop me a message with a bit more details if you like.