Unittest library keeps having problems importing module upon running tests - python

I have the following folder structure:
The content of my files are as follows:
library.py:
def hello():
return "Hello World"
main.py:
from library import hello
def main_func():
return hello()
test_main.py:
import unittest
from src.main import main_func
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual(main_func(), "Hello World!")
if __name__ == "__main__":
unittest.main()
Now, when I run my unittests using the following command:
python3 -m unittest discover test
I get the following error:
ImportError: Failed to import test module: test_main
Traceback (most recent call last):
File "/Users/user/.pyenv/versions/3.8.11/lib/python3.8/unittest/loader.py", line 436, in _find_test_path
module = self._get_module_from_name(name)
File "/Users/user/.pyenv/versions/3.8.11/lib/python3.8/unittest/loader.py", line 377, in _get_module_from_name
__import__(name)
File "/Users/user/Desktop/example/test/test_main.py", line 3, in <module>
from src.main import main_func
File "/Users/user/Desktop/example/src/main.py", line 1, in <module>
from library import hello
ModuleNotFoundError: No module named 'library'
Can someone please explain how I can get this to work? I am deploying functions to AWS lambda so need the import structure as it is otherwise it won't work. Is there a way to configure unittests to work with this import structure?

Related

Importing modules in embedded python

I'm trying to get module imports to work in embeddable python, but it doesn't want to work
C:\Users\test\Desktop\winpy\python-3.10.10-embed-win32>type run_scripts\script.py
from module_test import test
print("Hello world!")
print(test())
C:\Users\test\Desktop\winpy\python-3.10.10-embed-win32>type run_scripts\module_test.py
def test():
return "Test!"
C:\Users\test\Desktop\winpy\python-3.10.10-embed-win32>#python.exe run_scripts\script.py
Traceback (most recent call last):
File "C:\Users\test\Desktop\winpy\python-3.10.10-embed-win32\run_scripts\script.py", line 1, in <module>
from module_test import test
ModuleNotFoundError: No module named 'module_test'
Why is the module not being imported? I tried changing PYTHONPATH but it didn't help

Can't find an import statement that works with both unittest and running the module standalone

I'm figuring out python and trying to work through some weirdness with import statements and unit tests. I don't think this problem is anything too profound, just not clear from a beginners stand point.
The jist of the problem :
If I have a certain import statement in a dev file, I can run tests.
If I have a different import statement in the same dev file, I can run the file in python via cmdline to check syntax quick checks ("python.exe foo.py" for example).
BUT - I can't seem to find a way to do both, at least without consistently modifying the dev file.
My directory structure :
Simple/
dev/
__init__.py
foo.py
bar.py
test/
__init__.py
test1.py
The files are all pretty basic :
Bar.py :
## bar.py
def barfunc(arg1, arg2):
return arg1 + arg2
Foo.py :
# foo.py
from dev import bar
def foofunc(arg):
return bar.barfunc(arg, arg)
print(foofunc(2))
my_test.py :
# my_test.py
import unittest
from dev import foo
class Testing(unittest.TestCase):
def test_mytest(self):
self.assertEqual(foo.foofunc(3), 6)
if __name__ == '__main__':
unittest.main()
So in the 'foo.py' file, If I have import statement 'from dev import foo' then I can run tests. Either through VSCODE test runner, or by console :
>> py -m unittest .\test\my_test.py
But if I try to run the 'foo.py' file, either by 'run' in vscode, or in cmd line "py .\dev\foo.py', I get this error :
>> py .\dev\foo.py
Traceback (most recent call last):
File "E:\Python\Simple\dev\foo.py", line 2, in <module>
from dev import bar
ModuleNotFoundError: No module named 'dev'
Conversely, if I change the import statement in foo.py from "from dev import bar" to just "import bar", then I can run the module just fine. But I can't run tests - it won't even load the module. Receiving this error :
> ~\AppData\Local\Programs\Python\Python310\python.exe ~\.vscode\extensions\ms-python.python-2022.14.0\pythonFiles\testing_tools\unittest_discovery.py ./test *_test.py
cwd: .
[ERROR 2022-8-21 12:10:38.65]: Error discovering unittest tests:
Failed to import test module: my_test
Traceback (most recent call last):
File
"C:\Users\b\AppData\Local\Programs\Python\Python310\lib\unittest\loader.py", line 436, in _find_test_path
module = self._get_module_from_name(name)
File "C:\Users\b\AppData\Local\Programs\Python\Python310\lib\unittest\loader.py", line 377, in _get_module_from_name
__import__(name)
File "e:\Python\Simple\test\my_test.py", line 3, in <module>
from dev import foo
File "e:\Python\Simple\dev\foo.py", line 2, in <module>
import bar
ModuleNotFoundError: No module named 'bar'
Is there some way to have it both ways? And why should the test module be affected by the import statement in dev code?

unittesting fails on ModuleNotFound in the file to be tested imports

I am writing a small unittest for my method move(). The application is running but when I do python -m unittest app.test.test_move_items, it fails with the following error:
ImportError: Failed to import test module: test_move
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/loader.py", line 154, in loadTestsFromName
module = __import__(module_name)
File "/Users/daniel/app/test/test_move.py", line 13, in <module>
from app.move_items import move
File "/Users/daniel/apps/app/move.py", line 29, in <module>
from helpers.calculator import determine_move_amount
ModuleNotFoundError: No module named 'helpers'
If i do python app/move.py, the output is correct.
The folder structure is as follows:
app
helpers
|-calculator.py
move_items.py
test
|-test_move_items.py
move_items.py
from helpers.calculator import determine_move_amount
def move(list_of_items, move_by):
pass
There is init.py file in app and the test directory as well.

Relative import error using python unittest

I'm using the built-in python unittest module. My directory structure is as follows:
game.py
test/test_game.py
The content of test_game.py:
import unittest
from ..game import *
class TestGame(unittest.TestCase):
def test_pawn(self):
game = Game()
game.make_move("e2e3")
self.assertEqual(game.board[6][5].piece_type, "P")
if __name__ == '__main__':
unittest.main()
Here's the problem. When I run python -m unittest test/test_game.py I get the error:
ImportError: Failed to import test module: test_game
Traceback (most recent call last):
File "/usr/local/Cellar/python#3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/loader.py", line 154, in loadTestsFromName
module = __import__(module_name)
ModuleNotFoundError: No module named 'test.test_game'
I was missing the test/__init__.py file. With this change, I didn't even need the relative ..game import.. But I'm wondering why python requires this just to import

no module named "3rd party library" in subfolder

My folder structure is as follows.
main
main.py
library
utils.py
utils.py has the following code
from aws_requests_auth.aws_auth import AWSRequestsAuth
def testAuth():
print('some code code')
main.py has the following code
from library.utils import testAuth
def start():
print ('some code')
I have run pip install aws_requests_auth in both my main and library folder.
When i tried to run python3 main.py I got this error
Traceback (most recent call last):
File "main.py", line 1, in <module>
from library.utils import testAuth
File "/Users/Docs/library/utils.py", line 1, in <module>
from aws_requests_auth.aws_auth import AWSRequestsAuth
ModuleNotFoundError: No module named 'aws_requests_auth'
How do i ensure 3rd party libraries are installed properly for sub folders? Thanks!

Categories