【问题标题】:Python 3.7 Unit TestsPython 3.7 单元测试
【发布时间】:2020-05-03 12:39:24
【问题描述】:

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

test.py 我调用它来运行测试:python3 ./src/preprocess/python/test.py 导入单元测试

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

怎么了?

【问题讨论】:

    标签: python-3.x python-unittest subdirectory relative-import


    【解决方案1】:

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

    testMethodPrefix

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

    来自docs.

    要么更改该属性,要么(最好)在方法名称前加上 test_

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-05
      • 1970-01-01
      • 2013-09-27
      • 2011-12-05
      • 1970-01-01
      • 2019-05-11
      相关资源
      最近更新 更多