将单元测试集成到bash脚本中时的ImportError

2024-04-27 00:18:52 发布

您现在位置:Python中文网/ 问答频道 /正文

假设文件夹“lib/”包含多个单元测试文件(以“__单元测试.py')。我想通过调用名为TESTING的bash脚本对上述文件夹中的所有测试文件进行单元测试。你知道吗

michael$ python -m unittest discover -s ./lib/ -p '*__unittest.py'
...................
----------------------------------------------------------------------
Ran 19 tests in 0.001s

OK

michael$ bash <(python -m unittest discover -s ./lib/ -p '*__unittest.py')
...................
----------------------------------------------------------------------
Ran 19 tests in 0.002s

OK

michael$ echo "python -m unittest discover -s ./lib/ -p '*__unittest.py'" > TESTING

michael$ bash <(cat TESTING)
EEE
======================================================================
ERROR: CheckingOps__unittest (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: CheckingOps__unittest
Traceback (most recent call last):
...
ImportError: No module named 'Bio'

似乎我不能通过调用bash脚本来执行单元测试,因为Python不会导入库Bio。为什么会这样?我如何解决这个问题?你知道吗

编辑1:直接调用脚本时的结果(当然)相同。你知道吗

michael$ bash TESTING
EEE
======================================================================
ERROR: CheckingOps__unittest (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: CheckingOps__unittest
Traceback (most recent call last):
...
ImportError: No module named 'Bio'

Tags: 文件py脚本文件夹bashlib单元测试unittest