当前位置:网站首页>Pytest环境部署+用例执行管理+用例参数化
Pytest环境部署+用例执行管理+用例参数化
2022-07-29 02:30:00 【司小幽】
1.命名规范
import pytest
# 测试函数
def testCase():
print("discover目录下的测试用例testCase")
if __name__ == '__main__':
pytest.main(['-s'])
# 测试函数
def test02():
print("doc目录下的用例test02")
# 测试类以Test
class TestDemo:
# 不能在pytest类中加构造方法
# def __init__(self):
# print("构造方法")
# 测试方法
def test_case_03(self):
print("类下的用例")
import pytest
def test_01():
print("hello")
assert 1 == 1
if __name__ == '__main__':
pytest.main(['-s'])
import unittest
class UnitDemo(unittest.TestCase):
def test_login(self):
print("login")
self.assertEqual("123","1231",msg = "断言失败")
if __name__ == '__main__':
unittest.main()
2.断言+rerun+setup
pytest.ini
[pytest]
markers =
smoke: marks tests as smoke
slow: marks tests as slow
""" 借助Python的运算符号和assert关键字来实现的。 """
import pytest
def test_kkk_01():
print('kkk')
# assert "预期结果" == "实际结果"
assert 1 == 1 and 1 == 2
""" 测试不相等 != <= >= 测试包含 in 测试不包含 not in 判断是否为true: is True 判断是否不为true: is not True/is False and or """
@pytest.mark.smoke
def test_kkk_02():
print('kkk')
assert 1 == 2
def test_zzz_03():
print('zzz')
assert 1==1
if __name__ == '__main__':
# pytest.main(['-s','-v','-k','zzz'])
# pytest.main(['-s','-v','-k=zzz'])
# pytest.main(['-vs'])
# pytest.main(['-q'])
# pytest.main(['-x'])
# pytest.main(['-s','./doc/test_demo2.py::TestDemo::test_case_03'])
# pytest.main(['-s','--junit-xml=./report/junit_report1.xml'])
# pytest.main(['--maxfail=2'])
pytest.main(['-m','smoke'])
import pytest
def test_case01():
assert 1==1
def test_case02():
assert 1==2
def test_case03():
assert 1 == 3
def test_case04():
assert 1 == 4
def test_case05():
assert 1 == 5
def test_case06():
assert 1 == 6
if __name__ == '__main__':
# pytest.main(['test_many.py'])
# 将测试执行发送到多个cpu;
# pytest.main(['-n','2','test_many.py'])
# 使用与计算机具有cpu内核一样多的进程
pytest.main(['-n', 'auto', 'test_many.py'])
import pytest
def test_case01():
assert 1==1
def test_case02():
assert 1==2
def test_case03():
assert 1 == 3
def test_case04():
assert 1 == 4
def test_case05():
assert 1 == 5
def test_case06():
assert 1 == 6
if __name__ == '__main__':
# 重新运行所有测试失败用例
# pytest.main(['test_many.py'])
# pytest.main(['--reruns', '3', 'test_rerun.py'])
# 在每次重跑之间,增加一次延迟时间
pytest.main(['--reruns', '3', '--reruns-delay','2','test_rerun.py'])
# 功能函数
import pytest
def multiply(a,b):
return a * b
# if __name__ == '__main__':
# print(multiply(1,2))
""" 不含有类的用例预置和后置函数 第一批次: setup_module/teardown_module:在当前文件中,在所有测试用例执行之前与之后执行 第二批次:setup_function/teardown_function:在每个测试函数之前与之后执行 第三批次:setup/teardown:在每个测试函数之前与之后执行,在类中可以使用 ps:执行的顺序是按优先级来的,改变方法位置结果也一样 """
# 用例前置后置
def setup_module(module):
print("setup_module========================")
def teardown_module(module):
print("teardown_module=======================")
def setup_function(function):
print("setup_function==========================")
def teardown_function(function):
print("teardown_function==========================")
def setup():
print("setup==========================")
def teardown():
print("teardown==========================")
# ================测试用例==================
def test_01():
print(multiply(3,4))
def test_02():
print(multiply(4,4))
if __name__ == '__main__':
pytest.main(['-s','test_setup01.py'])
# 功能函数
import unittest
import pytest
def multiply(a,b):
return a*b
class TestCase:
""" 第一批次:setup_class/teardown_class:在当前的测试类的开始和结束时执行 第二批次:setup_method/teardown_method:在每个测试方法开始与结束时执行 第三批次:setup/teardown:在每个测试方法开始与结束时执行 ps:执行的顺序按优先级来的,改变方法位置结果也一样 """
@classmethod
def setup_class(cls):
print("setup_class====================================")
@classmethod
def teardown_class(cls):
print("teardown_class=====================================")
def setup_method(self,method):
print("setup_method===============================")
def teardown_method(self, method):
print("teardown_method===============================")
def setup(self):
print("setup===============================")
def teardown(self):
print("teardown===============================")
#================测试用例=================
def test01(self):
print("类里的第一条用例")
print(multiply(2,2))
# 测试函数
def test02():
print("doc目录下的用例test02")
# 测试类以Test
class TestDemo:
# 不能在pytest类中加构造方法
# def __init__(self):
# print("构造方法")
# 测试方法
def test_case_03(self):
print("类下的用例")
3.Pytest配置文件
pytest.ini
[pytest]
# 01 命令行参数,默认加到执行过程中
addopts = -s -v --html=./report/html_report.html
# 02 指定要运行的测试目录
testpaths = ./doc
# 03 指定要运行的测试文件规则
python_files = auto*.py
# 04 指定要运行的类名规则
python_classes = A* B*
# 05 指定要运行的测试用例方法/函数名
python_functions = ff*
import pytest
def test_case_01():
print("test_case_01")
if __name__ == '__main__':
pytest.main()
import pytest
def test_case_01():
print("test_case_02")
if __name__ == '__main__':
pytest.main()
import pytest
def test_case_03():
print("test_case_03")
def ff01():
print("ff01用例")
class A01:
def test_aaa(self):
print("A类里的用例")
def ff02(self):
print("A类里的用例ff02")
class B01:
def test_bbb(self):
print("B类里的用例")
def ff03(self):
print("B类里的用例ff03")
class Test01:
def test_ccc(self):
print("Test01类里的用例")
def ff04(self):
print("Test01类里的用例ff03")
if __name__ == '__main__':
pytest.main()
4.Pytest高阶用法之跳过用例
1.无条件跳过
import pytest
@pytest.mark.skip(reason="不想跑了")
def test_01():
print("用例1执行")
def test_02():
print("用例2执行")
@pytest.mark.skip
def test_03():
print("用例3执行")
if __name__ == '__main__':
pytest.main(['-s'])
2.有条件跳过
import pytest
def test_02():
print("用例2执行")
return True
@pytest.mark.skipif(condition= 1>2,reason="不想跑了")
def test_01():
print("用例1执行")
@pytest.mark.skip
def test_03():
print("用例3执行")
if __name__ == '__main__':
pytest.main(['-s','test_skipif.py'])
5.函数数据参数化
1.单参数
import pytest
@pytest.mark.parametrize('a',['aaa','bbb','ccc'])
def test_01(a):
print('\n'+a)
if __name__ == '__main__':
pytest.main(['-s'])
2.多参数
import pytest
@pytest.mark.parametrize('username,pwd',[('zz','123456'),('xz','123456'),('qs','123456')])
def test_01(username,pwd):
print('\n'+username)
print('\n'+pwd)
if __name__ == '__main__':
pytest.main(['-s','test_multi.py'])
3.函数式参数
import pytest
def return_data():
return [('zz','123456'),('xz','123456'),('qs','123456')]
@pytest.mark.parametrize('data',return_data())
def test_01(data):
print('\n'+data[0])
print('\n'+data[1])
if __name__ == '__main__':
pytest.main(['-s','test_func.py'])
边栏推荐
- 12. Writing rules - static mode
- 自动分账系统哪家好?
- Continuous learning / life long learning
- 区区区间---线段树lazy标记板子题
- etcd实现大规模服务治理应用实战
- Why is redis fast? Message queue, single thread
- Zone --- line segment tree lazy marking board sub problem
- 《微信小程序-进阶篇》Lin-ui组件库源码分析-Button组件(二)
- Master-slave replication and its principle
- idea配置web容器与war打包
猜你喜欢

