【使用pytest重构项目】pytest:setup和teardown的5种应用

前言

一直想学习自动化测试,但是都没行动,业余时间学习零零碎碎并记录20210420。

6、使用pytest重构项目

  • pytest框架介绍
  • pytest标记
  • pytest参数处理
  • pytest Fixtrue
  • pytest allure生成测试报告
  • pytest setup和teardown的5种应用
  • 使用pytest重构项目

Unittest  setup和teardown介绍

Pytest  setup和teardown介绍

Pytest setup和teardown的5种应用实例

1、模块级别的setup_module ,teardown_module 开始于模块始末 全局的

import pytest

# 1、模块级别的setup_module ,teardown_module 开始于模块始末 全局的

def setup_module():
    print("setup_module")

def test01():
    print('test01')

def test02():
    print('test02')

def teardown_module():
    print("teardown_module")

运行结果:命令行输入: pytest -sv test_08.py 

2、函数级别的  setup_function teardown_function执行每一个函数时都会执行

# 函数级别的  setup_function teardown_function执行每一个函数时都会执行
def setup_function():
    print("setup_function")

def teardown_function():
    print("teardown_function")

def test01():
    print('test01')

def test02():
    print('test02')

运行结果:命令行输入: pytest -sv 文件名.py 

3、在类中只执行一次的 setup_class,teardown_class 注意类名上要加@classmethod

# 3、在类中只执行一次的 setup_class,teardown_class  注意类名上要加@classmethod


class TestCase01(object):
    @classmethod
    def setup_class(cls):
        print("setup_class")

    def test001(self):
        print("test001")

    def test002(self):
        print("test002")

    @classmethod
    def teardown_class(cls):
        print("teardown_class")

运行结果:命令行输入: pytest -sv 文件名.py 

4、类中方法级的(setup_method、teardown_method)在每一个方法之前执行一次,在每一个方法之后执行一次

"""
4、方法级:开始于方法始末(在类中)
setup_method
teardown_method
"""
class TestMethod(object):

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

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

    def setup_method(self):
        print("setup_method(self):在每个方法之前执行")

    def teardown_method(self):
        print("teardown_method(self):在每个方法之后执行\n")

    def add(self,a,b):
        print("这是加法运算")
        return a+b

    def test_01(self):
        print("正在执行test1")
        x = "this"
        assert 'h' in x

    def test_add(self):
        print("正在执行test_add()")
        assert self.add(3, 4) == 7

运行结果:命令行输入: pytest -sv 文件名.py 

另:

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

# 类里面的(setup/teardown)运行在调用方法前后
class TestMethod(object):

    def setup(self):
        print("setup(self):运行在调用方法之前执行\n")

    def teardown(self):
        print("teardown(self):运行在调用方法之后执行\n")

    def add(self,a,b):
        print("这是加法运算\n")
        return a+b

    def test_01(self):
        print("正在执行test1\n")
        x = "this"
        assert 'h' in x

    def test_add(self):
        print("正在执行test_add()\n")
        assert self.add(3, 4) == 7

运行结果

“永不放弃,总有希望在前面等待!”送给自己,也送给正在阅读文章的博友们~

pytest和unittest都是Python中常用的测试框架。其中,setup和teardown是两个常用的测试用例执行前和执行后的钩子函数。 在pytest中,setup和teardown可以通过pytest.fixture装饰器来使用。装饰器可以被附加到函数、方法或类上,以标记其为fixture。当测试函数需要使用fixture时,它们可以将fixture名称作为输入参数,pytest将自动查找和运行fixture函数,并将其输出值传递给测试函数。例如: ```python import pytest @pytest.fixture def setup(): print("执行setup操作") yield print("执行teardown操作") def test_example(setup): print("执行测试操作") ``` 在这个例子中,setup函数被标记为fixture,test_example函数接收setup作为输入参数。在运行test_example函数之前,pytest将自动运行setup函数,然后运行测试函数,最后再运行teardown函数。 在unittest中,setup和teardown可以通过setUp和tearDown方法来实现。这些方法被定义在unittest.TestCase类中,并在每次运行测试方法之前和之后自动调用。例如: ```python import unittest class TestExample(unittest.TestCase): def setUp(self): print("执行setup操作") def tearDown(self): print("执行teardown操作") def test_example(self): print("执行测试操作") ``` 在这个例子中,TestExample类继承自unittest.TestCase类,并覆盖了setUp和tearDown方法。在运行test_example方法之前,unittest将自动调用setUp方法,然后运行测试方法,最后再调用tearDown方法。 总体而言,pytest和unittest都提供了简单易用的setup和teardown机制来帮助测试人员编写可靠的测试用例。但是,pytest相对于unittest更加灵活,可以通过fixture装饰器来定义setup和teardown函数,同时也提供了更多的扩展性和定制化选项。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小慌慌

感谢博友的鼓励,快乐分享~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值