Skip to content

Commit

Permalink
Output improvements (#5)
Browse files Browse the repository at this point in the history
Co-authored-by: Guido Schmitz <guido.schmitz@fedaix.de>
  • Loading branch information
Markus Bong and Shutgun authored Jan 31, 2022
1 parent 320be5e commit 87a2eaa
Show file tree
Hide file tree
Showing 7 changed files with 318 additions and 24 deletions.
33 changes: 29 additions & 4 deletions .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,40 @@ jobs:
run: |
python -m pip install --upgrade pip
python -m pip install flake8
flake8 pytest_pretty_terminal --count --show-source --statistics --max-line-length 160
flake8 pytest_pretty_terminal --count --show-source --statistics
flake8 . --count --exit-zero --statistics
- name: Lint with pylint
run: |
python -m pip install pylint pytest
python -m pip install -e .
python -m pip install pylint pytest pylint-pytest
python -m pip install -e .[test]
pylint --errors-only --score=n pytest_pretty_terminal
pylint --exit-zero --score=n --disable=C,E,R --enable=useless-suppression pytest_pretty_terminal pytest_pretty_terminal
pylint --errors-only --score=n --load-plugins pylint_pytest tests
pylint --exit-zero --score=n --disable=E,R,C0103 --max-line-length=160 --enable=useless-suppression --load-plugins pylint_pytest tests
- name: Lint with mypy
run: |
python -m pip install mypy
mypy --ignore-missing-imports pytest_pretty_terminal
mypy pytest_pretty_terminal
mypy tests || true
test:
name: Test with Python ${{ matrix.python-version }}
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10"]
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest-cov
pip install -e .[test]
- name: Test with pytest
run: |
pytest --cov=pytest_pretty_terminal tests
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.1.0] - 2022/01/31

### Added

* Blocked testcases are in blue color now
* Errors in setup or teardown are shown
* We have unittests now :rocket:

## [1.0.2] - 2022/01/25

### Fixed

Fix XFAIL/XPASS handling
* Fix XFAIL/XPASS handling

## [1.0.1] - 2021/11/24

### Fixed

Fix setup
* Fix setup

## [1.0.0] - 2021/11/24

Expand Down
14 changes: 8 additions & 6 deletions pytest_pretty_terminal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

import pytest
from _pytest._io import TerminalWriter
from _pytest.capture import CaptureManager
from _pytest.config import Config
from _pytest.config.argparsing import Parser
from _pytest.logging import _LiveLoggingStreamHandler, get_log_level_for_setting
from _pytest.logging import LoggingPlugin, _LiveLoggingStreamHandler, get_log_level_for_setting
from _pytest.python import Function
from _pytest.reports import CollectReport, TestReport
from _pytest.runner import CallInfo
from _pytest.terminal import TerminalReporter

from ._pretty_terminal_reporter import PrettyTerminalReporter

Expand Down Expand Up @@ -49,7 +51,7 @@ def enable_terminal_report(config: Config):
pretty_terminal_reporter = PrettyTerminalReporter(config)
config.pluginmanager.register(pretty_terminal_reporter, "pretty_terminal_reporter")

capture_manager = config.pluginmanager.getplugin("capturemanager")
capture_manager: CaptureManager = config.pluginmanager.getplugin("capturemanager")

# Capturing needs to be turned off. Otherwise additional output might mess up our terminal.
if getattr(config.option, "capture") != "no":
Expand All @@ -60,15 +62,15 @@ def enable_terminal_report(config: Config):

# The original terminal reporter needs some overwrites because we want to suppress output made during log start and finish.
# However, we need to reregister the terminalreporter to get the overwrites in place.
terminal_reporter = config.pluginmanager.getplugin("terminalreporter")
terminal_reporter: TerminalReporter = config.pluginmanager.getplugin("terminalreporter")
config.pluginmanager.unregister(terminal_reporter)
terminal_reporter.pytest_runtest_logstart = lambda nodeid, location: None
terminal_reporter.pytest_runtest_logfinish = lambda nodeid: None
terminal_reporter.pytest_runtest_logstart = lambda nodeid, location: None # type: ignore
terminal_reporter.pytest_runtest_logfinish = lambda nodeid: None # type: ignore
config.pluginmanager.register(terminal_reporter, "terminalreporter")

