|
3 | 3 |
|
4 | 4 | import os
|
5 | 5 | import signal
|
| 6 | +import sys |
6 | 7 | import time
|
| 8 | +from contextlib import contextmanager |
7 | 9 |
|
8 | 10 | import pytest
|
9 | 11 | import zmq
|
@@ -50,7 +52,6 @@ def generate_output():
|
50 | 52 | a rich displayable object.
|
51 | 53 | """
|
52 | 54 |
|
53 |
| - import sys |
54 | 55 | from IPython.core.display import display, HTML, Math
|
55 | 56 |
|
56 | 57 | print("stdout")
|
@@ -83,6 +84,28 @@ def skip_without_names(f, *args, **kwargs):
|
83 | 84 | return skip_without_names
|
84 | 85 |
|
85 | 86 |
|
| 87 | +@contextmanager |
| 88 | +def raises_remote(etype): |
| 89 | + if isinstance(etype, str): |
| 90 | + # allow Exception or 'Exception' |
| 91 | + expected_ename = etype |
| 92 | + else: |
| 93 | + expected_ename = etype.__name__ |
| 94 | + |
| 95 | + try: |
| 96 | + try: |
| 97 | + yield |
| 98 | + except error.CompositeError as e: |
| 99 | + e.raise_exception() |
| 100 | + except error.RemoteError as e: |
| 101 | + assert ( |
| 102 | + expected_ename == e.ename |
| 103 | + ), f"Should have raised {expected_ename}, but raised {e.ename}" |
| 104 | + |
| 105 | + else: |
| 106 | + pytest.fail("should have raised a RemoteError") |
| 107 | + |
| 108 | + |
86 | 109 | # -------------------------------------------------------------------------------
|
87 | 110 | # Classes
|
88 | 111 | # -------------------------------------------------------------------------------
|
@@ -130,19 +153,8 @@ def connect_client(self):
|
130 | 153 | return c
|
131 | 154 |
|
132 | 155 | def assertRaisesRemote(self, etype, f, *args, **kwargs):
|
133 |
| - try: |
134 |
| - try: |
135 |
| - f(*args, **kwargs) |
136 |
| - except error.CompositeError as e: |
137 |
| - e.raise_exception() |
138 |
| - except error.RemoteError as e: |
139 |
| - self.assertEqual( |
140 |
| - etype.__name__, |
141 |
| - e.ename, |
142 |
| - "Should have raised %r, but raised %r" % (etype.__name__, e.ename), |
143 |
| - ) |
144 |
| - else: |
145 |
| - self.fail("should have raised a RemoteError") |
| 156 | + with raises_remote(etype): |
| 157 | + f(*args, **kwargs) |
146 | 158 |
|
147 | 159 | def _wait_for(self, f, timeout=10):
|
148 | 160 | """wait for a condition"""
|
|
0 commit comments