diff --git a/tests/test_core.py b/tests/test_core.py
index dd69a96..c666a3a 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -389,13 +389,23 @@ def test_unknown_preferredencoding_warned_and_fallback():
"""Ensure a locale without a codec emits a warning."""
@as_subprocess
def child():
+ warnings.filterwarnings("error", category=UserWarning)
+ match = ('LookupError: unknown encoding: ---unknown--encoding---, '
+ 'defaulting to UTF-8 for keyboard.')
with mock.patch('locale.getpreferredencoding') as get_enc:
get_enc.return_value = '---unknown--encoding---'
- with pytest.warns(UserWarning, match=(
- 'LookupError: unknown encoding: ---unknown--encoding---, '
- 'defaulting to UTF-8 for keyboard.')):
- t = TestTerminal()
- assert t._encoding == 'UTF-8'
+ try:
+ TestTerminal()
+ except UserWarning as e:
+ assert match in str(e)
+ else:
+ raise AssertionError('Did not raise warning')
+
+ warnings.filterwarnings("ignore", category=UserWarning)
+ t = TestTerminal()
+ assert t._encoding == 'UTF-8'
+
+ warnings.resetwarnings()
child()