# Enable logging and set the loglevel. Without this, live logging would be disabled.
# Still we want to respect to settings made via config.
logging_plugin = config.pluginmanager.getplugin("logging-plugin")
logging_plugin: LoggingPlugin = config.pluginmanager.getplugin("logging-plugin")
logging_plugin.log_cli_handler = _LiveLoggingStreamHandler(terminal_reporter, capture_manager)
logging_plugin.log_cli_level = get_log_level_for_setting(config, "log_cli_level", "log_level") or logging.INFO

Expand Down
33 changes: 21 additions & 12 deletions pytest_pretty_terminal/_pretty_terminal_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
from _pytest._io import TerminalWriter
from _pytest.config import Config
from _pytest.reports import TestReport
from _pytest.terminal import TerminalReporter
from _pytest.terminal import TerminalReporter, _color_for_type

COLORMAP = {
"passed": {"green": True, "bold": True},
"failed": {"red": True, "bold": True},
"blocked": {"blue": True, "bold": True},
"error": {"red": True, "bold": True},
"failed": {"red": True, "bold": True},
"passed": {"green": True, "bold": True},
"skipped": {"yellow": True, "bold": True},
"xfailed": {"yellow": True, "bold": True},
"xpassed": {"yellow": True, "bold": True}
}
_color_for_type["blocked"] = "blue"


class PrettyTerminalReporter:
Expand Down Expand Up @@ -51,7 +53,7 @@ def pytest_runtest_logreport(self, report: TestReport):
if (getattr(self.config.option, "numprocesses", 0) or 0) < 2:
self._print_docstring_and_params(title, user_properties)

if not report.skipped:
if not report.skipped and report.outcome != "failed":
return

if (getattr(self.config.option, "numprocesses", 0) or 0) > 1:
Expand All @@ -65,6 +67,8 @@ def pytest_runtest_logreport(self, report: TestReport):
outcome = "xfailed"
elif hasattr(report, "wasxfail") and report.outcome == "passed":
outcome = "xpassed"
elif report.failed and report.when in ("setup", "teardown"):
outcome = "error"
else:
outcome = report.outcome

Expand All @@ -84,16 +88,21 @@ def pytest_report_teststatus(self, report: TestReport) -> Optional[Tuple[str, st

outcome: str = report.outcome
if report.when in ("collect", "setup", "teardown"):
if hasattr(report, "wasxfail") and report.skipped:
outcome = "xfailed"
elif hasattr(report, "wasxfail") and report.passed:
outcome = "xpassed"
elif outcome == "failed":

if outcome == "failed":
outcome = "error"
elif getattr(report, "blocked", False): # Establish compatibility to pytest-adaptavist
outcome = "blocked"
elif not report.skipped:
elif outcome == "passed":
outcome = ""

if report.when == "call" and hasattr(report, "wasxfail"):
if report.skipped:
outcome = "xfailed"
elif report.passed:
outcome = "xpassed"
if getattr(report, "blocked", False): # Establish compatibility to pytest-adaptavist
outcome = "blocked"
elif outcome == "skipped":
outcome = "skipped"
return outcome, "", ""

def _print_docstring_and_params(self, title: str, user_properties: Dict[str, Any]):
Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
max-line-length = 160
ignore = W503
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
platforms="any",
python_requires=">=3.8",
install_requires=["pytest>=3.4.1"],
extras_require={"test": ["pytest-adaptavist>=5.1.1"]},
setup_requires=['setuptools_scm'],
keywords="python pytest adaptavist kanoah tm4j jira test testmanagement report",
classifiers=[
Expand Down
Loading

0 comments on commit 87a2eaa

Please sign in to comment.