您现在的位置是:首页 > 文章详情

python unittest框架理解

日期:2019-06-19点击:499

整个平台的搭建使用的是python的unittest测试框架,这里简单介绍下unittest模块的简单应用。

unittest是python的标准测试库,相比于其他测试框架是python目前使用最广的测试框架。

Unittest framework
3
4

正常调用unittest的流程是:

1、TestLoader 自动将测试用例TestCase中加载到TestSuite里

2、在执行TestCase过程中,先进行SetUp()环境准备,执行测试代码。

3、TextTestRunner调用TestSuite的run方法,顺序执行里面的TestCase中以test开头的方法,其中TestLoader在加载过程中,进行添加的TestCase是没有顺序的。一个TestCase里如果存在多个验证方法的话,会按照方法中test后方首字母的排序进行执行。可以通过手动调用TestSuite的addTest、addTests方法来动态添加TestCase,这样既可以确定添加用例的执行顺序,也可避免TestCase中的验证方法一定要用test开头。

4、最后tearDown()进行测试的还原。

5、得到测试结果TestResult。

Keywords
TestCase: The individual unit of testing.
TestSuite: A collection of test cases, test suites, or both. It is used to aggregate tests that should be executed together.
TestLoader: Load the test cases to the test suite.
TestRunner: A component which orchestrates the execution of tests and provides the outcome to the user.
TestFixture: Represents the preparation needed to perform one or more tests, and any associate cleanup actions. setUp(), tearDown(), setUpClass(), tearDownClass()
Decorator: skip, skipIf, skipUnless, expectedFailure

unittest有四个比较重要的概念是test fixture, test case, test suite, test runner, 。

test fixture:The test fixture is everything we need to have in place to exercise the sut。简单来说就是做一些测试过程中需要准备的东西,比如创建临时的数据库,文件和目录等,其中 setUp() 和 setDown() 是最常用的方法

test case:用户自定义的测试case的基类,调用run()方法,会依次调用setUP方法、执行用例的方法、tearDown()方法。

test suite:测试用例集合,可以通过addTest()方法手动增加Test Case,也可通过TestLoader自动添加Test Case,TestLoader在添加用例时,会没有顺序。

test runner:运行测试用例的驱动类,可以执行TestCase,也可执行TestSuite。执行后TestCase和Testsuite会自动管理TestResult。

Command line
python -m unittest xxx
python -m unittest -h
python -m unittest discover # discovery the test cases(test*.py) and execute automatically

原文链接:https://yq.aliyun.com/articles/706075
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章