背景
先看看 @scenario 的源码
def scenario(feature_name: str, scenario_name: str, encoding: str = "utf-8", features_base_dir=None):
"""Scenario decorator.
:param str feature_name: Feature file name. Absolute or relative to the configured feature base path.
:param str scenario_name: Scenario name.
:param str encoding: Feature file encoding.
"""
再看看怎么使用 @scenario
@scenario("publish_article.feature", "Payments - Check off-chain payment for checkout via Main App")
def test():
pass
重点
- 当你使用 @scenario 的时候,如果一个 Feature 有多个 Scenario 时,就要写多个 @scenario
- 然后 feature_name 就要重复写,挺麻烦的,为了解决这个痛点,可以使用
functools.partial
没使用 partial 之前
from pytest_bdd import scenario
@scenario("publish_article.feature", "Payments - Check off-chain payment for checkout via Main App")
def test():
pass
@scenario("publish_article.feature", "Payments - Check off-chain payment in CRO/BTC/ETH/TUSD/USDC/USDT")
def test():
pass
使用 partial 之后
from functools import partial
import pytest_bdd
scenarioa = partial(pytest_bdd.scenario, "publish_article.feature")
@scenarioa("Payments - Check off-chain payment for checkout via Main App")
def test1():
pass
@scenarioa("Payments - Check off-chain payment in CRO/BTC/ETH/TUSD/USDC/USDT")
def test2():
pass
感觉并不好用,还有 bug,不推荐使用