I have a problem in providing the correct path to mocker patcher. I have looked at couple stack threads but to no avail.
Project structure:
my_app
| src
| a
| mod.py
| tests
| test_mod.py
| b
| mod.py
| tests
| test_mod.py
Let's say that file my_app/src/a/mod.py contains:
from abc import x
def foo():
result = x.y()
return result
In test file I import mod.py via relative path. I also want to patch abc.x.y() function (imported in mod.py).
import pytest
from .. import mod
from ..mod import foo
def test_foo(mocker):
mocker.patch("mod.x.y", return_value=None)
assert foo() is None
The problem is that pytest does not find the "mod" module.
I have also tried using:
mocker.patch.object(mod, "x.y", return_value=None)
and while "mod" is found, "x.y" is not part of "mod".
How can I patch that function while staying contained inside "a" package (I do not want to provide a path through the whole project)?
Related
I want to import constants from a constants module from two different modules, but I get the following error:
Traceback (most recent call last):
File "C:\Temp\tmp\pycircular\pycircular\pycircular.py", line 2, in <module>
from my_classes.foo import Foo
File "C:\Temp\tmp\pycircular\pycircular\my_classes\foo.py", line 1, in <module>
from pycircular.constants import ANOTHER_CONSTANT
File "C:\Temp\tmp\pycircular\pycircular\pycircular.py", line 2, in <module>
from my_classes.foo import Foo
ImportError: cannot import name 'Foo' from partially initialized module 'my_classes.foo' (most likely due to a circular import) (C:\Temp\tmp\pycircular\pycircular\my_classes\foo.py)
My project structure is the following:
|-constants.py
|-my_classes
| |-foo.py
| |-__init__.py
|-pycircular.py
|-__init__.py
# =============
# pycircular.py
# =============
from constants import SOME_CONSTANT
from my_classes.foo import Foo
def main():
print(SOME_CONSTANT)
my_foo = Foo()
my_foo.do_something()
if __name__ == "__main__":
main()
# =============
# foo.py
# =============
from pycircular.constants import ANOTHER_CONSTANT
class Foo:
def do_something(self):
print(ANOTHER_CONSTANT)
# =============
# constants.py
# =============
ANOTHER_CONSTANT = "ANOTHER"
SOME_CONSTANT = "CONSTANT"
I assume that it is the same problem as solved here https://stackoverflow.com/a/62303448/2021763.
But I really do not get why from my_classes.foo import Foo in pycircular.py is called a second time.
Update:
After renaming the package pycircular to pycircular_pack it worked in PyCharm.
But it only works because in Pycharm the option Add content roots to to PYTHONPATH is automatically set.
The output of sys.path is ['C:\\Temp\\tmp\\pycircular\\pycircular_pack', 'C:\\Temp\\tmp\\pycircular', 'C:\\Tools\\miniconda\\envs\\my_env\\python39.zip', 'C:\\Tools\\miniconda\\envs\\my_env\\DLLs', 'C:\\Tools\\miniconda\\envs\\my_env\\lib', 'C:\\Tools\\miniconda\\envs\\my_env', 'C:\\Tools\\miniconda\\envs\\my_env\\lib\\site-packages']
Without the option the output is ['C:\\Temp\\tmp\\pycircular\\pycircular_pack', 'C:\\Tools\\miniconda\\envs\\my_env\\python39.zip', 'C:\\Tools\\miniconda\\envs\\my_env\\DLLs', 'C:\\Tools\\miniconda\\envs\\my_env\\lib', 'C:\\Tools\\miniconda\\envs\\my_env', 'C:\\Tools\\miniconda\\envs\\my_env\\lib\\site-packages']
And without the option I only get it to work with absolute imports.
# pycircular.py
from constants import SOME_CONSTANT
from my_classes.foo import Foo
...
# foo.py
from constants import ANOTHER_CONSTANT
To elaborate based on the comments and edit:
After renaming the package pycircular to pycircular_pack it worked in PyCharm. But it only works because in Pycharm the option Add content roots to to PYTHONPATH is automatically set.
You should make sure the package directory is not set as a content root or source root. The directory hosting the package directory should be set as source root.
C:\Temp\tmp\pycircular # <- source root
|- pycircular_pack # <- not set as anything
| |- constants.py
| |- my_classes
| | |- foo.py
| | |- __init__.py
| |- pycircular.py
| |- __init__.py
|- other_file.py # <- for illustration's sake
Now your sys.path will be set to include C:\Temp\tmp\pycircular only and there will be exactly one way to import things from your module.
Namely,
other_file.py (outside the package) will be able to use the package as pycircular_pack
pycircular_pack/*.py can refer to modules in the pycircular_pack package by either
(e.g.) from .constants import ... (relative import from current package), or
(e.g.) from pycircular_pack.constants import ... (absolute import)
pycircular_pack/my_classes/*.py can refer to modules in the pycircular_pack package by either
(e.g.) from ..constants import ... (relative import from parent package), or
(e.g.) from pycircular_pack.constants import ... (absolute import)
If your pycircular_pack package would contain a runnable script, e.g. a CLI as pycircular_pack/cli.py, then the correct way to run that script on the command line would be to use python -m pycircular_pack.cli; this has Python set up the path just like we want here, where python pycircular_pack/cli.py would not do the right thing.
I have a file system like this at the moment.
app
|--__init__.py (empty)
|
|--domain
| |--__init__.py (empty)
| |--model.py
| |--questionmatcher.py
|
|--interface
| |--__init__.py (empty)
| |--basiccli.py
| |--userinterface.py
|
|--parser
| |--__init__.py (empty)
| |--json_loader.py
| |--parsing.py
|
|--testfiles
| |--__init__.py (empty)
| |--testsuite.py
I am trying to run the testsuite.py which will need to import classes from various files in directory.
I have tried this structure:
import unittest
from ..parser.json_loader import JsonLoader
from ..parser.parsing import get_vectors, parseThreadsFromFile, getPostsFromThreads
from ..domain import UniversalEncoder, SentBERT
class TestParsing(unittest.TestCase):
def test(self):
pass
class TestJson(unittest.TestCase):
def test(self):
pass
class TestModelEncoders(unittest.TestCase):
def test(self):
pass
if __name__ == "__main__":
unittest.main()
However when I go to run the test I get:
from ..parser.json_loader import JsonLoader
ImportError: attempted relative import with no known parent package
EDIT:
I have tried
from parser.json_loader import JsonLoader
but now I get
from parser.json_loader import JsonLoader
ModuleNotFoundError: No module named 'parser.json_loader'; 'parser' is not a package
you can add this package to your PYTHONPATH environmental variable:
export PYTHONPATH=$PYTHONPATH:/path/to/parser
****solved: added __init__.py to Test/ and renamed testcode.py to test_code.py. To run tests cd -> Zee and type pytest****
Structure:
|--Zee/
| |--Test/
| | |--__init__.py
| | |--test_code.py
| |--Codetotest/
| | |--code.py
in code.py
class Foo():
some code...
in testcode.py
from Codetotest.code import Foo
def test_foo():
assert ...
When I move to the Zee directory in my command line and run pytest Test/testcode.py I get ModuleNotFoundError: No module named Zee. How can I fix this?
I tried making Test a module by adding Test/__init__.py as suggested here. Ran from multiple directories, no dice.
Pytest version 5.3.4, imported from python 3.6
What I don't understand is, when I add __init__.py to Zee/, it gives me the same error
You need a __init__.py in the module directory.
Here's a typical project structure:
|--zee-project-directory/
| |--tests/
| | |--test_zee.py
| |--zee/
| | |--__init__.py
| | |--code.py
code.py
class Foo():
some code...
test_zee.py
from zee.code import Foo
def test_foo():
assert ...
I am relatively new to python, and I am struggling to find a combination of file hierarchy and import statements that will work in pycharm, pytest on the command line, running the actual program on the command line, and building in bamboo.
Here is my hierarchy:
foo
| goo
| __init__.py
| run.py
| koo
| __init__.py
| bar.py
| loo
| __init__.py
| baz.py
| tests
| __init__.py
| test_bar.py
| test_baz.py
| test_file.py
| data
| text.txt
Here is my code:
foo/goo/koo/bar.py:
greeting = "hello"
def hello():
return greeting
foo/goo/loo/baz.py:
from koo import bar
def greet():
return "the greeting is..." + bar.hello()
foo/goo/run.py:
import loo.baz as baz
print(baz.greet())
foo/tests/test_bar.py:
import goo.koo.bar as b
def test_hello():
assert b.hello() == "hello"
foo/tests/test_baz.py:
import goo.loo.baz as b
def test_greet():
assert b.greet() == "the greeting is...hello"
foo/tests/test_file.py:
import os.path
import sys
def test_file():
f = open(os.path.join(sys.path[0], "tests", "data", "test.txt"), "rt")
assert f.read() == "hello world"
When I go to the foo directory and run
python goo/run.py
this works. But when I run
python -m pytest tests
I get the error
Traceback:
tests/test_baz.py:1: in <module>
import goo.loo.baz as b
goo/loo/baz.py:1: in <module>
from koo import bar
E ModuleNotFoundError: No module named 'koo'
If I change baz.py to the following:
from goo.koo import bar
def greet():
return "the greeting is..." + bar.hello()
then all the tests pass, but running the program gives this error:
Traceback (most recent call last):
File "goo/run.py", line 1, in <module>
import loo.baz as baz
File "/home/me/PycharmProjects/foo/goo/loo/baz.py", line 1, in <module>
from goo.koo import bar
ModuleNotFoundError: No module named 'goo'
This question is similar, but does not have a marked answer. One posted answer suggests moving the tests folder down, but this caused an issue on our build server, and also the common practice here seems to be to have the tests at the top level.
Assuming I want to keep the tests at the top level, is there any combination of imports that will work?
I would use a combination of absolute imports e.g. from goo.koo import bar and adding the folder foo to your PYTHONPATH with
export PYTHONPATH=$PYTHONPATH:/path/to/foo
then structuring all your imports as if they are originating from the foo folder
I am at my wits end with trying to get all my unittest to run in Python. I have searched about 30 different posts and the unit test documentation but still cannot figure it out.
First I have two test classes that I can run each individually and all the tests pass:
File: unittest.subfolder1.TestObject1.py
class TestObject1(unittest.TestCase):
def test_case1(self):
...some code...
...some assertions...
if __name__ == '__main__':
unittest.main()
File: unittest.subfolder2.TestObject2.py
class TestObject2(unittest.TestCase):
def test_case1(self):
...some code...
...some assertions...
if __name__ == '__main__':
unittest.main()
Starting in the top level directory above 'unittest' I am trying to us unittest.discover to find and run all my tests:
import unittest
loader = unittest.TestLoader()
suite = loader.discover('unittest')
unittest.TextTestRunner().run(suite)
When I do this I get the error `ModuleNotFoundError: No module named 'subfolder1.TestObject1'
What am I doing wrong?
A good approach is to run all the tests in a subdirectory from the command line. In order to find the following files "TestObject1.py, TestObject2.py, ..." in subdirectories, you can run the following command in the command line:
python -m unittest discover -p 'Test*.py'
Additionally, the __init__.py is required within the import and module directories: Python unittest discovery with subfolders
The import unittest is required in the files unittest.subfolder1.TestObject1.py and unittest.subfolder2.TestObject2.py
It is also possible to define explicitly the directory where the discovery starts with the -s parameter:
python -m unittest discover [options]
-s directory Directory to start discovery ('.' default)
-p pattern Pattern to match test files ('test*.py' default)
In case you are using unittest2, it comes with a script unit2. The command line usage is:
unit2 discover unit2 -v test_module
Do not name your directory unittest, it may conflict with the standard library.
You also need to create a file named __init__.py in all of your directories (subfolder1, etc.), so they become packages and their content can be imported.
So I had to do my own workaround but at least I can get them all to run with the above file structure. It requires that I reinstantiate the TestLoader and the TestSuite each time I give it a new file path, so first I need to collect all relevant file paths in the unittest directory.
import os
import unittest
import traceback
class UnitTestLauncher(object):
def runTests(self):
#logging.INFO("Running unit tests...")
lsPaths = []
#Find all relevant subdirectories that contain unit tests
#Exclude 'unittest' directory, but include subdirectories, with code `path != 'unittest'`
for path,subdirs,files in os.walk('unittest'):
if "pycache" not in path and path != 'unittest':
lsPaths.append(path)
#loop through subdirectories and run individually
for path in lsPaths:
loader = unittest.TestLoader()
suite = unittest.TestSuite()
suite = loader.discover(path)
unittest.TextTestRunner().run(suite)
This solution is not perfect and each different directory comes out as a line of output so you have to look through each line manually for failed tests.
Old question but, oh, so current. I am new to Python, coming from strong typed languages and while the language itself is ok(ish), the conventions, tools and workarounds to make everything work in the ecosystem can drive you nuts. I struggled myself with running unit tests from separate subdirectories and this is the way I solved it.
First, the code you test, package it into a package. Organize your directories like this:
Work
|
+---PkToTest
| |
| +--- __init__.py
| +--- a.py
| +--- <other modules>.py
|
+---Tests (for PKToTest)
|
+--- test_a.py
PkToTest becomes a package due to the init.py file. In test_a.py make sure your sys.path will contain the path to PkToTest (absolute path not relative). I did that by:
import sys
sys.path.insert(0, "<absolute path to parent of PkTotest directory>")
import unittest
from PkToTest import a
class aTestSuite(unittest.TestCase):
def test1(self):
self.assertEqual(a.fnToTest(), ...)
Testing All Subdirectories
Given a structure of:
my_package
|
|
controller
|-- validator.py
|
validator
|-- controller.py
|
test
|-- controller
|
|-- __init__.py (MAKE SURE THIS EXISTS OR unittest MODULE WOULD NOT KNOW)
|-- test_controller.py
|
|-- validator
|
|-- __init__.py (MAKE SURE THIS EXISTS OR unittest MODULE WOULD NOT KNOW)
|-- test_validator.py
|
then just run
python -m unittest discover -s my_package/test
What this does is to test and -s means to start with the my_package/test as the starting directory
In my project all folders are folders (not modules) and they have the structure:
Folder > Subfolder > Subfolder > Tests > test_xxxx.py
Folder > Subfolder > Subfolder > xxxx.py
So i modified the answer from here, and also took a part from How do I run all Python unit tests in a directory? and came up with this:
import os, unittest
testFolderPaths = []
for path, subdirs, files in os.walk(os.getcwd()):
for file in files:
if file.startswith("test_") and file.endswith(".py"):
testFolderPaths.append(path)
for path in testFolderPaths:
print(f"Running tests from {path}...")
loader = unittest.TestLoader()
suite = loader.discover(path)
runner = unittest.TextTestRunner()
result = runner.run(suite)
print(f"RUN {result.testsRun} Tests. PASSED? {result.wasSuccessful()}")
If any tests fail it will throw and error showing which one exactly failed.