-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pkgutil.get_data support #808
Comments
Would care to open a PR, or perhaps give a use case to test this with? |
Well our use case is the test for an import function with a "big" import (to ensure nothing blows up and get perf info), there's ~50k of test data so it's not really convenient to have it inlined in the test module. The test data lives in a json file next to the test module instead, and is opened via get_data() to try and avoid overly depending on living in an actual filesystem. |
I could not reproduce the problem... here's what I tried: Created a simple text file
# contents of foo.py
def foo():
return 1 # contents of test_foo.py
import pkgutil
def test_foo():
assert pkgutil.get_data('foo', 'foo_data.txt') == 'Hey' This test passes for me, both for
If you could provide a minimum example with the problem, I would be happy to work on a fix. |
Yeah the problem is the test module being used as base to get the test data (otherwise Create a # contents of test_foo.py
import pkgutil
import unittest
class TestFoo(unittest.TestCase):
def test_foo(self):
assert pkgutil.get_data(self.__module__, 'data.txt') == 'Hey\n' then from outside
If I monkeypatch the AssertionRewritingHook with a (crappy) from _pytest.assertion.rewrite import AssertionRewritingHook
AssertionRewritingHook.get_data = lambda self, pathname: open(pathname, 'rb').read() then the test passes:
|
Thanks, I will give it a try. |
pkgutil.get_data
currently only returnsNone
when called from a module py.test matches, which is inconvenient when trying to load test data from a file in a test package.This happens because
AssertionRewritingHook
sets itself as the module's loader, but it does not implement the (optional) PEP302get_data
loader API. As a result,pkgutil.get_data
simply returnsNone
when invoked on a test module while running py.test.It would be nice if either
AssertionRewritingHook
didn't set itself as the module's loader, though I don't know what the side-effects would be if it setpkgutil.ImpLoader
as the loaderor
AssertionRewritingHook
implementedget_data
, for which the trivial implementation seems like it should work given the rewriting hook correctly sets__file__
on the loaded module:The text was updated successfully, but these errors were encountered: