pytest(1)-简介

简介: pytest 是 python 的一个第三方单元测试框架,它继承自 python 自带的单元测试框架unittest,兼容 unittest。相比unittest,pytest的可扩展性更高,也是目前最为流行的 python 单元测试框架。至于它扩展性表现在哪些方面,我们需在后续的学习中一点一点进行总结。


前言


pytest 是 python 的一个第三方单元测试框架,它继承自 python 自带的单元测试框架unittest,兼容 unittest

相比unittestpytest的可扩展性更高,也是目前最为流行的 python 单元测试框架。至于它扩展性表现在哪些方面,我们需在后续的学习中一点一点进行总结。


简单使用


1 安装

安装pytest

pip install -U pytest

验证安装是否成功

pytest --version

2 用例编写

为了先让大家对pytest的使用有个粗浅的认识,接下来我们使用pytest对两个自定义接口进行简单测试,其中查询所有用户信息接口为get请求,注册用户接口为post请求,编写脚本test_demo.py,代码如下:

import pytest
import requests, json
class TestDemo:
    def test_get_all_users(self):
        '''查询所有用户信息'''
        url = "http://127.0.0.1:5000/users"
        # 请求接口
        res = requests.get(url=url).text
        res = json.loads(res)
        print(res)
        # 断言
        assert res['code'] == 0
    def test_register(self):
        '''注册用户'''
        headers = {"Content-Type": "application/json;charset=utf8"}
        url = "http://127.0.0.1:5000/register"
        data = {
            "username": "张学友",
            "password": "123456",
            "sex": "0",
            "telephone": "13823456789",
            "address": "北京东城区"
        }
        # 请求接口
        res = requests.post(url=url, headers=headers, json=data).text
        res = json.loads(res)
        print(res)
        # 断言
        assert res['code'] == 0
if __name__ == '__main__':
    pytest.main()

注意,测试类中不能定义__init__方法。

3 用例执行

  • 方式一:在python代码里调用pytest,使用pytest.mian()
# 执行当前目录下的测试用例
pytest.main()
# 执行指定的测试用例
pytest.main("testcase/test_one.py")
# 加参数如-s,执行参数根据需要进行添加,后面文章会对常用的参数进行说明
pytest.main(["-s", "testcase/test_one.py"])
  • pycharm控制台输出如下:

微信图片_20220424220635.png

  • 方式二:命令行执行
    pytest命令行执行pytest + 测试文件路径,示例如下
pytest E:/apiAutoTest/test_demo.py
# 加参数如-s
pytest -s E:/apiAutoTest/test_demo.py
  • 除了上面的直接通过pytest命令调用外,还可以通过命令在python解释器里调用,python -m 测试文件完整路径,如下:
python -m pytest E:/apiAutoTest/test_demo.py

结果如下:

微信图片_20220424220638.png


总结


如果使用unittest框架编写上面的测试用例的话,代码应该如下:

import unittest
import requests, json
class TestDemo(unittest.TestCase):
    def test_get_all_users(self):
        '''查询所有用户信息'''
        url = "http://127.0.0.1:5000/users"
        res = requests.get(url=url).text
        res = json.loads(res)
        self.assertEqual(res['code'], 0)
    def test_register(self):
        '''注册用户'''
        headers = {"Content-Type": "application/json;charset=utf8"}
        url = "http://127.0.0.1:5000/register"
        data = {
            "username": "张学友",
            "password": "123456",
            "sex": "0",
            "telephone": "13823456789",
            "address": "北京东城区"
        }
        res = requests.post(url=url, headers=headers, json=data).text
        res = json.loads(res)
        self.assertEqual(res['code'], 0)
if __name__ == '__main__':
    unittest.main()

由这个简单的测试用例,我们可以比较出pytestunittest 在测试用例的编写及执行时不一样的地方:

  1. pytest框架编写测试用例时,只需要引入 pytest 模块,使用python源生态的 assert 进行断言,且可以使用自带的命令行执行测试用例。
  2. unittest框架编写测试用例时,自定义的测试类需要继承unittest.TestCase,断言则使用的是unittest提供的断言方式(如 assertEqual、assertTrue、assertIn等),没有提供命令行方式执行测试用例。

当然,pytestunittest的区别远不止这些,待我们后续慢慢道来。

相关文章
|
10月前
|
测试技术 C++ Python
selenium-pytest框架的基础学习
selenium-pytest框架的基础学习
61 0
|
11月前
|
测试技术 Python
01-pytest-安装及入门
01-pytest-安装及入门
|
11月前
|
JSON 测试技术 数据格式
19-pytest-allure-pytest环境搭建
19-pytest-allure-pytest环境搭建
|
12月前
|
测试技术 数据库 Python
Python单测框架Pytest教程
The pytest framework makes it easy to write small, readable tests, and can scale to support complex functional testing for applications and libraries. pytest介绍和教程。 pytest 框架使编写小型可读测试变得容易,并且可以扩展以支持应用程序和库的复杂功能测试。
139 0
|
Python Windows
pytest 框架环境搭建
pytest 框架环境搭建
355 0
|
测试技术 Python
Pytest系列(16)- 分布式测试插件之pytest-xdist的详细使用
Pytest系列(16)- 分布式测试插件之pytest-xdist的详细使用
562 0
|
自然语言处理 Java 测试技术
pytest学习和使用21-测试报告插件allure-pytest如何使用?
pytest学习和使用21-测试报告插件allure-pytest如何使用?
129 0
pytest学习和使用21-测试报告插件allure-pytest如何使用?
|
负载均衡 监控 测试技术
pytest学习和使用20-pytest如何进行分布式测试?(pytest-xdist)
pytest学习和使用20-pytest如何进行分布式测试?(pytest-xdist)
156 0
pytest学习和使用20-pytest如何进行分布式测试?(pytest-xdist)
|
Linux 测试技术 Python
pytest学习和使用1-pytest安装和版本查看
pytest学习和使用1-pytest安装和版本查看
413 0
pytest学习和使用1-pytest安装和版本查看
|
Web App开发 Java Python
Python+Pychram+pytest环境搭建
学习了解Python+Pychram+pytest环境搭建。
186 0
Python+Pychram+pytest环境搭建