unittest和pytest
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
多线程运行测试用例
方法一:pytest -n 2
方法二:
all.py
import pytest
if __name__ == '__main__':
pytest.main(['-n=2'])
失败重跑
test_product.py
from time import sleep
import pytest
class TestProduct:
def test_product01(self):
assert 1==2
print("-----test_product01")
def test_product02(self):
print("-----test_product02")
def test_product03(self):
print("-----test_product03")
def test_product04(self):
print("-----test_product04")
def test_product05(self):
print("-----test_product05")
if __name__ == '__main__':
pytest.main()
all.py
import pytest
if __name__ == '__main__':
pytest.main(['--reruns=2'])或 终端测试方法: pytest --reruns 2
部分测试结果:
test_product.py::TestProduct::test_product01 RERUN
test_product.py::TestProduct::test_product01 RERUN
test_product.py::TestProduct::test_product01 FAILED
==================== 1 failed, 4 passed, 2 rerun in 0.10s =====================
运行pytest -x
输出
!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!
=============================== 1 failed in 0.12s ===============================
运行pytest -x在test_product.py的test_product01方法失败后就不会继续运行下面的方法
pytest --maxfail 2 最大失败次数2次
========================== 1 failed, 4 passed in 0.13s ==========================
test_product.py::TestProduct::test_product01 FAILED
test_product.py::TestProduct::test_product02 -----test_product02
PASSED
test_product.py::TestProduct::test_product03 -----test_product03
PASSED
test_product.py::TestProduct::test_product04 -----test_product04
PASSED
test_product.py::TestProduct::test_product05 -----test_product05
PASSED
test_product01方法失败了一次,但没有到2次,后面方法仍可以运行
终端运行pytest -k test_product03 测试名字包含test_product03的方法
测试结果:
test_product.py::TestProduct::test_product03 -----test_product03
PASSED
======================== 1 passed, 4 deselected in 0.03s ========================
unittest:ascII的大小来绝对的执行的顺序
pytest执行测试用例顺序默认是从上到下的顺序,
pytest.mark.run(order=1)可改变执行顺序
test_product.py
import pytest
class TestProduct:
def test_product01(self):
print("-----test_product01")
def test_product02(self):
print("-----test_product02")
@pytest.mark.run(order=1)
def test_product03(self):
print("-----test_product03")
@pytest.mark.run(order=2)
def test_product04(self):
print("-----test_product04")
@pytest.mark.run(order=3)
def test_product05(self):
print("-----test_product05")
if __name__ == '__main__':
pytest.main()
运行结果:
-----test_product03
PASSED-----test_product04
PASSED-----test_product05
PASSED-----test_product01
PASSED-----test_product02
PASSED
pytest.ini的使用
[pytest]
addopts = -vs
testpaths = ./tests #测试用例的路径
python_files = test_*.py #模块名的规则
python_classes = Test* #类名的规则
python_finctions = test #方法名的规则
@pytest.mark.smoke的使用
pytest.ini
[pytest]
addopts = -vs
testpaths = ./tests
python_files = test_*.py
python_classes = Test*
python_finctions = test
markers=
smoke:冒烟用例
test_product.py
import pytest
class TestProduct:
def test_product01(self):
print("-----test_product01")
def test_product02(self):
print("-----test_product02")
@pytest.mark.run(order=1)
@pytest.mark.smoke
def test_product03(self):
print("-----test_product03")
@pytest.mark.run(order=2)
def test_product04(self):
print("-----test_product04")
@pytest.mark.run(order=3)
def test_product05(self):
print("-----test_product05")
if __name__ == '__main__':
pytest.main()
输入pytest -m “smoke”
运行test_product.py,执行结果
============================ 1 passed, 5 deselected, 2 warnings in 0.04s =============================
只有product03被标记了,所以只有一个passed
test_product.py
import pytest
class TestProduct:
def test_product01(self):
print("-----test_product01")
def test_product02(self):
print("-----test_product02")
@pytest.mark.run(order=1)
@pytest.mark.smoke
def test_product03(self):
print("-----test_product03")
@pytest.mark.usermanage
@pytest.mark.run(order=2)
def test_product04(self):
print("-----test_product04")
@pytest.mark.run(order=3)
def test_product05(self):
print("-----test_product05")
if __name__ == '__main__':
pytest.main()
输入pytest -m “smoke or usermanage”,
运行结果:
test_product.py::TestProduct::test_product03 -----test_product03
PASSED
test_product.py::TestProduct::test_product04 -----test_product04
PASSED
pytest跳过测试用例
(1)无条件跳过
@pytest.mark.skip(reason=“skip”)的用法:
test_product.py
import pytest
class TestProduct:
def test_product01(self):
print("-----test_product01")
def test_product02(self):
print("-----test_product02")
@pytest.mark.run(order=1)
@pytest.mark.smoke
def test_product03(self):
print("-----test_product03")
@pytest.mark.usermanage
@pytest.mark.run(order=2)
@pytest.mark.skip(reason="skip")
def test_product04(self):
print("-----test_product04")
@pytest.mark.run(order=3)
def test_product05(self):
print("-----test_product05")
if __name__ == '__main__':
pytest.main()
终端输入pytest
测试结果
============================== 5 passed, 1 skipped, 2 warnings in 0.04s ==============================
(2)有条件跳过
@pytest.mark.skipif(age>=18, reason=“skip”)的用法:
test_product.py
import pytest
class TestProduct:
age = 18
def test_product01(self):
print("-----test_product01")
def test_product02(self):
print("-----test_product02")
@pytest.mark.run(order=1)
@pytest.mark.smoke
def test_product03(self):
print("-----test_product03")
@pytest.mark.usermanage
@pytest.mark.run(order=2)
@pytest.mark.skip(reason="skip")
def test_product04(self):
print("-----test_product04")
@pytest.mark.run(order=3)
@pytest.mark.skipif(age>=18, reason="skip")
def test_product05(self):
print("-----test_product05")
if __name__ == '__main__':
pytest.main()
输入pytest,执行结果:
test_product.py::TestProduct::test_product04 SKIPPED (skip)
test_product.py::TestProduct::test_product05 SKIPPED (skip)