unittest和pytest

时间:2024-11-16 07:38:10

unittest终端运行方法
ecshop_login.py

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

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

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

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

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

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

yaml_util.py

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

-
  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

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

import pytest

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


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

方法一:终端运行pytest test_login.py
方法二:新建all.py并运行

all.py如下:

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