1.命名规范
import pytest
def testCase():
print("discover目录下的测试用例testCase")
if __name__ == '__main__':
pytest.main(['-s'])
def test02():
print("doc目录下的用例test02")
class TestDemo:
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 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(['-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(['-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(['--reruns', '3', '--reruns-delay','2','test_rerun.py'])
import pytest
def multiply(a,b):
return a * b
"""
不含有类的用例预置和后置函数
第一批次: 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")
class TestDemo:
def test_case_03(self):
print("类下的用例")
3.Pytest配置文件
pytest.ini
[pytest]
addopts = -s -v --html=./report/html_report.html
testpaths = ./doc
python_files = auto*.py
python_classes = A* B*
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'])
import pytest
def return_data():
return [[1,2,3,4,5],[2,2,2,2,2],[4,4,4,4,4]]
@pytest.mark.parametrize('data',return_data())
def test_01(data):
print(data)
print(type(data))
print(data[0])
print(data[1])
print(data[2])
if __name__ == '__main__':
pytest.main(['-s','test_func02.py'])