# -*- coding: utf-8 -*-
import pytest
from _pytest.terminal import TerminalReporter

from . import formatters, parsers


def pytest_addoption(parser):
    group = parser.getgroup('terminal reporting', 'reporting', after='general')
    group.addoption(
        '--testdox', action='store_true', dest='testdox', default=False,
        help='Report test progress in testdox format'
    )


@pytest.mark.trylast
def pytest_configure(config):
    if config.option.testdox:
        # Get the standard terminal reporter plugin and replace it with ours
        standard_reporter = config.pluginmanager.getplugin('terminalreporter')
        testdox_reporter = TestdoxTerminalReporter(standard_reporter.config)
        config.pluginmanager.unregister(standard_reporter)
        config.pluginmanager.register(testdox_reporter, 'terminalreporter')


class TestdoxTerminalReporter(TerminalReporter):

    _last_header = ''

    def pytest_runtest_logreport(self, report):
        if report.when != 'call':
            return

        node = parsers.parse_node(report.nodeid)

        self._tw.line(report.nodeid)

        if node.class_name:
            header = formatters.format_class_name(node.class_name)
        else:
            header = formatters.format_module_name(node.module_name)

        if header != self._last_header:
            self._last_header = header
            self._tw.sep(' ')
            self._tw.line(header)

        self._tw.line('- [{outcome}] {title}'.format(
            outcome=formatters.format_outcome(report.outcome),
            title=formatters.format_title(node.title)
        ))

# -*- coding: utf-8 -*-
import pytest
from _pytest.terminal import TerminalReporter

from . import formatters, parsers


def pytest_addoption(parser):
    group = parser.getgroup('terminal reporting', 'reporting', after='general')
    group.addoption(
        '--testdox', action='store_true', dest='testdox', default=False,
        help='Report test progress in testdox format'
    )


@pytest.mark.trylast
def pytest_configure(config):
    if config.option.testdox:
        # Get the standard terminal reporter plugin and replace it with ours
        standard_reporter = config.pluginmanager.getplugin('terminalreporter')
        testdox_reporter = TestdoxTerminalReporter(standard_reporter.config)
        config.pluginmanager.unregister(standard_reporter)
        config.pluginmanager.register(testdox_reporter, 'terminalreporter')


class TestdoxTerminalReporter(TerminalReporter):

    _last_header = ''

    def pytest_runtest_logreport(self, report):
        if report.when != 'call':
            return

        node = parsers.parse_node(report.nodeid)

        if node.class_name:
            header = formatters.format_class_name(node.class_name)
        else:
            header = formatters.format_module_name(node.module_name)

        if header != self._last_header:
            self._last_header = header
            self._tw.sep(' ')
            self._tw.line(header)

        self._tw.line('- [{outcome}] {title}'.format(
            outcome=formatters.format_outcome(report.outcome),
            title=formatters.format_title(node.title)
        ))

Stop printing the nodeid at the test report