我创建了一个Django应用程序,并且非常依赖pytest
来发现和组织
我的单位和功能测试。但是,我希望将行为驱动与behave
开发一起应用于将来的测试。遗憾的是,behave
未自动检测到pytest
测试功能。
如何将behave
及其'测试集成到pytest
发现,执行和报告中?
答案 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}"
注意:
feature
、element
或 step
状态与 Enum
中的 behave.model_core.Status
进行比较behave
作为子过程而不是其内部 API。适当的迭代会考虑
behave.runner:Runner
、behave.runner:ModelRunner
和触发器。