Python 3.x Python 3.7单元测试

Python 3.x Python 3.7单元测试,python-3.x,python-unittest,subdirectory,relative-import,Python 3.x,Python Unittest,Subdirectory,Relative Import,因此,我尝试了很多方法(从更多方面)来运行测试,但都没有效果这是我当前的代码: test.py我调用它来运行测试:python3./src/preprocess/python/test.py 导入单元测试 if __name__ == '__main__': testsuite = unittest.TestLoader().discover('.') unittest.TextTestRunner(verbosity=2).run(testsuite) 测试文件如下所示: i

因此,我尝试了很多方法(从更多方面)来运行测试,但都没有效果这是我当前的代码:

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
但输出总是这样:

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/
    中,包含一个
    \uuu init\uuu.py
    文件(test.py级别还有一个init文件)
  • 如果我必须把所有测试的所有调用都写下来,那对我来说没问题,我只是想完成这个
  • 使用-t进行自动搜索也不起作用,因此我认为使用test.py的更健壮的方法会起作用
  • 使用这个框架是我必须遵循的要求
  • test_preprocessing.py位于test文件夹中,并且
    from scrapes.pdf import full_path_to_destination_txt_file
    scrapes是与test相同级别的模块文件夹
  • 当我在命令行中直接调用单单元测试时,由于相对导入,它失败了。但是使用test.py(显然)可以找到模块

怎么了?

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

testMethodPrefix

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

更改该属性,或者(最好)在方法名称前面加上
test\uu