pytest(1) - 简介

前言

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控制台输出如下:

  • 方式二:命令行执行

    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
    

结果如下:

总结

如果使用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的区别远不止这些,待我们后续慢慢道来。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容