I have a Python package organized roughly as such:
pkg
--subpkg1
--foo.py
--bar.py
--subpkg2
--baz.py
--buz.py
When I run unittest discover passing in pkg as the start location, it imports the tests I have put in those packages, but throws import errors saying things like "failed to import pkg.subpkg2..idea" or "failed to import pkg.subpkg2." Is there anyway to run discover from a root without having it fail when it discovers folders that are not Python modules? Would be nice if I didn't have to specify each individual test.
Related
I am trying to import a file (module) that contains test data into my python tests. I am keeping the data separate as it is a large python dictionary and the tests will be mmore readable with them in a separate place.
My test folder:
source/
tests/
elasticsearch_tests/
fixture_data.py
test_search.py
__init__.py
I run the tests with:
PYTHONPATH=./source python -m unittest discover -s tests
In test_search.py I attempt to import the data from fixture_data.py with:
from fixture_data import EVENT_DATA
It looks like fixture_data is not available on the path as it gives this error:
ModuleNotFoundError: No module named 'fixture_data'
I can explicitly add the test folder to the python path and then the data is found and tests work with:
PYTHONPATH=./source:./tests/elasticsearch_tests python -m unittest discover -s tests
Is there a better way though - in which the tests will automatically pickup the test data module?
I'm creating my first package and I can not figure out how to create a simple test.py file to debug my code.
Consider the following file structure.
|--MyPackage
|--test.py
|--PackageName
|--__init__.py
|--module.py
Inside test.py I have: from .src.module import SomeClass
And of course this gives me the dreaded "attempted relative import with no known parent package" message.
I know I could just install the package and then import it with from PackageName.module import SomeClass but then I'm using the code installed on my system and not the code that I am actively editing.
There has got to be some kind of standard way for testing and debugging a package right? Despite all the searching I've done, I can't seem to find any kind of solution.
I'm running test.py with python3 test.py
If it's helpful, here is a screenshot of my actual project folder structure:
I've got a project running on a server with the structure
proj
__init__.py
module_a.py
module_b.py
main.py
And in the header of main.py, I import from other modules with the format
from .module_a import func1
from .module_b import func2
This runs fine on the server, but when I'm testing things on my local machine it raises the error:
ModuleNotFoundError: No module named '__main__.module_a'; '__main__' is not a package
There have been a lot of questions asked regarding this error and the accepted solution is almost always to replace the import statement with
from proj.module_a import func1
Is there something I can do to configure my local environment to allow this type of syntax without having a completely different set of import statements depending on whether the code is running locally or remotely?
Keep your imports relative, without using the package full path, so that you have the flexibility of renaming it as you wish, like in
from .module_a import func1
Then in your local environment, change your current dir to the proj parent folder and run:
python -m proj.main
An alternative would be to rename main.py to __main__.py and then just writing
python -m proj
will do. But that may affect the behaviour on the server if you copy the files as is.
Packages are usually to be imported. This is a common problem when we start running from arbitrary scripts located inside the package (in this case main.py). If the package is simply imported from outside, everything works.
I am working on developing unittests for a project that has been already completed, however I am having a hard time running my unittests without modifying the original code. The module I am trying to test has other dependencies in the same folder that will not import when the unittests are run. Here is what my directory looks like:
root
|--main_folder
|--module1.py
|--module2.py
|--tests
|--test_module1.py
The original code in module1.py successfully imports module2.py on its own like this: from module2 import Practices where Practices is a function from module2.
The issue I am running into is that in order to run test_module1.py (which I am doing by calling python3 -m unittest from the root directory), I have to modify module1.py itself such that it says: from main_folder.module2 import Practices.
If I run the test file without modifying module1.py, I get the error ModuleNotFoundError: No module named 'module2'.
Ideally I cannot modify the code in this way, and I am trying to find a way to make my tests work without touching the application itself. How should I go about this? module1.py runs normally when I run the application without modifying the file, however modifying it so that the tests work breaks the main application. What can I do to make my tests independent of the code for the main app?
(For some more background, the test_module1.py file works by calling from main_folder.module1 import fun1 where fun1 is the function I am trying to test)
Try running your tests using one of the following commands (replacting the actual paths):
if your tests import the modules "from main_folder import ..."
env PYTHONPATH=/root python3 -m unittest
or if your tests import directly "import module1":
env PYTHONPATH=/root/main_folder python3 -m unittest
As a side note, you might need to have existing
main_folder/__init__.py
file, to get the main_folder recognized as package, depending of the python version you're using. If you currently don't have such file, try creating it (empty, no need to put code inside it) and check if the issue persists.
I have the following directory structure:
src
\conftest.py
\dir_A
\run_A.py
\test_run_A.py
\dir_B
\run_B.py
where run_A.py has the following code:
from dir_B import run_B
...
When I run pytest from src, I get the error:
ImportError while importing test module '/home/user/src/dir_A/run_A.py'
...
ImportError: cannot import name 'run_B'
Is there a reason why this fails despite using conftest.py?
I'd like to add that running python3 -m dir_A.test_run_A from src works perfectly fine as a test.
You'll find this link helpful: https://docs.pytest.org/en/latest/pythonpath.html
Essentially, when running these tests from the src directory, pytest will walk all the way down to /home/user/src/dir_A/run_A.py. Without any __init__.py files, it will then refuse to walk anywhere else, as it doesn't realize it is part of a larger package.