pytest-bdd demo
时间: 2023-08-07 22:59:03 浏览: 122
Sure! Here's a simple example of how you can use pytest-bdd for behavior-driven development (BDD) testing in Python.
First, make sure you have pytest-bdd installed. You can do this by running `pip install pytest-bdd`.
Next, create a new Python file called `test_demo.py` and add the following code:
```python
import pytest
from pytest_bdd import scenario, given, when, then
@scenario('demo.feature', 'Add two numbers')
def test_add_numbers():
pass
@given("I have two numbers <a> and <b>")
def numbers(a, b):
return int(a), int(b)
@when("I add the numbers")
def add_numbers(numbers):
return sum(numbers)
@then("I expect the result <result>")
def expect_result(add_numbers, result):
assert add_numbers == int(result)
```
Next, create a new file called `demo.feature` and add the following code:
```gherkin
Feature: Addition
Scenario: Add two numbers
Given I have two numbers <a> and <b>
When I add the numbers
Then I expect the result <result>
Examples:
| a | b | result |
| 2 | 3 | 5 |
| 5 | 7 | 12 |
```
Now, you can run the tests by executing `pytest` in your terminal. pytest-bdd will automatically discover and execute your BDD scenarios.
This is just a basic example to get you started with pytest-bdd. You can define more complex scenarios and steps as per your testing needs. Happy testing!
阅读全文