pytest--前置后置执行

前言

在unittest中就有前置setup和后置teardown来处理测试用例执行前的准备工作(浏览器驱动实例化,数据库连接等)以及执行后的处理工作(清理数据,关闭浏览器驱动,关闭数据库连接等),那么pytest同样也提供了前置后置的方法来满足这个需求

pytest前置后置方法

pytest提供了以下5个前置后置方法:

  • setup、teardown:每条用例都会执行,既可以在类中使用,也可以在类外使用
  • setup_class、teardown_class:类中的测试用例执行前后只执行一次
  • setup_method、teardown_method:类中的每条测试用例执行前后都执行一次
  • setup_function、teardown_function:类外的每条测试用例执行前后都执行一次
  • setup_module、teardown_module:类外的测试用例执行前后只执行一次

以下来具体写代码来看执行结果情况:

1、setup、teardown:每条用例都会执行,既可以在类中使用,也可以在类外使用

复制代码
import pytest

class
Test_04: def setup(self): print('setup前置执行') def teardown(self): print('teardown后置执行') def test_01(self): print('用例01执行') def test_02(self): print('用例02执行') if __name__ == '__main__': pytest.main()
复制代码

 执行结果如下:

 

可以看到Test_04测试类中的test_01和test_02两个测试用例执行前后,都有setup和teardown执行的打印

 

2、setup_class、teardown_class:类中的测试用例执行前后只执行一次

复制代码
import pytest

class Test_04:

    def setup_class(self):
        print('setup_class前置执行')
    def teardown_class(self):
        print('teardown_class后置执行')

    def test_01(self):
        print('用例01执行')
    def test_02(self):
        print('用例02执行')


if __name__ == '__main__':
    pytest.main()
复制代码

 

 输出结果:

 

 可以看到Test_04测试类中test_01用例执行前执行了一次前置setup_class,test_02用例执行完成后执行了一次后置teardown_class

 

 3、setup_method、teardown_method:类中的每条测试用例执行前后都执行一次

复制代码
import pytest

class Test_04:
    def setup_method(self):
        print('setup_method前置执行')
    def teardown_method(self):
        print('teardown_method后置执行')

    def test_01(self):
        print('用例01执行')
    def test_02(self):
        print('用例02执行')

if __name__ == '__main__':
    pytest.main()
复制代码

 输出结果:

 

 类中的每条测试用例前后都执行了前置、后置对应的打印

 

4、setup_function、teardown_function:类外的每条测试用例执行前后都执行一次

复制代码
import pytest

class Test_04:
    def test_01(self):
        print('用例01执行')
    def test_02(self):
        print('用例02执行')

def setup_function():
    print('setup_function前置执行')
def teardown_function():
    print('teardown_function后置执行')

def test_03():
    print('类外用例03执行')
def test_04():
    print('类外用例04执行')

if __name__ == '__main__':
    pytest.main()
复制代码

 

执行结果:

 

 

 可以看到测试类外的tes03、test04都执行了前后置的打印,测试类中的test_01、test_02都没有前后置打印

 

5、setup_module、teardown_module:类外的测试用例执行前后只执行一次

复制代码
import pytest

class Test_04:
    def test_01(self):
        print('用例01执行')
    def test_02(self):
        print('用例02执行')


def test_03():
    print('类外用例03执行')
def test_04():
    print('类外用例04执行')

if __name__ == '__main__':
    pytest.main()
复制代码

 

输出结果:

 可以看到测试类外的测试用例test_03、test_04执行前后,前后置打印都已执行

 

前、后置执行顺序

测试类中前、后置执行顺序:

复制代码
import pytest

class Test_04:
    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_01(self):
        print('用例01执行')
    def test_02(self):
        print('用例02执行')


if __name__ == '__main__':
    pytest.main()
复制代码

 输出结果:

 

 从打印顺序可以看到,前置执行顺序:setup_class > setup_method > setup (后置执行顺序则相反)

 

测试类外前、后置执行顺序:

复制代码
import pytest

def setup():
    print('类外setup前置执行')
def teardown():
    print('类外teardown后置执行')
def setup_function():
    print('setup_function前置执行')
def teardown_function():
    print('teardown_function后置执行')
def setup_module():
    print('setup_module前置执行')
def teardown_module():
    print('teardown_module后置执行')

def test_03():
    print('类外用例03执行')
def test_04():
    print('类外用例04执行')

if __name__ == '__main__':
    pytest.main()
复制代码

输出结果:

 

  从打印顺序可以看到,前置执行顺序:setup_module > setup_function > setup (后置执行顺序则相反)

 

总结:

1、测试类中:setup_class、teardown_class

                        setup_method、teardown_method

      测试类外:setup_function、teardown_function

                        setup_module、teardown_module

      测试类中、类外均可:setup、teardown

2、执行顺序:

      类中前置执行顺序:setup_class > setup_method > setup (后置执行顺序则相反)

      类外前置执行顺序:setup_module > setup_function > setup (后置执行顺序则相反)

posted @   测试-13  阅读(1560)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示