[英]Is it possible to run all unit test?
I have two module with two different classes and their corresponding test classes. 我有两个模块,有两个不同的类及其相应的测试类。
foo.py
------
class foo(object):
def fooMethod(self):
// smthg
bar.py
------
class bar(object):
def barMethod(self):
// smthg
fooTest.py
------
class fooTest(unittest.TestCase):
def fooMethodTest(self):
// smthg
barTest.py
------
class barTest(unittest.TestCase):
def barMethodTest(self):
// smthg
In any, test and source module, file, I erased the if __name__ == "__main__":
because of increasing coherency and obeying object-oriented ideology. 在任何测试和源模块文件中,我删除了
if __name__ == "__main__":
因为增加了一致性并遵循面向对象的意识形态。
Like in Java unit test, I'm looking for creating a module to run all unittest. 就像在Java单元测试中一样,我正在寻找创建一个模块来运行所有unittest。 For example,
例如,
runAllTest.py
-------------
class runAllTest(unittest.TestCase):
?????
if __name__ == "__main__":
?????
I looked for search engine but didn't find any tutorial or example. 我寻找搜索引擎,但没有找到任何教程或示例。 Is it possible to do so?
有可能这样做吗? Why?
为什么? or How?
或者怎么样?
Note: I'm using eclipse and pydev distribution on windows machine. 注意:我在windows机器上使用eclipse和pydev发行版。
You could create a TestSuite
and run all your tests in it's if __name__ == '__main__'
block: 您可以创建一个
TestSuite
并在其中运行所有测试, if __name__ == '__main__'
块:
import unittest
def create_suite():
test_suite = unittest.TestSuite()
test_suite.addTest(fooTest())
test_suite.addTest(barTest())
return test_suite
if __name__ == '__main__':
suite = create_suite()
runner=unittest.TextTestRunner()
runner.run(suite)
If you do not want to create the test cases manually look at this quesiton/answer , which basically creates the test cases dynamically, or use some of the features of the unittest
module like test discovery feature and command line options .. 如果您不想手动创建测试用例,请查看此问题/答案 ,它基本上可以动态创建测试用例,或者使用
unittest
模块的一些功能,如测试发现功能和命令行选项。
When running unit tests based on the built-in python unittest module, at the root level of your project run 在基于内置python unittest模块运行单元测试时,在项目运行的根级别
python -m unittest discover <module_name>
For the specific example above, it suffices to run 对于上面的具体示例,运行就足够了
python -m unittest discover .
https://docs.python.org/2/library/unittest.html https://docs.python.org/2/library/unittest.html
I think what you are looking for is the TestLoader . 我认为你要找的是TestLoader 。 With this you can load specific tests or modules or load everything under a given directory.
有了这个,您可以加载特定的测试或模块或加载给定目录下的所有内容。 Also, this post has some useful examples using a
TestSuite
instance. 此外, 这篇文章还有一些使用
TestSuite
实例的有用示例。
EDIT: The code I usually have in my test.py: 编辑:我通常在test.py中的代码:
if not popts.tests:
suite = unittest.TestLoader().discover(os.path.dirname(__file__)+'/tests')
#print(suite._tests)
# Print outline
lg.info(' * Going for Interactive net tests = '+str(not tvars.NOINTERACTIVE))
# Run
unittest.TextTestRunner(verbosity=popts.verbosity).run(suite)
else:
lg.info(' * Running specific tests')
suite = unittest.TestSuite()
# Load standard tests
for t in popts.tests:
test = unittest.TestLoader().loadTestsFromName("tests."+t)
suite.addTest(test)
# Run
unittest.TextTestRunner(verbosity=popts.verbosity).run(suite)
Does two things: 有两件事:
You are looking for nosetests . 你正在寻找鼻试 。
You might need to rename your files; 您可能需要重命名文件; I'm not sure about the pattern nose uses to find the test files but, personally, I use
*_test.py
. 我不确定模式鼻子用于查找测试文件,但个人而言,我使用
*_test.py
。 It is possible to specify a custom pattern which your project uses for test filenames but I remember being unable to make it work so I ended up renaming my tests instead. 可以指定项目用于测试文件名的自定义模式,但我记得无法使其工作,所以我最终重命名了我的测试。
You also need to follow PEP 328 conventions to work with nose. 您还需要遵循PEP 328惯例才能使用鼻子。 I don't use IDEs with Python but your IDE may already follow it---just read the PEP and check.
我不使用带有Python的IDE,但你的IDE可能已经遵循它了 - 只需阅读PEP并检查。
With a PEP 328 directory/package structure, you can run individual tests as 使用PEP 328目录/包结构,您可以运行单独的测试
nosetests path.to.class_test
Note that instead of the usual directory separators ( /
or \\
), I used dots. 请注意,我使用点而不是通常的目录分隔符(
/
或\\
)。
To run all tests, simply invoke nosetests
at the root of your project. 要运行所有测试,只需在项目的根目录中调用
nosetests
即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.