MPEG音频编码三十年

Wechat applet - Advanced chapter Lin UI component library source code analysis button component (II)

Cloud development workers must go to work fishing and paddling wechat applet source code

DHCP protocol detailed analysis

HTTP cache

New conch movie theme template m3.1 fully decrypted version multifunctional apple cmsv10 background adaptive theme open source fully decrypted version

Cloud development pocket toolbox wechat applet source code

用于校园流浪猫信息记录和分享的小程序源码/微信云开发中大猫谱小程序源码

C语言:判断字母

PHP lucky draw system with background source code
随机推荐
Small program source code for campus stray cat information recording and sharing / wechat cloud development medium big cat spectrum small program source code
Source code and display of 18 classic programs in C language vs2019
区区区间---线段树lazy标记板子题
OSPF实验
Source code of Jiugongge heart puzzle Applet / source code of main wechat applet with traffic
童年的快乐时光
双for循环
Flink生产环境经典问题汇总
(作业)C语言:atoi和strncpy、strncat、strncmp的模拟实现
Multiple inheritance and derived class member identification
6-21漏洞利用-mysql弱口令破解
owt-server源码剖析(三)--video模块分析之Mixer In
HTB-Blocky
STM32C8T6编码器电机测速与arduino光电模块测速
C language: Little Lele and Euclid
一款好看的iapp捐赠榜单源码
第五天实验
etcd实现大规模服务治理应用实战
Deliver temperature with science and technology, vivo appears at the digital China Construction Summit
C language: hollow square pattern