背景

先看看 @scenario 的源码

  1. def scenario(feature_name: str, scenario_name: str, encoding: str = "utf-8", features_base_dir=None):
  2. """Scenario decorator.
  3. :param str feature_name: Feature file name. Absolute or relative to the configured feature base path.
  4. :param str scenario_name: Scenario name.
  5. :param str encoding: Feature file encoding.
  6. """

再看看怎么使用 @scenario

  1. @scenario("publish_article.feature", "Payments - Check off-chain payment for checkout via Main App")
  2. def test():
  3. pass

重点

  • 当你使用 @scenario 的时候,如果一个 Feature 有多个 Scenario 时,就要写多个 @scenario
  • 然后 feature_name 就要重复写,挺麻烦的,为了解决这个痛点,可以使用 functools.partial

没使用 partial 之前

  1. from pytest_bdd import scenario
  2. @scenario("publish_article.feature", "Payments - Check off-chain payment for checkout via Main App")
  3. def test():
  4. pass
  5. @scenario("publish_article.feature", "Payments - Check off-chain payment in CRO/BTC/ETH/TUSD/USDC/USDT")
  6. def test():
  7. pass

使用 partial 之后

  1. from functools import partial
  2. import pytest_bdd
  3. scenarioa = partial(pytest_bdd.scenario, "publish_article.feature")
  4. @scenarioa("Payments - Check off-chain payment for checkout via Main App")
  5. def test1():
  6. pass
  7. @scenarioa("Payments - Check off-chain payment in CRO/BTC/ETH/TUSD/USDC/USDT")
  8. def test2():
  9. pass

感觉并不好用,还有 bug,不推荐使用