pytest学习4-测试用例setup 和teardown
前言:
学过unittest 的都知道里面有前置和后置 方法 setup和teardown非常好用,再每次用例开始前和结束后都去执行一次。
当然还有更高级一点的setupClass 和teardownClass,需要配合 @classmethod 装饰器一起使用,在做selenium自动化的时候,它的效率尤为突出,可以只启动一次浏览器执行多个用例。
pytest框架也有类似于setup和teardown的语法,并且不止这四个。
用例运行级别:
-模块级别 setup_module /teardown_module 开始于模块始末,全局的
-函数级 setup_function / teardown_function 只对函数用例生效 (不在类中)
-类级 setup_class /teardown_class 只在类中前后运行一次(在类中)
-方法级 setup_method / teardown_method 开始于方法始末(在类中)
-类里面的 setup /teardown 运行在调用方法的前后
下面对这几个方法分别讲解:
1、setup_function/ teardown_function
# !/usr/bin/env python
# -*-coding:utf-8 -*-
"""
# File : test_setup.py
# Time :2021/11/16 20:47
# Author :author Kong_hua_sheng_25304
# version :python 3.8
# Description:
"""
import pytest
def setup_function():
print("\n setup function : 每个函数用例开始前执行此方法")
def teardown_function():
print("\n teardown function: 每个函数用例结束后执行此方法")
def test_one():
print(" 正在执行----testa_one")
x = 'this'
assert 'h' in 'this'
def test_two():
print("正在执行----test_two")
x = 'hello'
assert hasattr(x, 'check')
def test_three():
print("正在执行---test three")
a = 'hello'
b = 'hello world'
assert a in b
if __name__ == '__main__':
pytest.main(['-s', 'test_setup.py'])
执行结果:
============================= test session starts =============================
collecting ... collected 3 items
test_setup.py::test_one
setup function : 每个函数用例开始前执行此方法
PASSED [ 33%] 正在执行----testa_one
teardown function: 每个函数用例结束后执行此方法
test_setup.py::test_two
setup function : 每个函数用例开始前执行此方法
FAILED [ 66%]正在执行----test_two
test_setup.py:26 (test_two)
def test_two():
print("正在执行----test_two")
x = 'hello'
> assert hasattr(x, 'check')
E AssertionError: assert False
E + where False = hasattr('hello', 'check')
test_setup.py:30: AssertionError
teardown function: 每个函数用例结束后执行此方法
test_setup.py::test_three
setup function : 每个函数用例开始前执行此方法
PASSED [100%]正在执行---test three
teardown function: 每个函数用例结束后执行此方法
========================= 1 failed, 2 passed in 0.12s =========================
Process finished with exit code 1
发现执行顺序: 先执行 setup_function --》再执行 用例 1--》 结束后执行teardown_function
先执行 setup_function --》再执行 用例 2--》 结束后执行teardown_function
先执行 setup_function --》再执行 用例 3--》 结束后执行teardown_function
备注:-s 参数是为了显示用例的打印信息。-q 参数只显示打印结果,不显示打印过程。
2、setup_module/teardown_module
setup_module是所有用例开始前只执行一次,teardown_module 是所有用例结束后只执行一次
# !/usr/bin/env python
# -*-coding:utf-8 -*-
"""
# File : test_setup.py
# Time :2021/11/16 20:47
# Author :author Kong_hua_sheng_25304
# version :python 3.8
# Description:
"""
import pytest
def setup_module():
print("\n setup module: 整个.py 模块开始只执行一次")
print("比如 所有用例开始前 只打开一次浏览器")
def teardown_module():
print("\n teardown_module : 整个.py模块结束 只执行一次")
print("比如:所有用例结束,最后关闭浏览器")
def setup_function():
print("\n setup function : 每个函数用例开始前执行此方法")
def teardown_function():
print("\n teardown function: 每个函数用例结束后执行此方法")
def test_one():
print(" 正在执行----testa_one")
x = 'this'
assert 'h' in 'this'
def test_two():
print("正在执行----test_two")
x = 'hello'
assert hasattr(x, 'check')
def test_three():
print("正在执行---test three")
a = 'hello'
b = 'hello world'
assert a in b
if __name__ == '__main__':
pytest.main(['-s', 'test_setup.py'])
从运行结果可以看出setup_module teardown_module 只执行了一次,运行结果如下:
============================ test session starts =============================
collecting ... collected 3 items
test_setupfunction.py::test_one
setup module: 整个.py 模块开始只执行一次
比如 所有用例开始前 只打开一次浏览器
setup function : 每个函数用例开始前执行此方法
PASSED [ 33%] 正在执行----testa_one
teardown function: 每个函数用例结束后执行此方法
test_setupfunction.py::test_two
setup function : 每个函数用例开始前执行此方法
FAILED [ 66%]正在执行----test_two
test_setupfunction.py:36 (test_two)
def test_two():
print("正在执行----test_two")
x = 'hello'
> assert hasattr(x, 'check')
E AssertionError: assert False
E + where False = hasattr('hello', 'check')
test_setupfunction.py:40: AssertionError
teardown function: 每个函数用例结束后执行此方法
test_setupfunction.py::test_three
setup function : 每个函数用例开始前执行此方法
PASSED [100%]正在执行---test three
teardown function: 每个函数用例结束后执行此方法
teardown_module : 整个.py模块结束 只执行一次
比如:所有用例结束,最后关闭浏览器
========================= 1 failed, 2 passed in 0.11s =========================
Process finished with exit code 1
备注: setup_function \teardown_function setup_module\teardown_module 这四种方法是可以任意组合的,用一个和多个都可以
类和方法:
1、setup\teardown 和unittest 里面的setup\teardown 是一样的功能,setup_class 和teardown_class 等价于unittest 里面的 setupClass 和 teardownClass
D:\Python\Python38\python.exe "D:\JetBrains\PyCharm Community Edition 2021.1.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --path E:/pythonProject1/learn_project/pytest_learn/test_setupmodule.py
Testing started at 10:06 ...
Launching pytest with arguments E:/pythonProject1/learn_project/pytest_learn/test_setupmodule.py --no-header --no-summary -q in E:\pythonProject1\learn_project\pytest_learn
============================= test session starts =============================
collecting ... collected 3 items
test_setupmodule.py::TestCase::test_one
test_setupmodule.py::TestCase::test_two
setup_class , 每次类中的所有用例执行前 执行一次
setup_method 每个用力执行前 执行一次
setup 每个用例执行前 执行一次
PASSED [ 33%] 正在执行----testa_one
teardown 每个用力执行后 执行一次
teardown_method 每个用例执行后 执行一次
setup_method 每个用力执行前 执行一次
setup 每个用例执行前 执行一次
FAILED [ 66%]正在执行----test_two
test_setupmodule.py:38 (TestCase.test_two)
self = <test_setupmodule.TestCase object at 0x0000024D163BA670>
def test_two(self):
print("正在执行----test_two")
x = 'hello'
> assert hasattr(x, 'check')
E AssertionError: assert False
E + where False = hasattr('hello', 'check')
test_setupmodule.py:42: AssertionError
teardown 每个用力执行后 执行一次
teardown_method 每个用例执行后 执行一次
setup_method 每个用力执行前 执行一次
setup 每个用例执行前 执行一次
PASSED [100%]正在执行---test three
teardown 每个用力执行后 执行一次
teardown_method 每个用例执行后 执行一次
teardown_class, 每次类中的所有用力执行后 执行一次
test_setupmodule.py::TestCase::test_three
========================= 1 failed, 2 passed in 0.12s =========================
Process finished with exit code 1
运行结果:
============================= test session starts =============================
collecting ... collected 3 items
test_setupmodule.py::TestCase::test_one
test_setupmodule.py::TestCase::test_two
setup_class , 每次类中的所有用例执行前 执行一次
setup_method 每个用力执行前 执行一次
setup 每个用例执行前 执行一次
PASSED [ 33%] 正在执行----testa_one
teardown 每个用力执行后 执行一次
teardown_method 每个用例执行后 执行一次
setup_method 每个用力执行前 执行一次
setup 每个用例执行前 执行一次
FAILED [ 66%]正在执行----test_two
test_setupmodule.py:38 (TestCase.test_two)
self = <test_setupmodule.TestCase object at 0x0000024D163BA670>
def test_two(self):
print("正在执行----test_two")
x = 'hello'
> assert hasattr(x, 'check')
E AssertionError: assert False
E + where False = hasattr('hello', 'check')
test_setupmodule.py:42: AssertionError
teardown 每个用力执行后 执行一次
teardown_method 每个用例执行后 执行一次
setup_method 每个用力执行前 执行一次
setup 每个用例执行前 执行一次
PASSED [100%]正在执行---test three
teardown 每个用力执行后 执行一次
teardown_method 每个用例执行后 执行一次
teardown_class, 每次类中的所有用力执行后 执行一次
从执行结果可以看出:运行的优先级:setup_class > setup_method>setup > 用例执行>teardown>teardown_method>teardown_class
一般: 这里setup_method \ teardown_method 的功能和 setup / teardown 功能 是一样的,一般二者用其中一个,一般使用 setup\teardown
函数 和 类混合
1、如果一个.py 的文件里面 既有函数用例又有类和方法用例,运行的顺序又是什么呢??????
# !/usr/bin/env python
# -*-coding:utf-8 -*-
"""
# File : test_hunhe.py
# Time :2021/11/18 10:20
# Author :author Kong_hua_sheng_25304
# version :python 3.8
# Description:
"""
import pytest
def setup_module():
print("\n setup_module: 整个.py模块执行前执行一次")
pass
def teardown_module():
print("teardown_module: 整个.py 模块执行后 执行一次")
pass
def setup_function():
print("setup_function: 每个用力开始前都会执行")
pass
def teardown_function():
print("teardown_function: 每个用例结束后 都会执行")
pass
def test_one():
print("正在执行----test_one")
x = "hello"
assert 'h' in x
pass
def test_two():
print("正在执行 test_two")
x = 'this'
assert hasattr(x, 'is')
pass
class TestCase:
def setup(self):
print(" setup : 类中每个用例执行前都会执行")
pass
def teardown(self):
print("teardown:类中每个用例执行结束后 都会执行")
pass
def setup_class(self):
print("setup_class: 类开始前执行一次")
pass
def teardown_class(self):
print("teardown_class: 类结束后 执行一次")
pass
def test_three(self):
print("执行测试用例 ----test_three")
x = 'hello'
assert 'h' in x
pass
def test_four(self):
print("执行测试用例---test_four")
x = 'this'
assert hasattr(x, 'is')
pass
if __name__ == '__main__':
pytest.main(['-s','-v'])
执行结果如下:
============================= test session starts =============================
collecting ... collected 4 items
test_hunhe.py::test_one
setup_module: 整个.py模块执行前执行一次
setup_function: 每个用力开始前都会执行
PASSED [ 25%]正在执行----test_one
teardown_function: 每个用例结束后 都会执行
test_hunhe.py::test_two setup_function: 每个用力开始前都会执行
FAILED [ 50%]正在执行 test_two
test_hunhe.py:39 (test_two)
def test_two():
print("正在执行 test_two")
x = 'this'
> assert hasattr(x, 'is')
E AssertionError: assert False
E + where False = hasattr('this', 'is')
test_hunhe.py:43: AssertionError
teardown_function: 每个用例结束后 都会执行
test_hunhe.py::TestCase::test_three
test_hunhe.py::TestCase::test_four setup_class: 类开始前执行一次
setup : 类中每个用例执行前都会执行
PASSED [ 75%]执行测试用例 ----test_three
teardown:类中每个用例执行结束后 都会执行
setup : 类中每个用例执行前都会执行
FAILED [100%]执行测试用例---test_four
test_hunhe.py:69 (TestCase.test_four)
self = <test_hunhe.TestCase object at 0x000001B8ABD6AFD0>
def test_four(self):
print("执行测试用例---test_four")
x = 'this'
> assert hasattr(x, 'is')
E AssertionError: assert False
E + where False = hasattr('this', 'is')
test_hunhe.py:73: AssertionError
teardown:类中每个用例执行结束后 都会执行
teardown_class: 类结束后 执行一次
teardown_module: 整个.py 模块执行后 执行一次
从运行结果看出:setup_module/teardown_module 的优先级是最大的,然后函数里面用到的setup_function/teardown_function 和类中的setup_class / teardown_class 互不干涉

浙公网安备 33010602011771号