使用TestLoader的Python unittest导致ModuleNotFoundError

时间:2017-08-29 08:33:29

标签: python python-3.x python-unittest

我尝试使用unittest.TestLoaderunittest.TextTestRunner运行单位测试,但每次尝试运行“{1}}时都会获得ModuleNotFoundError测试文件(此处:test_all.py)。我有以下文件结构:

src.
    test_all.py
    dir1.
        __init__.py
        module1.py
        submodule1.py
        test_module1.py
    dir2.
        __init__.py
        module2.py
        submodule2.py
        test_module2.py
    dir3.
        ...

test_all.py文件如下所示:

# test_all.py
import os
import unittest

loader = unittest.TestLoader()
suite = loader.discover(os.getcwd())
runner = unittest.TextTestRunner()

runner.run(suite)

最后,单个测试用例的结构如下所示:

# test_module1.py
import unittest
from module1 import Module1

class Module1TestCase(unittest.TestCase):

    def setUp(self):
        # do something

    def test_something(self):
        # test test

    def tearDown(self):
        # do something

if __name__ == '__main__':
    unittest.main()

因此,运行test_all.py始终会导致ModuleNotFoundError引用TestCase from module1 import Module1内的test_module1.py(以及以下TestCases中的相同内容)。据我所知,没有循环依赖。也许添加当前路径到PythonPath会起作用,但它对我来说没有意义:一方面我在当前目录中运行test_all.py作为主要,另一方面unittest.TestLoader.discover()已经采用[Aug 29 08:14:42] DEBUG[8683] cel_pgsql.c: Inserting a CEL record: [INSERT INTO cel ("id","eventtype","eventtime","userdeftype","cid_name","cid_num","cid_ani","cid_rdnis","cid_dnid","exten","context","channame","appname","appdata","amaflags","accountcode","peeraccount","uniqueid","linkedid","userfield","peer","id","eventtype","eventtime","userdeftype","cid_name","cid_num","cid_ani","cid_rdnis","cid_dnid","exten","context","channame","appname","appdata","amaflags","accountcode","peeraccount","uniqueid","linkedid","userfield","peer") VALUES (DEFAULT,'CHAN_END','2017-08-29 08:14:42.167195','','9004','9004','9004','','9001','9001','public','SIP/9004-00000008','','',3,'','','1503994474.12','1503994474.12','','',DEFAULT,'CHAN_END','2017-08-29 08:14:42.167195','','9004','9004','9004','','9001','9001','public','SIP/9004-00000008','','',3,'','','1503994474.12','1503994474.12','','')]. alp-test*CLI> core show version Asterisk 14.4.1 built by buildozer @ build-3-6-x86_64 on a x86_64 running Linux on 2017-05-22 06:13:12 UTC 目前的道路。

PS:我知道将所有TestCase放在一个文件夹中会更好。但首先我要弄清楚为什么这不起作用。谢谢!

0 个答案:

没有答案