|
| 1 | +# -*- encoding: utf-8 -*- |
| 2 | + |
| 3 | +from _pytest.terminal import TerminalReporter |
| 4 | + |
| 5 | + |
| 6 | +def add_options(parser): |
| 7 | + group = parser.getgroup("terminal reporting", "reporting", after="general") |
| 8 | + group._addoption( |
| 9 | + '--gherkin-terminal-reporter', |
| 10 | + action="store_true", |
| 11 | + dest="gherkin_terminal_reporter", |
| 12 | + default=False, |
| 13 | + help=( |
| 14 | + "enable gherkin output" |
| 15 | + ) |
| 16 | + ) |
| 17 | + |
| 18 | + |
| 19 | +def configure(config): |
| 20 | + if config.option.gherkin_terminal_reporter: |
| 21 | + # Get the standard terminal reporter plugin and replace it with our |
| 22 | + current_reporter = config.pluginmanager.getplugin('terminalreporter') |
| 23 | + if current_reporter.__class__ != TerminalReporter: |
| 24 | + raise Exception("gherkin-terminal-reporter is not compatibile with any other terminal reporter." |
| 25 | + "You can use only one terminal reporter." |
| 26 | + "Currently '{0}' is used." |
| 27 | + "Please decide to use one by deactivating {0} or gherkin-terminal-reporter." |
| 28 | + .format(current_reporter.__class__)) |
| 29 | + gherkin_reporter = GherkinTerminalReporter(config) |
| 30 | + config.pluginmanager.unregister(current_reporter) |
| 31 | + config.pluginmanager.register(gherkin_reporter, 'terminalreporter') |
| 32 | + if config.pluginmanager.getplugin("dsession"): |
| 33 | + raise Exception("gherkin-terminal-reporter is not compatible with 'xdist' plugin.") |
| 34 | + |
| 35 | + |
| 36 | +class GherkinTerminalReporter(TerminalReporter): |
| 37 | + |
| 38 | + def __init__(self, config): |
| 39 | + TerminalReporter.__init__(self, config) |
| 40 | + |
| 41 | + def pytest_runtest_logstart(self, nodeid, location): |
| 42 | + # Prevent locationline from being printed since we already |
| 43 | + # show the module_name & in verbose mode the test name. |
| 44 | + pass |
| 45 | + |
| 46 | + def pytest_runtest_logreport(self, report): |
| 47 | + rep = report |
| 48 | + res = self.config.hook.pytest_report_teststatus(report=rep) |
| 49 | + cat, letter, word = res |
| 50 | + |
| 51 | + if not letter and not word: |
| 52 | + # probably passed setup/teardown |
| 53 | + return |
| 54 | + |
| 55 | + if isinstance(word, tuple): |
| 56 | + word, word_markup = word |
| 57 | + else: |
| 58 | + if rep.passed: |
| 59 | + word_markup = {'green': True} |
| 60 | + elif rep.failed: |
| 61 | + word_markup = {'red': True} |
| 62 | + elif rep.skipped: |
| 63 | + word_markup = {'yellow': True} |
| 64 | + feature_markup = {'blue': True} |
| 65 | + scenario_markup = word_markup |
| 66 | + |
| 67 | + if self.verbosity <= 0: |
| 68 | + return TerminalReporter.pytest_runtest_logreport(self, rep) |
| 69 | + elif self.verbosity == 1: |
| 70 | + if hasattr(report, 'scenario'): |
| 71 | + self.ensure_newline() |
| 72 | + self._tw.write('Feature: ', **feature_markup) |
| 73 | + self._tw.write(report.scenario['feature']['name'], **feature_markup) |
| 74 | + self._tw.write('\n') |
| 75 | + self._tw.write(' Scenario: ', **scenario_markup) |
| 76 | + self._tw.write(report.scenario['name'], **scenario_markup) |
| 77 | + self._tw.write(' ') |
| 78 | + self._tw.write(word, **word_markup) |
| 79 | + self._tw.write('\n') |
| 80 | + else: |
| 81 | + return TerminalReporter.pytest_runtest_logreport(self, rep) |
| 82 | + elif self.verbosity > 1: |
| 83 | + if hasattr(report, 'scenario'): |
| 84 | + self.ensure_newline() |
| 85 | + self._tw.write('Feature: ', **feature_markup) |
| 86 | + self._tw.write(report.scenario['feature']['name'], **feature_markup) |
| 87 | + self._tw.write('\n') |
| 88 | + self._tw.write(' Scenario: ', **scenario_markup) |
| 89 | + self._tw.write(report.scenario['name'], **scenario_markup) |
| 90 | + self._tw.write('\n') |
| 91 | + for step in report.scenario['steps']: |
| 92 | + self._tw.write(' {} {}\n'.format(step['keyword'], |
| 93 | + step['name']), **scenario_markup) |
| 94 | + self._tw.write(' ' + word, **word_markup) |
| 95 | + self._tw.write('\n\n') |
| 96 | + else: |
| 97 | + return TerminalReporter.pytest_runtest_logreport(self, rep) |
| 98 | + self.stats.setdefault(cat, []).append(rep) |
0 commit comments