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

相关推荐
程序猿000001号23 分钟前
探索Python的pytest库:简化单元测试的艺术
python·单元测试·pytest
爱学测试的雨果1 天前
分布式测试插件 pytest-xdist 使用详解
分布式·pytest
七灵微3 天前
【测试】Pytest
pytest
钱钱钱端3 天前
Pytest参数详解 — 基于命令行模式!
自动化测试·软件测试·python·jmeter·职场和发展·pytest·压力测试
blues_C4 天前
Pytest-Bdd vs Behave:选择最适合的 Python BDD 框架
自动化测试·python·pytest·测试框架·bdd
天天要nx4 天前
D101【python 接口自动化学习】- pytest进阶之fixture用法
python·pytest
大霞上仙4 天前
pytest入门五:命令行参数
pytest
blues_C6 天前
Pytest-Bdd-Playwright 系列教程(17):标签管理(Tags)
自动化测试·pytest·bdd·tags
大霞上仙6 天前
pytest入门十:配置文件
pytest
大霞上仙6 天前
pytest入门九:feature
pytest