Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用unittest在Python中编写测试_Python_Unit Testing_Python Unittest - Fatal编程技术网

如何使用unittest在Python中编写测试

如何使用unittest在Python中编写测试,python,unit-testing,python-unittest,Python,Unit Testing,Python Unittest,我想用TDD方法编写我的库,但我不知道如何设计目录结构(或者如何使用unittest)。现在,我创建一个目录树,如: myproject (directory) - tests (directory) - src (directory) - test.py (file running tests) src中的每个类都有自己的unittest类。每个目录都有自己的\uuuu init\uuuu.py文件。我只想从test.py文件运行测试,因此每个测试都可以从src.mo

我想用TDD方法编写我的库,但我不知道如何设计目录结构(或者如何使用
unittest
)。现在,我创建一个目录树,如:

myproject (directory)
    - tests (directory)
    - src (directory)
    - test.py (file running tests)
src
中的每个类都有自己的
unittest
类。每个目录都有自己的
\uuuu init\uuuu.py
文件。我只想从
test.py
文件运行测试,因此每个测试都可以从src.modulename导入类名
,然后运行test
unittest.main()
函数。不幸的是,它不工作(运行零测试)


这是好办法吗?我的错误是什么?

文件
test.py
中的代码应该如下所示:

from tests import *
import unittest

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

此代码从
tests
目录复制所有测试,因为它复制整个包。main方法运行
tests
package类中包含的所有测试方法每个测试文件名必须以
test

开头。您看过文档测试吗?我一直喜欢它的简单是的,我喜欢。我有很多测试都是用
unittest
编写的,如果可能的话,我宁愿使用它们,而不是在另一个框架上重写。