diff options
| author | Kyle Stewart <4b796c65+bitbucket@gmail.com> | 2016-03-17 19:26:59 -0700 |
|---|---|---|
| committer | Kyle Stewart <4b796c65+bitbucket@gmail.com> | 2016-03-17 19:26:59 -0700 |
| commit | c6ec3b0eb9afb4b988d92b5c503b27c3285229da (patch) | |
| tree | 07d6f5f8419a6ed2499d3dba836ead71aa8fbe51 | |
| parent | d8b754b02daadaacc3a016a812a8d55fdcd83d5e (diff) | |
| download | wheel-git-c6ec3b0eb9afb4b988d92b5c503b27c3285229da.tar.gz | |
Added strict resource testing to all tests.
Not closing files in a test will mark that test as erroneous.
| -rw-r--r-- | wheel/test/conftest.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/wheel/test/conftest.py b/wheel/test/conftest.py new file mode 100644 index 0000000..bda2a98 --- /dev/null +++ b/wheel/test/conftest.py @@ -0,0 +1,38 @@ +"""
+pytest local configuration plug-in
+"""
+
+import gc
+import warnings
+
+import pytest
+
+@pytest.yield_fixture(scope='function', autouse=True)
+def fail_on_ResourceWarning():
+ """This fixture captures ResourceWarning's and raises an assertion error
+ describing the file handles left open.
+
+ Since only Python 3 and PyPy3 have ResourceWarning's, this context will
+ have no effect when running tests on Python 2 or PyPy.
+
+ Because of autouse=True, this function will be automatically enabled for
+ all test_* functions in this module.
+
+ This code is primarily based on the examples found here:
+ https://stackoverflow.com/questions/24717027/convert-python-3-resourcewarnings-into-exception
+ """
+ try:
+ ResourceWarning
+ except NameError:
+ # Python 2, PyPy
+ yield
+ return
+ # Python 3, PyPy3
+ with warnings.catch_warnings(record=True) as caught:
+ warnings.resetwarnings() # clear all filters
+ warnings.simplefilter('ignore') # ignore all
+ warnings.simplefilter('always', ResourceWarning) # add filter
+ yield # run tests in this context
+ gc.collect() # run garbage collection (for pypy3)
+ # display the problematic filenames if any warnings were caught
+ assert not caught, '\n'.join((str(warning.message) for warning in caught))
|
