I'm having a tough time understanding packages, and specifically how to use unittest with packages. I looked at this question () but the correct answer to that question didn't solve my problem. I have the following structure:
model
|-- __init__.py
|-- boardmodel.py
|
|-- exceptions
| |
| |-- __init__.py
| |-- exceptions.py
|
|-- test
|-- __init__.py
|-- test_boardmodel.py
With the following files/imports:
model/__init__.py:
import model.exceptions.exceptions
import model.boardmodel
model/exceptions/__init__.py:
contains nothing
model/test/__init__.py:
contains nothing
imports inside boardmodel.py::
from model.exceptions.exceptions import ZeroError, OverlapError, ArgumentError, ProximityError
imports inside test_boardmodel.py:
import unittest
from model.boardmodel import Board, Ball, Wall
from model.exceptions.exceptions import ProximityError
I place myself in the model-directory and I run python -m unittest test.test_boardmodel.py
. I get the following message:
ERROR: test_boardmodel (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: test_boardmodel
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 153, in loadTestsFromName
module = __import__(module_name)
File "/Users/sahandzarrinkoub/Documents/Programming/pythonfun/BouncingBalls/balls/src/model/test/test_boardmodel.py", line 3, in <module>
from model.boardmodel import Board, Ball, Wall
ModuleNotFoundError: No module named 'model'
I'm a bit lost with how the imports work and what location the modules/packages are looked for when an import statement is executed. Why isn't model
found?
I shall add that if I remove model.
from all the imports listed, the tests work, but I can't use the package from "outside" anymore:
src
|-- visual.py
|
|-- model
|-- __init__.py
|-- boardmodel.py
|
|-- exceptions
| |
| |-- __init__.py
| |-- exceptions.py
|
|-- test
|-- __init__.py
|-- test_boardmodel.py
inside visual.py:
import model
from model.boardmodel import Board
Which is better – pytest or unittest? Although both the frameworks are great for performing testing in python, pytest is easier to work with. The code in pytest is simple, compact, and efficient. For unittest, we will have to import modules, create a class and define the testing functions within that class.
unittest has been built into the Python standard library since version 2.1. You'll probably see it in commercial Python applications and open-source projects. unittest contains both a testing framework and a test runner. unittest has some important requirements for writing and executing tests.
Unit testing is a method for testing software that looks at the smallest testable pieces of code, called units, which are tested for correct operation. By doing unit testing, we can verify that each part of the code, including helper functions that may not be exposed to the user, works correctly and as intended.
I was facing the same issue, being able to import some modules from several files, but not from a test file, so I saw this solution:
If you have test/my_test.py, tests should be run as:
python -m test.my_test
After that, I imported what I wanted and got no errors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With