I'm writing foobaz python app which uses plugins (in rules/plugins/
directory) and tests to these plugins (in rules/tests/
) from project foobar . I do not control these (both plugins and their tests).
Because of import statements in the tests, I can not figure out how to run these. Test looks like this:
$ cat rules/tests/test_something.py
import unittest
from foobar.rules.plugins import something
class TestSomething(unittest.TestCase):
def test_something_equals(self):
self.assertEqual(1, something.equals(1))
Plugin itself can look like this:
$ cat rules/plugins/something.py
def equals(i):
return i
And script I'm using to run all the tests look like this:
$ cat tests.py
#!/usr/bin/env python
import sys
import unittest
sys.path.append('rules/tests/')
from test_something import TestSomething
if __name__ == '__main__':
unittest.main()
When I want to execute the tests, I (no surprise) get:
$ ./tests.py
Traceback (most recent call last):
File "./tests.py", line 8, in <module>
from test_something import TestSomething
File "rules/tests/test_something.py", line 6, in <module>
from foobar.rules.plugins import something
ImportError: No module named foobar.rules.plugins
Is there some way I can get this running given I can not edit plugin or test?
If I could edit test, I would do:
- from foobar.rules.plugins import something
+ import sys
+ sys.path.append('rules/plugins/')
+ import something
and it would work (tested).
OK, after a discussion with a colleagues, there is a really simple way how to resolve this:
$ mkdir foobar
$ mv rules/ foobar/
$ touch foobar/__init__.py
$ touch foobar/rules/__init__.py
$ touch foobar/rules/plugins/__init__.py
This way test will be able to find what it needs. I did not realized this obvious thing before.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.