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

相关推荐
_可乐无糖7 小时前
使用--log-file保存pytest的运行日志
pytest
blues_C20 小时前
Pytest-Bdd-Playwright 系列教程(9):datatable 参数的使用
自动化测试·pytest·bdd·playwright
ayyyy____1 天前
pytest执行用例时从conftest.py抛出ModuleNotFoundError:No module named ‘XXX‘异常的解决办法
pytest
_可乐无糖1 天前
pytest中的断言
python·pytest
幸运的星竹2 天前
使用pytest+openpyxl做接口自动化遇到的问题
python·自动化·pytest
_可乐无糖2 天前
mac终端使用pytest执行iOS UI自动化测试方法
macos·pytest
小码哥说测试4 天前
Selenium+Pytest自动化测试框架 ------ 禅道实战
自动化测试·软件测试·selenium·测试工具·单元测试·pytest·接口测试
m0_371356154 天前
【测试框架篇】单元测试框架pytest(2):用例编写
单元测试·pytest
m0_371356154 天前
【测试框架篇】单元测试框架pytest(3):用例执行参数详解
单元测试·pytest