Closed khanna7 closed 2 years ago
You specify individual tests to run with python -m unittest A.B.C.D
where
Also, you should put your tests in a test
directory rather than src
.
├── README.md ├── src │ ├── init.py │ ├── pycache │ ├── cadre_model.py │ ├── cadre_person.py │ ├── main.py └── test ├── init.py ├── pycache └── test_person.py
Thanks Nick! My file structure is above. When I call the test as you suggested, I get an error that says:
module = __import__(module_name)
File "/Volumes/GoogleDrive/My Drive/code/cadre/python/test/test_person.py", line 4, in <module>
from src import cadre_model
File "/Volumes/GoogleDrive/My Drive/code/cadre/python/src/cadre_model.py", line 8, in <module>
import cadre_person
ModuleNotFoundError: No module named 'cadre_person
I suspect it is because I am not correctly importing the module files in the test module? I have
from src import cadre_model
from src import cadre_person
Am I doing this wrong?
The command I am calling is
python -m unittest test.test_person.TestPerson.test_age_assignment
from root.
Just noticed your project structure is a bit off. The src directory should be your package name. Something like cadre
or whatever you want to call it. So, the imports would be from cadre import cadre_model
etc.
To make the tests work, you may need to add something like:
try:
from cadre import cadre_model
except ModuleNotFoundError:
sys.path.append("{}/...".format(os.path.dirname(os.path.abspath(__file__))))
from cadre import cadre_model
But I don't think that should be necessary if you are running the tests from the parent directory of the tests directory.
Oh, you might need a init.py in the tests directory.
Thanks Nick! I do have an _init_
in the test directory, though I have some stuff that was automatically generated as well. Is the generated stuff possibly causing issues? And I should rename src
to cadre
? And then in the test_person.py
file add from cadre import cadre_model
and from cadre import cadre_person
?
├── README.md
├── src
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── Person.cpython-39.pyc
│ │ ├── __init__.cpython-310.pyc
│ │ ├── cadre_model.cpython-310.pyc
│ │ ├── cadre_model.cpython-39.pyc
│ │ ├── cadre_person.cpython-310.pyc
│ │ ├── cadre_person.cpython-39.pyc
│ │ └── test_person.cpython-310.pyc
│ ├── cadre_model.py
│ ├── cadre_person.py
│ ├── main.py
│ └── obsolete
│ ├── model-class.py
│ └── simple-alc-use-transitions-oop.py
└── test
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-310.pyc
│ └── test_person.cpython-310.pyc
└── test_person.py
I think I got this to work. The error was that once my code is organized into modules, I needed to update the import statement headers in the other files as well. That's it - it works now. thanks @ncollier
https://github.com/khanna7/cadre/blob/cc7570bec82ed47206ef06abbcbb73073382878e/python/src/test_person.py#L55
Currently if I call run the
test_person.py
file, each test is run sequentially. So, if I am looking only to run the newest test, I am having to wait until all the above tests are first completed. Is there any way to run only the test I want to run?