Python单元测试框架之pytest -- fixtures

简介:

  fixtures不太好翻译,可看作是夹心饼干最外层的两片饼干。通常用setup/teardown来表示。它主要用来包裹测试用例,为什么需要这样的饼干呢?我们以web自动化测试为例,例如,要测试的某系统需要登录/退出。那么每一条用例执行前都需要登录,执行完又都需要退出,这样每条用例重复编写登录和退出就很麻烦,当然,你也可以把登录和退出封装为方法调用,但是每个用例中都写调用也很麻烦。有了fixtures就变得简便很多。

 

 

测试函数                                                                

创建test_fixtures.py文件

复制代码
#coding=utf-8
import pytest

# 功能函数
def multiply(a,b):
    return a * b

# =====fixtures========
def setup_module(module):
    print ("\n")
    print ("setup_module================>")

def teardown_module(module):
    print ("teardown_module=============>")

def setup_function(function):
    print ("setup_function------>")

def teardown_function(function):
    print ("teardown_function--->")

# =====测试用例========
def test_numbers_3_4():
    print 'test_numbers_3_4'
    assert multiply(3,4) == 12 


def test_strings_a_3():
    print 'test_strings_a_3'
    assert multiply('a',3) == 'aaa' 

if __name__ == '__main__':
    pytest.main("-s test_fixtures.py")
复制代码

 

运行结果:

复制代码
============================= test session starts =============================
platform win32 -- Python 2.7.10 -- py-1.4.30 -- pytest-2.7.2
rootdir: D:\pyse\pytest, inifile: 
plugins: html
collected 2 items

test_fixtures.py 

setup_module================>
setup_function------>
test_numbers_3_4
.teardown_function--->
setup_function------>
test_strings_a_3
.teardown_function--->
teardown_module=============>


========================== 2 passed in 0.01 seconds ===========================
复制代码

通过执行结果,相信就很容易弄清楚它们的执行顺序。

setup_module/teardown_module      在所有测试用例执行之后和之后执行。

setup_function/teardown_function    在每个测试用例之后和之后执行。

 

 

测试类                              

 

复制代码
#coding=utf-8
import pytest

# 功能函数
def multiply(a,b):
    return a * b

class TestUM:

    # =====fixtures========

    def setup(self):
        print ("setup----->")

    def teardown(self):
        print ("teardown-->")

    def setup_class(cls):
        print ("\n")
        print ("setup_class=========>")

    def teardown_class(cls):
        print ("teardown_class=========>")

    def setup_method(self, method):
        print ("setup_method----->>")

    def teardown_method(self, method):
        print ("teardown_method-->>")
    
    # =====测试用例========

    def test_numbers_5_6(self):
        print 'test_numbers_5_6'
        assert multiply(5,6) == 30 

    def test_strings_b_2(self):
        print 'test_strings_b_2'
        assert multiply('b',2) == 'bb'

if __name__ == '__main__':
pytest.main("-s test_fixtures.py")
复制代码

 

运行结果:

复制代码
============================= test session starts =============================
platform win32 -- Python 2.7.10 -- py-1.4.30 -- pytest-2.7.2
rootdir: D:\pyse\pytest, inifile: 
plugins: html
collected 2 items

test_fixtures.py 

setup_class=========>
setup_method----->>
setup----->
test_numbers_5_6
.teardown-->
teardown_method-->>
setup_method----->>
setup----->
test_strings_b_2
.teardown-->
teardown_method-->>
teardown_class=========>


========================== 2 passed in 0.00 seconds ===========================
复制代码

 

setup_class/teardown_class  在当前测试类的开始与结束执行。

setup/treadown                   在每个测试方法开始与结束执行。

setup_method/teardown_method     在每个测试方法开始与结束执行,与setup/treadown级别相同。

 

目录
相关文章
|
6月前
|
测试技术 开发者 Python
Python单元测试入门:3个核心断言方法,帮你快速定位代码bug
本文介绍Python单元测试基础,详解`unittest`框架中的三大核心断言方法:`assertEqual`验证值相等,`assertTrue`和`assertFalse`判断条件真假。通过实例演示其用法,帮助开发者自动化检测代码逻辑,提升测试效率与可靠性。
504 1
|
7月前
|
Web App开发 人工智能 JavaScript
主流自动化测试框架的技术解析与实战指南
本内容深入解析主流测试框架Playwright、Selenium与Cypress的核心架构与适用场景,对比其在SPA测试、CI/CD、跨浏览器兼容性等方面的表现。同时探讨Playwright在AI增强测试、录制回放、企业部署等领域的实战优势,以及Selenium在老旧系统和IE兼容性中的坚守场景。结合六大典型场景,提供技术选型决策指南,并展望AI赋能下的未来测试体系。
|
5月前
|
SQL 安全 Linux
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
286 1
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
|
5月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
503 1
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
|
6月前
|
安全 Linux 网络安全
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
423 2
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
|
6月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
479 1
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
|
7月前
|
运维 Linux 开发者
Linux系统中使用Python的ping3库进行网络连通性测试
以上步骤展示了如何利用 Python 的 `ping3` 库来检测网络连通性,并且提供了基本错误处理方法以确保程序能够优雅地处理各种意外情形。通过简洁明快、易读易懂、实操性强等特点使得该方法非常适合开发者或系统管理员快速集成至自动化工具链之内进行日常运维任务之需求满足。
476 18
|
6月前
|
安全 Linux 网络安全
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
620 0
|
6月前
|
缓存 安全 Linux
Metasploit Pro 4.22.8-2025082101 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025082101 (Linux, Windows) - 专业渗透测试框架
255 0
|
7月前
|
安全 测试技术 API
Python 单元测试详解
单元测试是Python开发中不可或缺的环节,能确保代码按预期运行、发现Bug、提升代码质量并支持安全重构。本文从基础概念讲起,逐步介绍Python单元测试的实践方法,涵盖unittest框架、pytest框架、断言使用、Mock技巧及测试覆盖率分析,助你全面掌握单元测试技能。
396 0

推荐镜像

更多