Pytest测试用例:setup和teardown方法(二)
回顾
pytest框架用例运行级别
>>模块级(setup_module/teardown_module)开始于横块始末,全局的
>>函数级(setup_function/teardown_function)只对函数用例生效(不在类中)
>>类级(setup_class/teardown_calss)只在类中前后运行一次(在类中)
>>方法级(setup_method/teardown_method)开始于方法始末(在类中)
>>类里面的(setup/teardown)运行在调用方法前后
接下来我们进入今天的小猪脚类与方法的setup、teardown
一、类里面的
pytest 框架类里面的前置与后置用法setup、teardown ]
以下代码是类里面的前置后置简要代码,我们一起看看他的执行顺序
-
# coding=utf-8
-
# authou:shichao
-
-
import pytest
-
-
-
class Testcaselist():
-
-
# 类里面的
-
def setup(self):
-
print('setup:每个用例前开始执行')
-
-
def teardown(self):
-
print('teardown:每个用例后开始执行')
-
-
# 测试用例
-
def test_001(self):
-
print("正在执行第一条用例")
-
p = "Python"
-
assert "h" in p
-
-
def test_002(self):
-
print("正在执行第二条用例")
-
p = 'test'
-
assert 't' in p
-
-
if __name__ == '__main__':
-
pytest.main(['-s', 'test_fixtclass.py'])
以下是代码执行后控制台输出
-
Testing started at 17:29 ...
-
F:\python3\python_code\venv\Scripts\python.exe "F:\PyCharm\PyCharm Community Edition 2019.2.2\helpers\pycharm\_jb_pytest_runner.py" --path F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.py
-
Launching pytest with arguments F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.py in F:\python3\python_code\Study\API_AutoTest_Pytest
-
-
============================= test session starts =============================
-
platform win32 -- Python 3.7.4, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- F:\python3\python_code\venv\Scripts\python.exe
-
cachedir: .pytest_cache
-
rootdir: F:\python3\python_code\Study\API_AutoTest_Pytest
-
collecting ... collected 2 items
-
-
test_fixtclass.py::Testcaselist::test_001 setup:每个用例前开始执行
-
PASSED [ 50%]正在执行第一条用例
-
teardown:每个用例后开始执行
-
-
test_fixtclass.py::Testcaselist::test_002 setup:每个用例前开始执行
-
PASSED [100%]正在执行第二条用例
-
teardown:每个用例后开始执行
-
-
-
============================== 2 passed in 0.02s ==============================
-
-
Process finished with exit code 0
>>类里面的setup、teardown控制台输出解析
# 类里面的 [ 可以看到控制台输出的结果执行顺序 ]
test_fixtclass.py : test_001 >>setup:每个用例前开始执行>>PASSED>> [ 50%]正在执行第一条用例>>teardown:每个用例后开始执行
test_fixtclass.py : test_002 >>setup:每个用例前开始执行>>PASSED>> [100%]正在执行第二条用例>>teardown:每个用例后开始执行
这是我们的类里面的setup、teardown作用对类里的测试用例生效
* 类里面的在每条测试用例执行前都会去执行一次
二、类级
接着我们在看看[ 类级setup_class、teardown_class前置与后置用法 ]
以下代码是类级的前置后置简要代码,我们一起看看他的执行顺序
-
# coding=utf-8
-
# authou:shichao
-
import pytest
-
-
-
class Testcaselist():
-
print('setup_class:所有用例执行之前')
-
-
# 类级
-
def setup_class(self):
-
print('setup_class:所有用例执行之前')
-
-
def teardown_class(self):
-
print('teardown_class:所有用例执行结束之后')
-
-
# 测试用例
-
def test_001(self):
-
print("正在执行第一条用例")
-
p = "Python"
-
assert "h" in p
-
-
def test_002(self):
-
print("正在执行第二条用例")
-
p = 'test'
-
assert 't' in p
-
-
if __name__ == '__main__':
-
pytest.main(['-s', 'test_fixtclass.py'])
以下是代码执行后控制台输出
-
Testing started at 20:12 ...
-
F:\python3\python_code\venv\Scripts\python.exe "F:\PyCharm\PyCharm Community Edition 2019.2.2\helpers\pycharm\_jb_pytest_runner.py" --path F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.py
-
Launching pytest with arguments F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.py in F:\python3\python_code\Study\API_AutoTest_Pytest
-
-
============================= test session starts =============================
-
platform win32 -- Python 3.7.4, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- F:\python3\python_code\venv\Scripts\python.exe
-
cachedir: .pytest_cache
-
rootdir: F:\python3\python_code\Study\API_AutoTest_Pytest
-
collecting ... collected 2 items
-
-
test_fixtclass.py::Testcaselist::test_001 setup_class:所有用例执行之前
-
PASSED [ 50%]正在执行第一条用例
-
-
test_fixtclass.py::Testcaselist::test_002 PASSED [100%]正在执行第二条用例
-
teardown_class:所有用例执行结束之后
-
-
-
============================== 2 passed in 0.02s ==============================
-
-
Process finished with exit code 0
>>类级setup_class、teardown_class控制台输出解析
# 类级 [ 我们可以看到控制台输出的结果执行顺序 ]
>>test_fixtclass.py:Testcaselist: >>setup_class:所有用例执行之前>>test_001 >>[ 50%]正在执行第一条用例>>PASSED >>test_002>>[100%]正在执行第二条用例>>PASSED >>teardown_class:所有用例执行结束之后
* 类级前置后置只打开一次就执行所有的测试用
三、方法级
接着我们在看看[ 方法级setup_method、teardown_method在一个测试用例文件里一起写前置与后置用法 看看它的执行顺序 ]
以下代码是模块级的前置后置简要代码,我们一起看看他的执行顺序
-
# coding=utf-8
-
# authou:shichao
-
-
import pytest
-
-
-
class Testcaselist():
-
-
# 方法级
-
def setup_method(self):
-
print('setup_method:每个用例开始之前执行')
-
-
def teardown_method(self):
-
print('teardown_method:每个用例结束后执行')
-
-
# 测试用例
-
def test_001(self):
-
print("正在执行第一条用例")
-
p = "Python"
-
assert "h" in p
-
-
def test_002(self):
-
print("正在执行第二条用例")
-
p = 'test'
-
assert 't' in p
-
-
if __name__ == '__main__':
-
pytest.main(['-s', 'test_fixtclass.py'])
以下是代码执行后控制台输出
-
Testing started at 17:48 ...
-
F:\python3\python_code\venv\Scripts\python.exe "F:\PyCharm\PyCharm Community Edition 2019.2.2\helpers\pycharm\_jb_pytest_runner.py" --path F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.py
-
Launching pytest with arguments F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.py in F:\python3\python_code\Study\API_AutoTest_Pytest
-
-
============================= test session starts =============================
-
platform win32 -- Python 3.7.4, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- F:\python3\python_code\venv\Scripts\python.exe
-
cachedir: .pytest_cache
-
rootdir: F:\python3\python_code\Study\API_AutoTest_Pytest
-
collecting ... collected 2 items
-
-
test_fixtclass.py::Testcaselist::test_001 setup_method:每个用例开始之前执行
-
PASSED [ 50%]正在执行第一条用例
-
teardown_method:每个用例结束后执行
-
-
test_fixtclass.py::Testcaselist::test_002 setup_method:每个用例开始之前执行
-
PASSED [100%]正在执行第二条用例
-
teardown_method:每个用例结束后执行
-
-
-
============================== 2 passed in 0.02s ==============================
-
-
Process finished with exit code 0
>>方法级的setup_method、teardown_method控制台输出解析
# 类里面的 [ 可以看到控制台输出的结果执行顺序 ]
test_fixtclass.py : test_001 >>setup_method:每个用例开始之前执行>>PASSED>> [ 50%]正在执行第一条用例>>teardown_method:每个用例结束后执行
test_fixtclass.py : test_002 >>setup_method:每个用例开始之前执行>>PASSED>> [100%]正在执行第二条用例>>teardown_method:每个用例结束后执行
* 方法级的在每条测试用例执行前都会去执行一次
四、类级 方法级 类里面的
接着我们在看看[ 类级 方法级setup、teardown、setup_class、teardown_class、setup_method、teardown_method在一个测试用例文件里一起写前置与后置用法 看看它的执行顺序 ]
以下代码是类级 模块级的前置后置简要代码,我们一起看看他的执行顺序
-
# coding=utf-8
-
# authou:shichao
-
-
import pytest
-
-
-
class Testcaselist():
-
-
# 类里面的
-
def setup(self):
-
print('setup:每个用例前开始执行')
-
-
def teardown(self):
-
print('teardown:每个用例后开始执行')
-
-
# 类级
-
def setup_class(self):
-
print('setup_class:所有用例执行之前')
-
-
def teardown_class(self):
-
print('teardown_class:所有用例执行结束之后')
-
-
# 方法级
-
def setup_method(self):
-
print('setup_method:每个用例开始之前执行')
-
-
def teardown_method(self):
-
print('teardown_method:每个用例结束后执行')
-
-
# 测试用例
-
def test_001(self):
-
print("正在执行第一条用例")
-
p = "Python"
-
assert "h" in p
-
-
def test_002(self):
-
print("正在执行第二条用例")
-
p = 'test'
-
assert 't' in p
-
-
if __name__ == '__main__':
-
pytest.main(['-s', 'test_fixtclass.py'])
以下是代码执行后控制台输出
-
Testing started at 17:57 ...
-
F:\python3\python_code\venv\Scripts\python.exe "F:\PyCharm\PyCharm Community Edition 2019.2.2\helpers\pycharm\_jb_pytest_runner.py" --path F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.py
-
Launching pytest with arguments F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.py in F:\python3\python_code\Study\API_AutoTest_Pytest
-
-
============================= test session starts =============================
-
platform win32 -- Python 3.7.4, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- F:\python3\python_code\venv\Scripts\python.exe
-
cachedir: .pytest_cache
-
rootdir: F:\python3\python_code\Study\API_AutoTest_Pytest
-
collecting ... collected 2 items
-
-
test_fixtclass.py::Testcaselist::test_001 setup_class:所有用例执行之前
-
setup_method:每个用例开始之前执行
-
setup:每个用例前开始执行
-
PASSED [ 50%]正在执行第一条用例
-
teardown:每个用例后开始执行
-
teardown_method:每个用例结束后执行
-
-
test_fixtclass.py::Testcaselist::test_002 setup_method:每个用例开始之前执行
-
setup:每个用例前开始执行
-
PASSED [100%]正在执行第二条用例
-
teardown:每个用例后开始执行
-
teardown_method:每个用例结束后执行
-
teardown_class:所有用例执行结束之后
-
-
-
============================== 2 passed in 0.02s ==============================
-
-
Process finished with exit code 0
-
>>类里面的 类级 方法级前置后置控制台输出解析
-
-
# 类里面的 类级 方法级 [ 我们可以看到控制台输出的结果执行顺序 ]
-
-
>>test_fixtclass.py::Testcaselist:setup_class:所有用例执行之前>>比如:所有用例开始前只打开一次浏览器
-
-
setup:每个用例前开始执行>>setup_method:每个用例开始之前执行>>test_001>>[ 50%]正在执行第一条用例>>PASSED >>teardown:每个用例后开始执行>>teardown_method:每个用例结束后执行
-
-
setup:每个用例前开始执行>>setup_method:每个用例开始之前执行>>test_002>>[ 50%]正在执行第一条用例>>PASSED >>teardown:每个用例后开始执行>>teardown_method:每个用例结束后执行
-
-
>>teardown_class:所有用例执行结束之后>>比如:所有用例结束只最后关闭浏览器
-
-
从结果看出,运行的优先级:setup_class>>setup_method>
-
-
setup >用例>teardown>teardown_method>>teardown_class
B站讲的最详细的Python接口自动化测试实战教程全集(实战最新版)
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfikebe
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
excel下划线不显示怎么办
PHP中文网 06-23 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
photoshop蒙版画笔没反应怎么办
PHP中文网 06-24