Skip to content

Commit

Permalink
Merge pull request #111 from kernc/prevent-unittest.assertWarns-error
Browse files Browse the repository at this point in the history
Fix unittest.TestCase.assertWarns error
  • Loading branch information
RonnyPfannschmidt committed Feb 23, 2017
2 parents 1f16f10 + 4030b09 commit 6fbe39c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

- fix issue106: Naive unicode encoding when calling fspath() in python2. Thanks Tiago Nobrega for the PR.

- fix issue110: unittest.TestCase.assertWarns fails with py imported.

1.4.32
====================================================================

Expand Down
2 changes: 2 additions & 0 deletions py/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# so that py.error.* instances are picklable
import sys
sys.modules['py.error'] = _apipkg.AliasModule("py.error", "py._error", 'error')
import py.error # "Dereference" it now just to be safe (issue110)


_apipkg.initpkg(__name__, attr={'_apipkg': _apipkg}, exportdefs={
# access to all standard lib modules
Expand Down
22 changes: 22 additions & 0 deletions testing/root/test_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,25 @@ def test_error_conversion_ENOTDIR(testdir):
def test_checked_call_supports_kwargs(tmpdir):
import tempfile
py.error.checked_call(tempfile.mkdtemp, dir=str(tmpdir))


try:
import unittest
unittest.TestCase.assertWarns
except (ImportError, AttributeError):
pass # required interface not available
else:
import sys
import warnings

class Case(unittest.TestCase):
def test_assertWarns(self):
# Clear everything "py.*" from sys.modules and re-import py
# as a fresh start
for mod in tuple(sys.modules.keys()):
if mod and (mod == 'py' or mod.startswith('py.')):
del sys.modules[mod]
import py

with self.assertWarns(UserWarning):
warnings.warn('this should work')

0 comments on commit 6fbe39c

Please sign in to comment.