pytest框架笔记(二) : 测试用例setup和teardown

  • Post author:
  • Post category:其他


一、用例运行级别

1、模块级:

开始于模块始末,全局的

setup_module:模块开始时执行

teardown_module:模块结束时执行

2、函数级

只对函数用例生效(不在类中)

setup_function:函数开始时执行

teardown_function:函数结束时执行

3、方法级

开始于方法始末(在类中)

setup_method:方法开始时执行

teardown_method:方法结束时执行

4、类里面的(setup/teardown)

运行在调用方法的前后,这个和unittest的setup/teardown作用相同

二、函数式

1、setup_function / teardown_function

pytest框架支持函数和类两种用例方式,先看函数里面的前置与后置用法:

1.1、setup_function / teardown_function 每个用例开始和结束调用一次

#!encoding=utf-8
import pytest


def setup_function():
    print('setup_function:每个用例开始前都会执行')

def teardown_function():
    print('teardown_function:每个用例结束后都会执行')

def test_one():
    x="this"
    assert 'h' in x

def test_two():
    x='hello'
    assert hasattr(x,'check')

def test_three():
    a='hello'
    b='hello world'
    assert a in b

if __name__=="__main__":
    pytest.main('-s test_class002.py')

运行结果:

setup_function:每个用例开始前都会执行
.teardown_function:每个用例结束后都会执行
setup_function:每个用例开始前都会执行
F
test_class002.py:14 (test_two)
def test_two():
        x='hello'
>       assert hasattr(x,'check')
E       AssertionError: assert False
E        +  where False = hasattr('hello', 'check')

test_class002.py:17: AssertionError
teardown_function:每个用例结束后都会执行
setup_function:每个用例开始前都会执行
.teardown_function:每个用例结束后都会执行
                                                     [100%]

=================================== FAILURES ===================================
___________________________________ test_two ___________________________________

    def test_two():
        x='hello'
>       assert hasattr(x,'check')
E       AssertionError: assert False
E        +  where False = hasattr('hello', 'check')

test_class002.py:17: AssertionError
---------------------------- Captured stdout setup -----------------------------
setup_function:每个用例开始前都会执行
--------------------------- Captured stdout teardown ---------------------------
teardown_function:每个用例结束后都会执行
========================= 1 failed, 2 passed in 0.16s ==========================

1.2、从结果可以看出用例执行顺序:

setup_function–>用例1–>teardown_function

setup_function–>用例2–>teardown_function

setup_function–>用例3–>teardown_function


备注:-s参数是为了显示用例的打印信息,-q参数只显示结果,不显示过程

2、setup_module / teardown_module

2.1、setup_module是所有用例开始前只执行一次,teardown_module是所有用例结束后只执行一次

#!encoding=utf-8
import pytest


def setup_module():
    print('setup_module:整个.py模型只执行一次')

def teardown_module():
    print('teardown_module:整个.py模型只执行一次')

def setup_function():
    print('setup_function:每个用例开始前都会执行')

def teardown_function():
    print('teardown_function:每个用例结束后都会执行')

def test_one():
    x="this"
    assert 'h' in x

def test_two():
    x='hello'
    assert hasattr(x,'check')

def test_three():
    a='hello'
    b='hello world'
    assert a in b

if __name__=="__main__":
    pytest.main('-s test_class002.py')

2.2、从运行结果可以看到setup_module和teardown_module执行了一次

setup_module:整个.py模型只执行一次
setup_function:每个用例开始前都会执行
.teardown_function:每个用例结束后都会执行
setup_function:每个用例开始前都会执行
F
test_class002.py:20 (test_two)
def test_two():
        x='hello'
>       assert hasattr(x,'check')
E       AssertionError: assert False
E        +  where False = hasattr('hello', 'check')

test_class002.py:23: AssertionError
teardown_function:每个用例结束后都会执行
setup_function:每个用例开始前都会执行
.teardown_function:每个用例结束后都会执行
teardown_module:整个.py模型只执行一次
                                                     [100%]

=================================== FAILURES ===================================
___________________________________ test_two ___________________________________

    def test_two():
        x='hello'
>       assert hasattr(x,'check')
E       AssertionError: assert False
E        +  where False = hasattr('hello', 'check')

test_class002.py:23: AssertionError
---------------------------- Captured stdout setup -----------------------------
setup_function:每个用例开始前都会执行
--------------------------- Captured stdout teardown ---------------------------
teardown_function:每个用例结束后都会执行
========================= 1 failed, 2 passed in 0.07s ==========================


备注:setup_fuction / teardown_function和setup_module / teardown_module 这四种方法是可以任意组合的,用一个和多个都可以

三、类和方法

1、setup / teardown和unittest里面的setup / teardown是一样的功能,setup_class和teardown_class等价于unittest里面的setupClass和teardownClass

