关键词:
unitest vs pytest
写此文章的目的并不是为了二选一,说哪个好,哪个不好。而是为了对比两个框架,吸收其优秀的特点和思想。
unittest 和 pytest,是python里面两个测试框架。
unittest和pytest都是用例执行引擎。
如果让我选的话,我肯定选择unittest,但是没有任何黑pytest的意思。不知道你更喜欢哪个呢?欢迎评论告诉我。
我有一百万条测试用例,需要分布到一百台服务器上运行。
unittest可以分布式测试用例执行吗?
pytest可以分布式测试用例执行吗?
我关心的是什么?
哪些点是可以用来作为对比两个测试框架的标注呢?
一条用例要怎么写?
多条用例要怎么组织?
有哪些可用的钩子?
断言
用例失败的方式
用例通过的方式
是否能够并发或者分布式执行
报告怎么样?
用例状态有哪些?
重试机制?
有没有类似于testng的监听器?
测试框架是否有侵入测试代码?unitest的测试用例需要继承对应的Class
如何控制用例的执行顺序?
如何处理用例的依赖关系?
如何对测试结果进行上报?
如何发现测试用例?
unittest是python的内置的一个模块。
The unittest unit testing framework was originally inspired by JUnit and has a similar flavor as major unit testing frameworks in other languages. It supports test automation, sharing of setup and shutdown code for tests, aggregation of tests into collections, and independence of the tests from the reporting framework.
To achieve this, unittest supports some important concepts in an object-oriented way:
test fixture
A test fixture represents the preparation needed to perform one or more tests, and any associated cleanup actions. This may involve, for example, creating temporary or proxy databases, directories, or starting a server process.
test case
A test case is the individual unit of testing. It checks for a specific response to a particular set of inputs. unittest provides a base class, TestCase, which may be used to create new test cases.
test suite
A test suite is a collection of test cases, test suites, or both. It is used to aggregate tests that should be executed together.
test runner
A test runner is a component which orchestrates the execution of tests and provides the outcome to the user. The runner may use a graphical interface, a textual interface, or return a special value to indicate the results of executing the tests.
unittest.TestCase类
留给使用者的一些钩子
setUp 每个用例(可以理解为方法)执行前运行
tearDown 每个用例执行后运行
在类级别的钩子
setUpClass 类执行前运行
tearDownClass 类执行后运行
Test Code example
unittest代码演示:断言:unittest里面一些常用的断言方法
https://blog.csdn.net/lineuman/article/details/51854817
我对unittest比较熟悉,pytest才刚开始使用,等我使用过一段时间再来补充这篇文章。
人们喜欢pytest的原因之一是,当写pytest的测试用例的时候,不需要继承任何基类。
pytest: helps you write better programs.
The pytest framework makes it easy to write small, readable tests, and can scale to support complex functional testing for applications and libraries.
pytest是一个三方包,需要pip install pytest
unittest好用还是pytest好用?
问题没有标准答案。不同条件,不同使用人员可能都会给出不同答案。
参考:
https://knapsackpro.com/testing_frameworks/difference_between/unittest/vs/pytest
https://www.pythonpool.com/python-unittest-vs-pytest/#:~:text=%20Unittest%20is%20the%20testing%20framework%20set%20in,require%20less%20piece%20of%20code%20compared%20to%20unittest.