-1

所以我尝试了很多东西(来自SO 更多)让我的测试运行但没有任何效果这是我当前的代码:

test.py我调用它来运行测试:python3 ./src/preprocess/python/test.py import 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

附加信息:

  • 如您所见,我不是从我的根目录中调用它。test 文件夹在里面./src/preprocess/python/test/,里面有一个__init__.py文件(在 test.py 级别上还有一个 init 文件)
  • 如果我必须编写所有测试的所有调用,我只想完成这个,这对我来说没问题
  • 使用 -t 的自动搜索也不起作用,所以我认为使用 test.py 的更强大的方法会起作用......
  • 使用这个框架是我必须遵循的要求
  • test_preprocessing.py 位于 test 文件夹中,from scrapes.pdf import full_path_to_destination_txt_filescrapes 是与 test 处于同一级别的模块文件夹
  • 当我直接在命令行中调用单个单元测试时,由于相对导入而失败。但是使用 test.py (显然)可以找到模块。

怎么了?

4

1 回答 1

2

默认情况下,unittest 只会执行名称以 开头的方法test

测试方法前缀

给出方法名称前缀的字符串,将被解释为测试方法。默认值为“测试”。这会影响 getTestCaseNames() 和所有 loadTestsFrom*() 方法。

文档。

更改该属性或(最好)在您的方法名称前加上test_.

于 2020-01-16T16:11:53.173 回答