diff options
Diffstat (limited to 'test/conftest.py')
-rw-r--r-- | test/conftest.py | 51 |
1 files changed, 21 insertions, 30 deletions
diff --git a/test/conftest.py b/test/conftest.py index 4b05624b..83823a19 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -2,6 +2,7 @@ import os import pytest import OpenSSL import functools +from contextlib import contextmanager import mitmproxy.net.tcp @@ -29,35 +30,19 @@ skip_appveyor = pytest.mark.skipif( original_pytest_raises = pytest.raises +# TODO: remove this wrapper when pytest 3.1.0 is released +@contextmanager @functools.wraps(original_pytest_raises) def raises(exc, *args, **kwargs): - if isinstance(exc, str): - return RaisesContext(exc) - else: - return original_pytest_raises(exc, *args, **kwargs) + with original_pytest_raises(exc, *args, **kwargs) as exc_info: + yield + if 'match' in kwargs: + assert exc_info.match(kwargs['match']) pytest.raises = raises -class RaisesContext: - def __init__(self, expected_exception): - self.expected_exception = expected_exception - - def __enter__(self): - return - - def __exit__(self, exc_type, exc_val, exc_tb): - if not exc_type: - raise AssertionError("No exception raised.") - else: - if self.expected_exception.lower() not in str(exc_val).lower(): - raise AssertionError( - "Expected %s, but caught %s" % (repr(self.expected_exception), repr(exc_val)) - ) - return True - - @pytest.fixture() def disable_alpn(monkeypatch): monkeypatch.setattr(mitmproxy.net.tcp, 'HAS_ALPN', False) @@ -118,15 +103,17 @@ def pytest_runtestloop(session): measured_files = [os.path.normpath(os.path.relpath(f, prefix)) for f in cov.get_data().measured_files()] measured_files = [f for f in measured_files if not any(f.startswith(excluded_f) for excluded_f in excluded_files)] - for name in pytest.config.option.full_cov: + for name in coverage_values.keys(): files = [f for f in measured_files if f.startswith(os.path.normpath(name))] try: with open(os.devnull, 'w') as null: - coverage_values[name] = cov.report(files, ignore_errors=True, file=null) + overall = cov.report(files, ignore_errors=True, file=null) + singles = [(s, cov.report(s, ignore_errors=True, file=null)) for s in files] + coverage_values[name] = (overall, singles) except: pass - if any(v < 100 for v in coverage_values.values()): + if any(v < 100 for v, _ in coverage_values.values()): # make sure we get the EXIT_TESTSFAILED exit code session.testsfailed += 1 else: @@ -147,12 +134,15 @@ def pytest_terminal_summary(terminalreporter, exitstatus): msg = "FAIL: Full test coverage not reached!\n" terminalreporter.write(msg, **markup) - for name, value in coverage_values.items(): - if value < 100: + for name in sorted(coverage_values.keys()): + msg = 'Coverage for {}: {:.2f}%\n'.format(name, coverage_values[name][0]) + if coverage_values[name][0] < 100: markup = {'red': True, 'bold': True} + for s, v in sorted(coverage_values[name][1]): + if v < 100: + msg += ' {}: {:.2f}%\n'.format(s, v) else: markup = {'green': True} - msg = 'Coverage for {}: {:.2f}%\n'.format(name, value) terminalreporter.write(msg, **markup) else: markup = {'green': True} @@ -160,6 +150,7 @@ def pytest_terminal_summary(terminalreporter, exitstatus): msg += '{}\n\n'.format('\n'.join(pytest.config.option.full_cov)) terminalreporter.write(msg, **markup) - msg = 'Excluded files:\n' - msg += '{}\n'.format('\n'.join(pytest.config.option.no_full_cov)) + msg = '\nExcluded files:\n' + for s in sorted(pytest.config.option.no_full_cov): + msg += " {}\n".format(s) terminalreporter.write(msg) |