#!encoding=utf-8
import pytest

class TestCase:
    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_one(self):
        x="this"
        assert 'h' in x

    def test_three(self):
        a='hello'
        b='hello world'
        assert a in b

if __name__=="__main__":
    pytest.main('-s test_class002.py')

运行结果

Testing started at 10:04 PM ...
/usr/local/bin/python3.6 /Applications/PyCharm.app/Contents/helpers/pycharm/_jb_pytest_runner.py --path /Users/luozelin/Desktop/pytest/pytest_demo/test_class002.py
Launching py.test with arguments /Users/luozelin/Desktop/pytest/pytest_demo/test_class002.py in /Users/luozelin/Desktop/pytest/pytest_demo

============================= test session starts ==============================
platform darwin -- Python 3.6.3, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: /Users/luozelin/Desktop/pytest/pytest_demo
plugins: html-1.21.1, rerunfailures-7.0, metadata-1.8.0collected 2 items

test_class002.py                                                       [100%]

============================== 2 passed in 0.02s ===============================setup_class:所有用例执行之前执行
setup_method:每个用例开始前执行
setup:每个用例开始前执行
.teardown:每个用例结束后执行
teardown_method:每个用例结束后执行
setup_method:每个用例开始前执行
setup:每个用例开始前执行
.teardown:每个用例结束后执行
teardown_method:每个用例结束后执行
teardown_class:所有用例执行之后执行

Process finished with exit code 0

2、从结果可以看出,运行的优先级如下:

setup_class–>setup_method–>setup–>用例–>teardown–>teardown_method–>teardown_class


备注:这里setup_method和teardown_method的功能和setup / teardown的功能是一样的,一般二者用其一即可

四、函数和类混合

1、如果一个.py文件里面既有函数用例又有类和方法用例,运行顺序是怎样的?

#!encoding=utf-8
import pytest

def setup_module():
    print('setup_module:整个.py模块只执行一次')

def teardown_module():
    print('teardown_module:整个.py模块只执行一次')

def setup_function():
    print('setup_function:每个用例开始前都会执行')

def teardown_function():
    print('teardown_function:每个用例结束后都会执行')

def test_one():
    x='this'
    assert 'h' in x

def test_two():
    x='hello'
    assert hasattr(x,'check')

class TestCase:
    def setup_class(self):
        print('setup_class:所有用例执行之前执行')

    def teardown_class(self):
        print('teardown_class:所有用例执行之后执行')

    def test_three(self):
        x='this'
        assert 'h' in x

    def test_four(self):
        x='hello'
        assert hasattr(x,'check')

if __name__=="__main__":
    pytest.main('-s test_class003.py')

运行结果:

setup_module:整个.py模块只执行一次
setup_function:每个用例开始前都会执行
.teardown_function:每个用例结束后都会执行
setup_function:每个用例开始前都会执行
F
test_class003.py:19 (test_two)
def test_two():
        x='hello'
>       assert hasattr(x,'check')
E       AssertionError: assert False
E        +  where False = hasattr('hello', 'check')

test_class003.py:22: AssertionError
teardown_function:每个用例结束后都会执行
setup_class:所有用例执行之前执行
.F
test_class003.py:34 (TestCase.test_four)
self = <test_class003.TestCase object at 0x112a37710>

    def test_four(self):
        x='hello'
>       assert hasattr(x,'check')
E       AssertionError: assert False
E        +  where False = hasattr('hello', 'check')

test_class003.py:37: AssertionError
teardown_class:所有用例执行之后执行
teardown_module:整个.py模块只执行一次
                                                    [100%]

=================================== FAILURES ===================================
___________________________________ test_two ___________________________________

    def test_two():
        x='hello'
>       assert hasattr(x,'check')
E       AssertionError: assert False
E        +  where False = hasattr('hello', 'check')

test_class003.py:22: AssertionError
---------------------------- Captured stdout setup -----------------------------
setup_function:每个用例开始前都会执行
--------------------------- Captured stdout teardown ---------------------------
teardown_function:每个用例结束后都会执行
______________________________ TestCase.test_four ______________________________

self = <test_class003.TestCase object at 0x112a37710>

    def test_four(self):
        x='hello'
>       assert hasattr(x,'check')
E       AssertionError: assert False
E        +  where False = hasattr('hello', 'check')

test_class003.py:37: AssertionError
--------------------------- Captured stdout teardown ---------------------------
teardown_class:所有用例执行之后执行
teardown_module:整个.py模块只执行一次
========================= 2 failed, 2 passed in 0.08s ==========================

2、从运行结果看出,setup_module / teardown_module的优先级是最大的,然后函数里面用到的setup_function / teardown_function与类里面的setup_class / teardown_class互不干涉



版权声明:本文为BearStarX原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。