unittest和pytest

unittest终端运行方法

ecshop_login.py

python 复制代码
import unittest


class EcshopLoginTest(unittest.TestCase):
    def test01_baidu(self):
        print("百度")

    def test01_bytedance(self):
        print("字节跳动")

终端运行

python -m unittest ecshop_login.EcshopLoginTest -v

python -m unittest -v ecshop_login.EcshopLoginTest -k *_bytedance

python -m unittest -v ecshop_login.EcshopLoginTest -k dance

python ecshop_login.py

用例执行顺序

按ASCII码的规则【0-9 A-Z a-z】

ecshop_login.py

python 复制代码
import unittest

class EcshopLoginTest(unittest.TestCase):
    def test01_baidu(self):
        print("百度")

    def test01_bytedance(self):
        print("字节跳动")

    def test11_alibaba(self):
        print("阿里巴巴")
if __name__ == '__main__':
    unittest.main()

输出顺序:百度 字节跳动 阿里巴巴

ecshop_login.py

python 复制代码
import os
import unittest
class EcshopLoginTest(unittest.TestCase):
    def test1_baidu(self):
        print("百度")

    def test2_bytedance(self):
        print("字节跳动")

    def test11_alibaba(self):
        print("阿里巴巴")

if __name__ == '__main__':
    print("++++++++++++++++++++++++++")
    suite = unittest.TestSuite()
    testcase = unittest.defaultTestLoader.discover(start_dir=os.getcwd(), pattern='ecshop*.py')
    suite.addTests(testcase)
    unittest.main(defaultTest='suite')

#输出阿里巴巴 百度 字节跳动

ecshop_login.py

python 复制代码
import unittest
class EcshopLoginTest(unittest.TestCase):
    def test1_baidu(self):
        print("百度")

    def test2_bytedance(self):
        print("字节跳动")

    def test11_alibaba(self):
        print("阿里巴巴")

if __name__ == '__main__':
    suite = unittest.TestSuite()
    testcase = [EcshopLoginTest("test1_baidu")]
    suite.addTests(testcase)
    unittest.main(defaultTest='suite')
#输出百度

ecshop_login.py

python 复制代码
import unittest
class EcshopLoginTest(unittest.TestCase):
    def test1_baidu(self):
        print("百度")

    def test2_bytedance(self):
        print("字节跳动")

    def test11_alibaba(self):
        print("阿里巴巴")

if __name__ == '__main__':
    suite = unittest.TestSuite()
    suite.addTest(EcshopLoginTest('test1_baidu'))
    suite.addTest(EcshopLoginTest('test2_bytedance'))
    unittest.main(defaultTest='suite')  #unittest.TextTestRunner().run(suite)
#输出百度 字节跳动

Json格式

yaml_test.yaml

yaml 复制代码
-
 大厂: [{name: '阿里巴巴'},{name: '字节跳动'},{name: '美团'}]
 大公司: [{name: '百度'},{name: '腾讯'},{name: '京东'}]

yaml_util.py

python 复制代码
import yaml

class YamlUtilTest:
    def __init__(self,yaml_path):
        self.yaml_path = yaml_path

    def read_yaml(self):
        with open(self.yaml_path,encoding='utf-8') as f:
            yaml_data = yaml.load(stream=f.read(),Loader=yaml.FullLoader)
            return yaml_data

if __name__ == '__main__':
    yaml_util = YamlUtilTest('yaml_test.yaml').read_yaml()
    print(yaml_util)#输出[{'大厂': [{'name': '阿里巴巴'}, {'name': '字节跳动'}, {'name': '美团'}], '大公司': [{'name': '百度'}, {'name': '腾讯'}, {'name': '京东'}]}]

yaml_test.yaml

yaml 复制代码
-
  name: 获取接口统一鉴权码token接口
  request:
    method: GET
    url: https://api.weixin.qq.com/cgi-bin/token
    data:
      grant type: client_credential
      appid: wx6b11b3efd1cdc290
      secret: 106a9c6157c4db5f6029918738f9529d
  validate:
    - equals: {status code: 200}
    - contains: access token

gzh_case.py

python 复制代码
import requests
from ddt import file_data, ddt

@ddt
class GzhTestCase(unittest.TestCase):

    @file_data('yaml_test.yaml')
    def test_get_token(self,**kwargs):
        if 'name' in kwargs.keys() and 'request' in kwargs.keys() and 'validate' in kwargs.keys():
            if jsonpath.jsonpath(kwargs,'$..url') and jsonpath.jsonpath(kwargs,'$..data') and jsonpath.jsonpath(kwargs,'$..method'):
                res = requests.get(url=kwargs['request']['url'],params=kwargs['request']['data'])
                print("res的值是:",res.json())
                print("validate",kwargs['validate'])

                for validate_data in kwargs['validate']:
                    print("validate_data:",validate_data)
                    for key,value in validate_data.items():
                        print('key=',key,'value=',value)
                        if key == 'equals':
                            pass
                        elif key == 'contains':
                            if value in res.text:
                                print("断言通过")
                            else:
                                print("断言失败",'value=',value)
            else:
                print("关键字不包含url或data或method")
        else:
            print("关键字必须包含name,request,validate")

if __name__ == '__main__':
    unittest.main()

只运行test_login.py:

test_login.py

python 复制代码
import pytest

class TestLogin:
    def test_login(self):
        print('-----test_login')


if __name__ == '__main__':
    pytest.main()

方法一:终端运行pytest test_login.py

方法二:新建all.py并运行

all.py如下:

python 复制代码
import pytest

if __name__ == '__main__':
    pytest.main(['test_login.py'])

pytest测试用例的运行方式

1.主函数模式

(1)运行所有:pytest.main()

(2)指定模块:pytest.main(['test_login.py'])

(3)指定目录:pytest.main(['./interface_testcase'])

(4)指定方法:pytest.main(['./interface_testcase/test_interface.py::test_01'])

2.命令行模式:

(1)运行所有:pytest

(2)指定模块:pytest test_login.py

(3)指定目录:pytest ./interface_testcase

(4)指定方法:pytest ./interface_testcase/test_interface.py::test_01

相关推荐
ThreeAu.9 小时前
pytest 实战:用例管理、插件技巧、断言详解
python·单元测试·pytest·测试开发工程师
我的xiaodoujiao17 小时前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 18--测试框架Pytest基础 2--插件和参数化
python·学习·测试工具·pytest
小小测试开发1 天前
pytest 库用法示例:Python 测试框架的高效实践
开发语言·python·pytest
know__ledge1 天前
Pytest+requests进行接口自动化测试8.0(Allure进阶 + 文件上传接口 + 单接口多用例)
pytest
川石课堂软件测试2 天前
CSS中常用的几种定位。
开发语言·css·python·网络协议·http·html·pytest
啊森要自信3 天前
【GUI自动化测试】Python 自动化测试框架 pytest 全面指南:基础语法、核心特性(参数化 / Fixture)及项目实操
开发语言·python·ui·单元测试·pytest
啊森要自信3 天前
【GUI自动化测试】YAML 配置文件应用:从语法解析到 Python 读写
android·python·缓存·pytest·pip·dash
我的xiaodoujiao4 天前
从 0 到 1 搭建完整 Python 语言 Web UI自动化测试学习系列 17--测试框架Pytest基础 1--介绍使用
python·学习·测试工具·pytest
程序员杰哥5 天前
Pytest与Unittest测试框架对比
自动化测试·软件测试·python·测试工具·测试用例·excel·pytest
软件测试小仙女5 天前
Pytest参数化实战:高效测试API接口
软件测试·测试开发·测试工具·pytest·接口测试·api·参数化