The unittest file imports the module from its root directory and runs the file directly without any problems; however, when running the test in its upper directory using discovery mode, a ModuleNotFoundError occurs.
Of course the easiest solution would be to move that module to the root directory where the tests are run, but because I often need to run the test software directly, so this is not OK.
It is possible to create a copy, but it violates the DRY rules and is prone to various naming conflicts, which is not an elegant solution.
Is there any other way to solve this problem?
├──TheTest_0
│ ├──__init__.py
│ ├──meta_test.py
│ ├──mydict.py
├──runTest.ps1
python -m unittest discover --start-directory .\ --pattern "meta_test.py"
from mydict import Dict
ModuleNotFoundError: No module named 'mydict'
Add these two lines of code at the top of the test file to solve the problem.
import sys,os
sys.path.append(os.path.dirname(__file__))
Thanks @drum!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.