pafede2
pafede2

Reputation: 1714

Parametrize set of tests using PyTest

I have the following problem, I need to execute a bunch of tests using pytest which each test is basically the same, the only difference is the parameter.

For instance I have to execute:

  ./command_line arg1
  ./command_line arg2
  ...
  ./command_line argN

And then I need to verify that the executable command always returns an expected given result.

I am aware of this, so I would like to ask for a piece of advice about which would be the best approach for my problem.

I thank you in advance!

Edit: Finally I found the question in StackOverflow where is adviced to take a look to this page which I found useful in my case.

Upvotes: 2

Views: 264

Answers (1)

sashk
sashk

Reputation: 4213

I usually do this using pytest.mark.parametrize and it works like this:

import pytest


@pytest.mark.parametrize('arg, result', [
 ('arg1', 'result1'),
 ('arg2', 'result2'),
 ('arg3', 'result3'),
 ('argN', 'resultN'),
])
def test_cmd0(arg, result):
    out = subprocess.check_output(['cmd', arg])
    assert out.rstrip() == out

where arg1, .. argN - your arguments, and result1, .., resultN your expected results.

In example above, I showed how to launch external command and expect different result on every run. If expected result is same, you can always skip result in parametrization and just do:

assert out.rstrip() == 'expected result'

Upvotes: 2

Related Questions