作用

  • 可以理解为当前 Feature 下多个 Scenario 都需要用到的 Given,就可以放到 Background 中
  • 可以理解为公共的前置数据(步骤)

注意

  • Backgrounds 只能添加 Given 关键字
  • Backgrounds 的所有步骤都会在所有 Scenario 之前运行完毕,但会在 BeforeScenario Hooks 之后运行

实际例子

background.feature

  1. Feature: Payment
  2. Background:
  3. Given Merchant No is <merchant_no>
  4. And username: polo and password: 123456
  5. @payment @off-chain
  6. Scenario Outline: Payments - Check off-chain payment for checkout via Main App
  7. When Create <amount> <currency> payment via api
  8. Then I select polo.com App and paid by <pay_currency> and verify captured for Checkout
  9. Examples:
  10. | merchant_no | amount | currency | pay_currency |
  11. | Merchant-001 | 200 | USD | USD |
  12. | Merchant-001 | 300 | USD | CNA |
  13. @payment @on-chain
  14. Scenario Outline: Payments - Check off-chain payment in CRO/BTC/ETH/TUSD/USDC/USDT
  15. When Create <amount> <currency> payment via api to website
  16. Then I select polo.com App and paid by <pay_currency> and verify captured for Checkout
  17. Examples:
  18. | merchant_no | amount | currency | pay_currency |
  19. | Merchant-001 | 1 | USD | CRO |
  20. | Merchant-001 | 2 | USD | ETH |
  21. | Merchant-001 | 3 | USD | TUSD |
  22. | Merchant-001 | 4 | USD | USDC |
  23. | Merchant-001 | 5 | USD | USDT |

test_background.py

  1. from pytest_bdd import given, when, then, parsers, scenarios
  2. # Background 的两个 given
  3. @given(parsers.parse("Merchant No is {merchant_no}"))
  4. def get_merchant_no(merchant_no):
  5. return merchant_no
  6. @given(parsers.parse("username: {name} and password: {password}"), target_fixture="user")
  7. def user(name, password):
  8. return dict(name=name, password=password)
  9. @when(parsers.parse("Create {amount} {currency} payment via api"))
  10. def create1(amount, currency, merchant_no, user, name, password):
  11. print(amount, currency, merchant_no, user, name, password)
  12. @when(parsers.parse("Create {amount} {currency} payment via api to website"))
  13. def create2(amount, currency, merchant_no, user, name, password):
  14. print(amount, currency, merchant_no, user, name, password)
  15. @then(parsers.parse("I select {wallet} and paid by {pay_currency} and verify captured for Checkout"))
  16. def walt(wallet, pay_currency, merchant_no, user):
  17. print(wallet, pay_currency, merchant_no, user)
  18. scenarios("backgroud.feature")

命令行运行

  1. pytest -sq test_backgroud.py

运行结果

  1. 200 USD Merchant-001 {'name': 'polo', 'password': '123456'} polo 123456
  2. polo.com App USD Merchant-001 {'name': 'polo', 'password': '123456'}
  3. .300 USD Merchant-001 {'name': 'polo', 'password': '123456'} polo 123456
  4. polo.com App CNA Merchant-001 {'name': 'polo', 'password': '123456'}
  5. .1 USD Merchant-001 {'name': 'polo', 'password': '123456'} polo 123456
  6. polo.com App CRO Merchant-001 {'name': 'polo', 'password': '123456'}
  7. .2 USD Merchant-001 {'name': 'polo', 'password': '123456'} polo 123456
  8. polo.com App ETH Merchant-001 {'name': 'polo', 'password': '123456'}
  9. .3 USD Merchant-001 {'name': 'polo', 'password': '123456'} polo 123456
  10. polo.com App TUSD Merchant-001 {'name': 'polo', 'password': '123456'}
  11. .4 USD Merchant-001 {'name': 'polo', 'password': '123456'} polo 123456
  12. polo.com App USDC Merchant-001 {'name': 'polo', 'password': '123456'}
  13. .5 USD Merchant-001 {'name': 'polo', 'password': '123456'} polo 123456
  14. polo.com App USDT Merchant-001 {'name': 'polo', 'password': '123456'}
  15. .
  16. 7 passed in 0.02s

7 条用例均正常运行