Pytest学习三 - setup和teardown的使用

一、前言

从文章标题可以看出,就是初始化和释放的操作,根据我的java习惯来学习pytest,个人感觉没差太多,理解上也不是很难。

哦,对了,差点跑题了,这个框架是基于Python语言的,在学习的时候难免总会用java的类比思想来学习,下面言归正传哈。

我们还从 unittest与pytest来对比学习吧

二、unittest用法

unittest有两个前置方法,两个后置方法,分别是:

  • setup()
  • setupClass()
  • teardown()
  • teardownClass()

个人始终觉得unittest和Junit像极了。

三、pytest用法

当然,Pytest也提供了类似setup、teardown的方法,分别是:

  • 模块级别:setup_module、teardown_module
  • 函数级别:setup_function、teardown_function,不在类中的方法
  • 类级别:setup_class、teardown_class
  • 方法级别:setup_method、teardown_method
  • 方法细化级别:setup、teardown

我总感觉学习pytest像是在学习testng一样,难道是我的错觉吗,啊啊啊啊,不能吧。

四、unittest示例

unittest的setupClass和teardownClass,需要配合@classmethod装饰器一起使用,也就是我们java说的注解呀,这块是翻译给java学Python的同学的,可忽略哈。
示例代码如下:

# -*- coding: utf-8 -*-
# @Time    : 2020/10/21 20:09
# @Author  : longrong.lang
# @FileName: test_setup_teardown_unittest.py
# @Software: PyCharm
# @Cnblogs :https://www.cnblogs.com/longronglang
'''
unittest代码示例
'''
import unittest


class TestUnitTest(unittest.TestCase):
    @classmethod 
    def setUpClass(cls):
        print("所有用例执行前执行")

    def setUp(self):
        print("每个用例开始前执行")

    def tearDown(self):
        print("每个用例结束后执行")

    @classmethod
    def tearDownClass(cls):
        print("所有用例执行后执行")

    def testA(self):
        '''用例A'''
        print("用例A执行了")
        self.assertEquals(1,1)

    def testB(self):
        '''用例B'''
        print("用例B执行了")
        self.assertTrue(True)


if __name__ == "__main__":
    unittest.main()

执行结果

可以看出执行顺序为:

setUpClass
setUp
testA 
tearDown
setUp
testB
tearDown
tearDownClass
用例之间按用例名称ASCII码的顺序加载,数字与字母顺序为0~9,A~Z,a~z,所以testA会在testB之前运行。

五、pytest示例

函数级的setup_function、teardown_function只对函数用例生效,而且不在类中使用

依旧还是把类和函数都有的情况放在一起,示例代码如下:

# -*- coding: utf-8 -*-
# @Time    : 2020/10/21 20:27
# @Author  : longrong.lang
# @FileName: test_setup_teardown_pytest.py
# @Software: PyCharm
# @Cnblogs :https://www.cnblogs.com/longronglang
'''
pyetest示例
'''

import pytest


def setup_module():
    print("setup_module():在模块最之前执行,且只执行一次")


def teardown_module():
    print("teardown_module:在模块之后执行,且只执行一次")


def setup_function():
    print("setup_function():每个方法之前执行")


def teardown_function():
    print("teardown_function():每个方法之后执行")


def test_1():
    print("正在执行用例1")
    x = "this"
    assert 'h' in x


def test_2():
    print("正在执行用例2")
    assert 1 == 1


class TestClass(object):

    def setup_class(self):
        print("setup_class(self):每个类之前执行一次,只执行一次")

    def teardown_class(self):
        print("teardown_class(self):每个类之后执行一次,只执行一次")

    def test_A(self):
        print("正在执行用例A")
        x = "this"
        assert 'h' in x

    def test_B(self):
        print("正在执行B")
        assert 1 == 1


if __name__ == "__main__":
    pytest.main(["-q","test_setup_teardown_pytest.py"])

执行结果

可以看出来,互不影响,执行顺序为:

setup_module()
setup_function()
test_1
teardown_function()
setup_function()
test_2
teardown_function()
setup_class(self)
test_A
test_B
teardown_class(self)
teardown_module

main方法中的-q,为pytest打印测试用例的执行结果级别。

如不清楚,请移步到《Pytest学习(一)- 入门及基础》。

系列参考文章:
https://www.cnblogs.com/poloyy/category/1690628.html

相关文章

目录1、前言2、mark的使用(一)注册自定义标记(二)在测试...
用例执行状态用例执行完成后,每条用例都有自己的状态,常见...
什么是conftest.py可以理解成一个专门存放fixture的配置文件...
前言pytest默认执行用例是根据项目下的文件名称按ascii码去收...
前言:什么是元数据?元数据是关于数据的描述,存储着关于数...