pytest parameters
时间: 2023-09-20 20:14:41 浏览: 55
Pytest parameters allow you to run the same test with different input values or configurations. You can use the `@pytest.mark.parametrize` decorator to define multiple sets of inputs for a test function.
Here's an example:
```python
import pytest
@pytest.mark.parametrize("input_value,expected_result", [
(5, 10),
(10, 20),
(15, 30)
])
def test_multiply_by_two(input_value, expected_result):
result = input_value * 2
assert result == expected_result
```
In this example, the `test_multiply_by_two` function is decorated with `@pytest.mark.parametrize`. The decorator takes two arguments: the names of the parameters (`input_value` and `expected_result`), and a list of tuples representing the different sets of inputs.
When running this test, pytest will execute the `test_multiply_by_two` function three times, each time with a different set of inputs. The test passes if the computed result (`result`) matches the expected result.
You can add as many parameters and sets of inputs as you need for your tests. This approach helps reduce code duplication and makes it easy to test different scenarios with minimal effort.