diff options
author | Thomas Kriechbaumer <thomas@kriechbaumer.name> | 2017-02-01 16:48:46 +0100 |
---|---|---|
committer | Thomas Kriechbaumer <thomas@kriechbaumer.name> | 2017-02-02 12:59:01 +0100 |
commit | ae008ed80b870688e4e0fe5ff305dc40c17458b4 (patch) | |
tree | e5d9fcd24a403f7491d0d6e42364bf91fdb2ac6e /test/conftest.py | |
parent | ec92d7f67e3c5960d9b30e067fb4ed1ae3fc8884 (diff) | |
download | mitmproxy-ae008ed80b870688e4e0fe5ff305dc40c17458b4.tar.gz mitmproxy-ae008ed80b870688e4e0fe5ff305dc40c17458b4.tar.bz2 mitmproxy-ae008ed80b870688e4e0fe5ff305dc40c17458b4.zip |
replace tutils.raises with pytest.raises + shim
Diffstat (limited to 'test/conftest.py')
-rw-r--r-- | test/conftest.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/test/conftest.py b/test/conftest.py index 3f623c51..ef262944 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -1,6 +1,7 @@ import os import pytest import OpenSSL +import functools import mitmproxy.net.tcp @@ -25,6 +26,38 @@ skip_appveyor = pytest.mark.skipif( ) +original_pytest_raises = pytest.raises + + +def raises(exc, *args, **kwargs): + functools.wraps(original_pytest_raises) + if isinstance(exc, str): + return RaisesContext(exc) + else: + return original_pytest_raises(exc, *args, **kwargs) + + +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) |