前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Pytest测试用例之setup与teardown方法(二)

Pytest测试用例之setup与teardown方法(二)

作者头像
测试小兵
发布2020-07-10 15:19:22
1.6K0
发布2020-07-10 15:19:22
举报
文章被收录于专栏:猪圈子猪圈子

日常积累 | 初识Pytest | 日常积累 | 初识pytest (二) | Pytest测试用例之setup与teardown方法(一)继续分享, 今天继模块级以及函数式setup与teardown之外的2种类与方法级的写法与执行顺序

回顾

pytest框架用例运行级别

>>模块级(setup_module/teardown_module)开始于横块始末,全局的

>>函数级(setup_function/teardown_function)只对函数用例生效(不在类中)

>>类级(setup_class/teardown_calss)只在类中前后运行一次(在类中)

>>方法级(setup_method/teardown_method)开始于方法始末(在类中)

>>类里面的(setup/teardown)运行在调用方法前后

接下来我们进入今天的小猪脚类与方法的setup、teardown

01类里面的

pytest 框架类里面的前置与后置用法setup、teardown ]

以下代码是类里面的前置后置简要代码,我们一起看看他的执行顺序

# coding=utf-8
# authou:shichao
# python测试社区学习记录

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作用对类里的测试用例生效

* 类里面的在每条测试用例执行前都会去执行一次

图例01

02类级

接着我们在看看[ 类级setup_class、teardown_class前置与后置用法 ]

以下代码是类级的前置后置简要代码,我们一起看看他的执行顺序

# coding=utf-8
# authou:shichao
# python测试社区学习记录
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:所有用例执行结束之后

* 类级前置后置只打开一次就执行所有的测试用例

图例02

03方法级

接着我们在看看[ 方法级setup_method、teardown_method在一个测试用例文件里一起写前置与后置用法 看看它的执行顺序 ]

以下代码是模块级的前置后置简要代码,我们一起看看他的执行顺序

# coding=utf-8
# authou:shichao
# python测试社区学习记录

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:每个用例结束后执行

* 方法级的在每条测试用例执行前都会去执行一次

图例03

04类级+方法级+类里面的

接着我们在看看[ 类级+方法级setup、teardown、setup_class、teardown_class、setup_method、teardown_method在一个测试用例文件里一起写前置与后置用法 看看它的执行顺序 ]

以下代码是类级+模块级的前置后置简要代码,我们一起看看他的执行顺序

# coding=utf-8
# authou:shichao
# python测试社区学习记录

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

图例04

以上就是今天给大家介绍的pytest前置后置[ 类级以及方法级] 的用法以及在实际代码中他们的执行优先级,小小的顺序结构可能会影响你这条case是否执行通过,希望本次分享对大家有所帮助

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-07-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python测试社区 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档