pytest(四)--测试用例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)运行在调用方法的前后

函数式

setup_function/teardown_function

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

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

#-*- coding=utf-8 -*-
import pytest
#函数式
def setup_function():
    print("setup_function:每个用例开始前都会执行")
def teardown_function():
    print("teardown_function:每个用例结束后都会执行")
def test_onea():
    print("正在执行--test_onea")
    x="xixi"
    assert 'i' in x
def test_twob():
    print("正在执行--test_twob")
    x="hua"
    assert hasattr(x,"check")
def test_threec():
    print("正在执行--test_threec")
    a="hello"
    b="hello world"
    assert a in b
if __name__=="__main__":
    pytest.main(["-s","test_f1.py"])

 运行结果:

============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: E:\study\python2020collected 3 items

test_f1.py setup_function:每个用例开始前都会执行
.正在执行--test_onea
teardown_function:每个用例结束后都会执行
setup_function:每个用例开始前都会执行
F正在执行--test_twob

test_f1.py:11 (test_twob)
def test_twob():
        print("正在执行--test_twob")
        x="hua"
>       assert hasattr(x,"check")
E       AssertionError: assert False
E        +  where False = hasattr('hua', 'check')

test_f1.py:15: AssertionError
teardown_function:每个用例结束后都会执行
setup_function:每个用例开始前都会执行
.正在执行--test_threec
teardown_function:每个用例结束后都会执行
                                                           [100%]

================================== FAILURES ===================================
__________________________________ test_twob __________________________________

    def test_twob():
        print("正在执行--test_twob")
        x="hua"
>       assert hasattr(x,"check")
E       AssertionError: assert False
E        +  where False = hasattr('hua', 'check')

test_f1.py:15: AssertionError
---------------------------- Captured stdout setup ----------------------------
setup_function:每个用例开始前都会执行
---------------------------- Captured stdout call -----------------------------
正在执行--test_twob
-------------------------- Captured stdout teardown ---------------------------
teardown_function:每个用例结束后都会执行
=========================== short test summary info ===========================
FAILED test_f1.py::test_twob - AssertionError: assert False
========================= 1 failed, 2 passed in 0.03s =========================

 2.从结果可以看出用例执行顺序:setup_function》用例1》teardown_function,setup_function》用例2》teardown_function,setup_function》用例3》teardown_function

备注:-s参数是为了显示用例的打印信息。-q参数只显示结果,不显示过程;pytest -h就可以查看不同参数的作用。

setup_module/teardown_module

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

#-*- coding=utf-8 -*-
import pytest
#函数式
def setup_module():
    print("setup_module:整个.py模块只执行一次")
    print("比如:所有用例开始前只打开一次浏览器")
def teardown_module():
    print("teardown_module:整个.py模块只执行一次")
    print("比如:所有用例结束只最后关闭浏览器")
def test_onea():
    print("正在执行--test_onea")
    x="xixi"
    assert 'i' in x
def test_twob():
    print("正在执行--test_twob")
    x="hua"
    assert hasattr(x,"check")
def test_threec():
    print("正在执行--test_threec")
    a="hello"
    b="hello world"
    assert a in b
if __name__=="__main__":
    pytest.main(["-q","test_f1.py"])

2.从运行结果可以看到setup_module和teardown_module只执行了一次

 

test_f1.py setup_module:整个.py模块只执行一次
比如:所有用例开始前只打开一次浏览器
.正在执行--test_onea
F正在执行--test_twob

test_f1.py:13 (test_twob)
def test_twob():
        print("正在执行--test_twob")
        x="hua"
>       assert hasattr(x,"check")
E       AssertionError: assert False
E        +  where False = hasattr('hua', 'check')

test_f1.py:17: AssertionError
.正在执行--test_threec
teardown_module:整个.py模块只执行一次
比如:所有用例结束只最后关闭浏览器

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

类和方法

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

#-*- coding=utf-8 -*-
import pytest
#test_f1.py 类和方法
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_onea(self):
        print("正在执行--test_onea")
        x = "meili"
        assert "li" in x
    def test_twob(self):
        print("正在执行--test_twob")
        x="he"
        assert hasattr(x,"check")
    def test_threec(self):
        print("正在执行--test_threec")
        x="hua"
        assert 'hu' in x
if __name__=="__main__":
    pytest.main(["-s",test_f1.py]) 

运行结果

============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: E:\study\python2020collected 3 items

test_f1.py setup_class:所有用例执行之前执行一次
setup_method:每个用例开始前执行
setup:每个用例开始前执行
.正在执行--test_onea
teardown:每个用例结束后执行
teardown_method:每个用例结束后执行
setup_method:每个用例开始前执行
setup:每个用例开始前执行
F正在执行--test_twob

test_f1.py:20 (TestCase.test_twob)
self = <test_f1.TestCase object at 0x000001C60F375EB8>

    def test_twob(self):
        print("正在执行--test_twob")
        x="he"
>       assert hasattr(x,"check")
E       AssertionError: assert False
E        +  where False = hasattr('he', 'check')

test_f1.py:24: AssertionError
teardown:每个用例结束后执行
teardown_method:每个用例结束后执行
setup_method:每个用例开始前执行
setup:每个用例开始前执行
.正在执行--test_threec
teardown:每个用例结束后执行
teardown_method:每个用例结束后执行
teardown_class:所有用例执行后执行一次

2.从结果看出,运行优先级别:setup_class》setup_method》setup》用例》teardown》teardown_method》teardown_class 

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

函数和类混合

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

 

#-*- coding=utf-8 -*-
#test_f1.py
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 setup_class():
    print("setup_class:类前一次") #定义在类外面,不报错,也不会被执行
def teardown_class():
    print("teardown_class:类后一次")
def test_one():
    print("正在执行--testone")
    assert 'o' in 'one'
def test_two():
    print("正在执行--testtwo")
    assert 'x' in 'xyz'
class Testc:
    def setup_method(self):
        print("setup_method每个类中方法开始前")
    def teardown_method(self):
        print("teardown_method每个类中方法结束后")
    def test_three(self):
        print("正在运行--类three")
        assert 'cs' in 'class'
    def test_four(self):
        print("正在运行--类four")
        assert 'c2' in 'c2'
if __name__=="__main__":
    pytest.main(["-s","test_f1.py"]) 

 运行结果:

test_f1.py setup_module:py脚本开始
setup_function:每个函数开始
.正在执行--testone
teardown_function:每个函数结束
setup_function:每个函数开始
.正在执行--testtwo
teardown_function:每个函数结束
setup_method每个类中方法开始前
F正在运行--类three

test_f1.py:26 (Testc.test_three)
self = <test_f1.Testc object at 0x0000020C230F37F0>

    def test_three(self):
        print("正在运行--类three")
>       assert 'cs' in 'class'
E       AssertionError: assert 'cs' in 'class'

test_f1.py:29: AssertionError
teardown_method每个类中方法结束后
setup_method每个类中方法开始前
.正在运行--类four
teardown_method每个类中方法结束后
teardown_module:py脚本结束

2.从运行结果看出,setup_module/teardown_module的优先级是最大的,然后函数里面用到的setup_function/teardown_function与类里面的setup_method/teardown_method互不干涉。定义在类外面的setup_class不执行,也不会报错。

posted on 2020-07-29 11:21  星空6  阅读(554)  评论(0编辑  收藏  举报

导航