Python 3.7单元测试

问题描述 投票:-1回答:1

所以我尝试了很多事情(从SO and起),但仍未成功,这是我当前的代码:

test.py,我称之为运行测试:python3 ./src/preprocess/python/test.py导入unittest

if __name__ == '__main__':
    testsuite = unittest.TestLoader().discover('.')
    unittest.TextTestRunner(verbosity=2).run(testsuite)

测试文件如下所示:

import unittest
from scrapes.pdf import full_path_to_destination_txt_file

print(full_path_to_destination_txt_file)

class PreprocessingTest(unittest.TestCase):

    def path_txt_appending(self):
        self.assertEqual(full_path_to_destination_txt_file(
            "test", "/usr/test"), "/usr/test/test.txt")


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

但是输出始终是这样的:

python3 ./src/preprocess/python/test.py

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

附加信息:

  • 如您所见,我不是从我的根目录调用此名称。测试文件夹位于./src/preprocess/python/test/中,并包含__init__.py文件(在test.py级别上还有一个初始化文件)
  • 如果我必须记下我只想完成的所有测试的所有调用,这对我来说是可以的,>
  • 使用-t自动搜索也不起作用,所以我认为使用test.py的更可靠的方法可以使用...
  • 使用此框架是我必须遵循的要求
  • test_preprocessing.py在测试文件夹中,from scrapes.pdf import full_path_to_destination_txt_file scrapes是与测试处于同一级别的模块文件夹
  • 当我直接在命令行中调用单个单元测试时,由于相对导入而失败。但是,使用test.py(显然)可以找到模块。
  • 怎么了?

[因此,我尝试了很多事情(从SO或更多工具中)来运行测试,但没有任何效果,这是我当前的代码:test.py,我调用它来运行测试:python3 ./src/preprocess/python/test.py。 ..

python-3.x python-unittest subdirectory relative-import
1个回答
1
投票

默认情况下,unittest将仅执行名称以test开头的方法:

© www.soinside.com 2019 - 2024. All rights reserved.