作用
- 可以理解为当前 Feature 下多个 Scenario 都需要用到的 Given,就可以放到 Background 中
- 可以理解为公共的前置数据(步骤)
注意
- Backgrounds 只能添加 Given 关键字
- Backgrounds 的所有步骤都会在所有 Scenario 之前运行完毕,但会在 BeforeScenario Hooks 之后运行
实际例子
background.feature
Feature: Payment
Background:
Given Merchant No is <merchant_no>
And username: polo and password: 123456
@payment @off-chain
Scenario Outline: Payments - Check off-chain payment for checkout via Main App
When Create <amount> <currency> payment via api
Then I select polo.com App and paid by <pay_currency> and verify captured for Checkout
Examples:
| merchant_no | amount | currency | pay_currency |
| Merchant-001 | 200 | USD | USD |
| Merchant-001 | 300 | USD | CNA |
@payment @on-chain
Scenario Outline: Payments - Check off-chain payment in CRO/BTC/ETH/TUSD/USDC/USDT
When Create <amount> <currency> payment via api to website
Then I select polo.com App and paid by <pay_currency> and verify captured for Checkout
Examples:
| merchant_no | amount | currency | pay_currency |
| Merchant-001 | 1 | USD | CRO |
| Merchant-001 | 2 | USD | ETH |
| Merchant-001 | 3 | USD | TUSD |
| Merchant-001 | 4 | USD | USDC |
| Merchant-001 | 5 | USD | USDT |
test_background.py
from pytest_bdd import given, when, then, parsers, scenarios
# Background 的两个 given
@given(parsers.parse("Merchant No is {merchant_no}"))
def get_merchant_no(merchant_no):
return merchant_no
@given(parsers.parse("username: {name} and password: {password}"), target_fixture="user")
def user(name, password):
return dict(name=name, password=password)
@when(parsers.parse("Create {amount} {currency} payment via api"))
def create1(amount, currency, merchant_no, user, name, password):
print(amount, currency, merchant_no, user, name, password)
@when(parsers.parse("Create {amount} {currency} payment via api to website"))
def create2(amount, currency, merchant_no, user, name, password):
print(amount, currency, merchant_no, user, name, password)
@then(parsers.parse("I select {wallet} and paid by {pay_currency} and verify captured for Checkout"))
def walt(wallet, pay_currency, merchant_no, user):
print(wallet, pay_currency, merchant_no, user)
scenarios("backgroud.feature")
命令行运行
pytest -sq test_backgroud.py
运行结果
200 USD Merchant-001 {'name': 'polo', 'password': '123456'} polo 123456
polo.com App USD Merchant-001 {'name': 'polo', 'password': '123456'}
.300 USD Merchant-001 {'name': 'polo', 'password': '123456'} polo 123456
polo.com App CNA Merchant-001 {'name': 'polo', 'password': '123456'}
.1 USD Merchant-001 {'name': 'polo', 'password': '123456'} polo 123456
polo.com App CRO Merchant-001 {'name': 'polo', 'password': '123456'}
.2 USD Merchant-001 {'name': 'polo', 'password': '123456'} polo 123456
polo.com App ETH Merchant-001 {'name': 'polo', 'password': '123456'}
.3 USD Merchant-001 {'name': 'polo', 'password': '123456'} polo 123456
polo.com App TUSD Merchant-001 {'name': 'polo', 'password': '123456'}
.4 USD Merchant-001 {'name': 'polo', 'password': '123456'} polo 123456
polo.com App USDC Merchant-001 {'name': 'polo', 'password': '123456'}
.5 USD Merchant-001 {'name': 'polo', 'password': '123456'} polo 123456
polo.com App USDT Merchant-001 {'name': 'polo', 'password': '123456'}
.
7 passed in 0.02s
7 条用例均正常运行