Python:如何将行为整合到pytest中?

时间:2016-12-14 15:44:59

标签: python bdd pytest python-behave

我创建了一个Django应用程序,并且非常依赖pytest来发现和组织 我的单位和功能测试。但是,我希望将行为驱动与behave开发一起应用于将来的测试。遗憾的是,behave未自动检测到pytest测试功能。

如何将behave及其'测试集成到pytest发现,执行和报告中?

2 个答案:

答案 0 :(得分:6)

Pytest和表现是两个独立的测试跑步者。

行为测试有一个pytest plugin,它也使用Gherkin作为DSL,但步骤的实现使用的语法与行为的语法不同,所以我认为你不能直接运行你创建的步骤用它。

答案 1 :(得分:-1)

遵循pytest docs example,您可以获得如下输出:

______________________________________________________________ Feature: Fight or flight  - Scenario: Stronger opponent ______________________________________________________________

Feature: Fight or flight
  Scenario: Stronger opponent
    Step [OK]: the ninja has a third level black-belt
    Step [ERR]: attacked by Chuck Norris
      Traceback (most recent call last):
        File ".venv/lib/python3.6/site-packages/behave/model.py", line 1329, in run
          match.run(runner.context)
        File ".venv/lib/python3.6/site-packages/behave/matchers.py", line 98, in run
          self.func(context, *args, **kwargs)
        File "tests/bdd/steps/tutorial.py", line 23, in step_impl4
          raise NotImplementedError('STEP: When attacked by Chuck Norris')
      NotImplementedError: STEP: When attacked by Chuck Norris
    Step [NOT REACHED]: the ninja should run for his life

带有来自 behaves tutorial 的特征文件

为了使 pytest 运行正常,您可以在 conftest.py 中使用以下代码段:

# content of conftest.py

import pytest


class BehaveException(Exception):
    """Custom exception for error reporting."""

def pytest_collect_file(parent, path):
    """Allow .feature files to be parsed for bdd."""
    if path.ext == ".feature":
        return BehaveFile.from_parent(parent, fspath=path)

class BehaveFile(pytest.File):
    def collect(self):
        from behave.parser import parse_file
        feature = parse_file(self.fspath)
        for scenario in feature.walk_scenarios(with_outlines=True):
            yield BehaveFeature.from_parent(
                self,
                name=scenario.name,
                feature=feature,
                scenario=scenario,
            )


class BehaveFeature(pytest.Item):

    def __init__(self, name, parent, feature, scenario):
        super().__init__(name, parent)
        self._feature = feature
        self._scenario = scenario

    def runtest(self):
        import subprocess as sp
        from shlex import split

        feature_name = self._feature.filename
        cmd = split(f"""behave tests/bdd/ 
            --format json 
            --no-summary
            --include {feature_name}
            -n "{self._scenario.name}"
        """)

        try:
            proc = sp.run(cmd, stdout=sp.PIPE)
            if not proc.returncode:
                return
        except Exception as exc:
            raise BehaveException(self, f"exc={exc}, feature={feature_name}")

        stdout = proc.stdout.decode("utf8")
        raise BehaveException(self, stdout)

    def repr_failure(self, excinfo):
        """Called when self.runtest() raises an exception."""
        import json
        if isinstance(excinfo.value, BehaveException):
            feature = excinfo.value.args[0]._feature
            results = excinfo.value.args[1]
            data = json.loads(results)
            summary = ""
            for feature in data:
                if feature['status'] != "failed":
                    continue
                summary += f"\nFeature: {feature['name']}"
                for element in feature["elements"]:
                    if element['status'] != "failed":
                        continue
                    summary += f"\n  {element['type'].title()}: {element['name']}"
                    for step in element["steps"]:
                        try:
                            result = step['result']
                        except KeyError:
                            summary += f"\n    Step [NOT REACHED]: {step['name']}"
                            continue
                        status = result['status']
                        if status != "failed":
                            summary += f"\n    Step [OK]: {step['name']}"
                        else:
                            summary += f"\n    Step [ERR]: {step['name']}"
                            summary += "\n      " + "\n      ".join(result['error_message'])

            return summary

    def reportinfo(self):
        return self.fspath, 0, f"Feature: {self._feature.name}  - Scenario: {self._scenario.name}"


注意:

  1. 需要适当的状态解码,例如将 featureelementstep 状态与 Enum 中的 behave.model_core.Status 进行比较
  2. 此代码段将调用 behave 作为子过程而不是其内部 API。适当的迭代会考虑
    1. 从同一进程中继承 behave.runner:Runnerbehave.runner:ModelRunner 和触发器。
    2. 在 repr_failure 中使用行为格式化程序,而不是手动解码 json 输出
  3. 您可以通过定位整个功能或特定步骤来实现更多/更少的粒度,但此代码段仅展示了针对场景的演示
  4. 由于 (1),您不会收集数据,例如用于报道目的...