Cleaned up hooks

This commit is contained in:
Oleg Pidsadnyi 2016-01-24 00:44:29 +01:00
parent 620a41ec40
commit 1049452a2a
6 changed files with 54 additions and 32 deletions

View File

@ -28,7 +28,7 @@ def add_options(parser):
)
def pytest_configure(config):
def configure(config):
cucumber_json_path = config.option.cucumber_json_path
# prevent opening json log on slave nodes (xdist)
if cucumber_json_path and not hasattr(config, "slaveinput"):
@ -36,7 +36,7 @@ def pytest_configure(config):
config.pluginmanager.register(config._bddcucumberjson)
def pytest_unconfigure(config):
def unconfigure(config):
xml = getattr(config, "_bddcucumberjson", None)
if xml is not None:
del config._bddcucumberjson

View File

@ -39,7 +39,7 @@ def add_options(parser):
)
def pytest_cmdline_main(config):
def cmdline_main(config):
"""Check config option to show missing code."""
if config.option.generate_missing:
return show_missing_code(config)

View File

@ -1,6 +1,4 @@
# -*- encoding: utf-8 -*-
import py
import pytest
from _pytest.terminal import TerminalReporter
@ -18,8 +16,7 @@ def add_options(parser):
)
@pytest.mark.trylast
def pytest_configure(config):
def configure(config):
if config.option.gherkin_terminal_reporter:
# Get the standard terminal reporter plugin and replace it with our
current_reporter = config.pluginmanager.getplugin('terminalreporter')

View File

@ -5,15 +5,9 @@ import pytest
from . import given, when, then
from . import cucumber_json
from . import generation
from . import reporting
from . import gherkin_terminal_reporter
# Import hook handlers:
from .reporting import *
from .cucumber_json import *
from .generation import *
from .gherkin_terminal_reporter import *
from .fixtures import *
@ -41,3 +35,44 @@ def pytest_addoption(parser):
cucumber_json.add_options(parser)
generation.add_options(parser)
gherkin_terminal_reporter.add_options(parser)
def pytest_configure(config):
"""Configure all subplugins."""
cucumber_json.configure(config)
gherkin_terminal_reporter.configure(config)
def pytest_unconfigure(config):
"""Unconfigure all subplugins."""
cucumber_json.unconfigure(config)
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
outcome = yield
reporting.runtest_makereport(item, call, outcome.get_result())
@pytest.mark.tryfirst
def pytest_bdd_before_scenario(request, feature, scenario):
reporting.before_scenario(request, feature, scenario)
@pytest.mark.tryfirst
def pytest_bdd_step_error(request, feature, scenario, step, step_func, step_func_args, exception):
reporting.step_error(request, feature, scenario, step, step_func, step_func_args, exception)
@pytest.mark.tryfirst
def pytest_bdd_before_step(request, feature, scenario, step, step_func):
reporting.before_step(request, feature, scenario, step, step_func)
@pytest.mark.tryfirst
def pytest_bdd_after_step(request, feature, scenario, step, step_func, step_func_args):
reporting.after_step(request, feature, scenario, step, step_func, step_func_args)
def pytest_cmdline_main(config):
generation.cmdline_main(config)

View File

@ -6,8 +6,6 @@ that enriches the pytest test reporting.
import time
import pytest
class StepReport(object):
@ -145,11 +143,8 @@ class ScenarioReport(object):
self.add_step_report(report)
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
def runtest_makereport(item, call, rep):
"""Store item in the report object."""
outcome = yield
rep = outcome.get_result()
try:
scenario_report = item.__scenario_report__
except AttributeError:
@ -159,25 +154,21 @@ def pytest_runtest_makereport(item, call):
rep.item = {"name": item.name}
@pytest.mark.tryfirst
def pytest_bdd_before_scenario(request, feature, scenario):
def before_scenario(request, feature, scenario):
"""Create scenario report for the item."""
request.node.__scenario_report__ = ScenarioReport(scenario=scenario, node=request.node)
@pytest.mark.tryfirst
def pytest_bdd_step_error(request, feature, scenario, step, step_func, step_func_args, exception):
def step_error(request, feature, scenario, step, step_func, step_func_args, exception):
"""Finalize the step report as failed."""
request.node.__scenario_report__.fail()
@pytest.mark.tryfirst
def pytest_bdd_before_step(request, feature, scenario, step, step_func):
def before_step(request, feature, scenario, step, step_func):
"""Store step start time."""
request.node.__scenario_report__.add_step_report(StepReport(step=step))
@pytest.mark.tryfirst
def pytest_bdd_after_step(request, feature, scenario, step, step_func, step_func_args):
def after_step(request, feature, scenario, step, step_func, step_func_args):
"""Finalize the step report as successful."""
request.node.__scenario_report__.current_step_report.finalize(failed=False)

View File

@ -3,8 +3,7 @@ import re
import pytest
from pytest_bdd import scenario, scenarios, given, when, then
from pytest_bdd import exceptions
from pytest_bdd import scenario, given, when, then
@scenario('gherkin_terminal_reporter.feature',
@ -133,7 +132,7 @@ def tests_are_run_with_verbose_mode(testdir, test_execution):
@when("tests are run with very verbose mode")
def tests_are_run_with_verbose_mode(testdir, test_execution):
def tests_are_run_with_very_verbose_mode(testdir, test_execution):
test_execution['regular'] = testdir.runpytest('-vv')
test_execution['gherkin'] = testdir.runpytest('--gherkin-terminal-reporter', '-vv')
@ -153,7 +152,7 @@ def output_should_contain_single_line_scenario_description(test_execution):
@then("output must contain full gherkin scenario description")
def output_should_contain_single_line_scenario_description(test_execution):
def output_should_contain_full_gherkin_scenario_description(test_execution):
ghe = test_execution['gherkin']
assert ghe.ret == 0
ghe.stdout.fnmatch_lines('*Scenario: Scenario example 1')