自动化测试框架unittest和pytest的区别

关键词:
unitest vs pytest

在这里插入图片描述

unittest vs pytest

写此文章的目的并不是为了二选一,说哪个好,哪个不好。而是为了对比两个框架,吸收其优秀的特点和思想。
unittest 和 pytest,是python里面两个测试框架。
unittest和pytest都是用例执行引擎。

如果让我选的话,我肯定选择unittest,但是没有任何黑pytest的意思。不知道你更喜欢哪个呢?欢迎评论告诉我。

我有一百万条测试用例,需要分布到一百台服务器上运行。
unittest可以分布式测试用例执行吗?
pytest可以分布式测试用例执行吗?

我关心的是什么?

哪些点是可以用来作为对比两个测试框架的标注呢?
一条用例要怎么写?
多条用例要怎么组织?
有哪些可用的钩子?
断言
用例失败的方式
用例通过的方式
是否能够并发或者分布式执行
报告怎么样?
用例状态有哪些?
重试机制?
有没有类似于testng的监听器?
测试框架是否有侵入测试代码?unitest的测试用例需要继承对应的Class
如何控制用例的执行顺序?
如何处理用例的依赖关系?
如何对测试结果进行上报?
如何发现测试用例?

unittest 介绍

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

pytest介绍

我对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

pytest的测试用例演示:
在这里插入图片描述

pytest or unittest?

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.

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值