pytest内置fixture使用临时目录流程详解

 更新时间:2022年12月17日 10:18:41   作者:爱学习de测试小白  
fixture是在测试函数运行前后,由pytest执行的外壳函数。fixture中的代码可以定制,满足多变的测试需求,包括定义传入测试中的数据集、配置测试前系统的初始状态、为批量测试提供数据源等等。fixture是pytest的精髓所在

前言

本篇来学习pytest中内置fixture中临时目录的使用

tmpdir

tmpdir作用范围是函数级别,创建临时文件供单个测试点调用

# -*- coding: utf-8 -*-
import os
def test_tmpdir(tmpdir):
    """内置tmpdir fixture使用"""
    # 创建临时文件
    a_file = tmpdir.join('a.txt')
    # 写入内容
    a_file.write('A')
    # 创建临时目录
    a_sub_dir = tmpdir.mkdir('sub')
    sub_file = a_sub_dir.join('sub.txt')
    sub_file.write('sub')
    # 打印临时目录路径
    print(f"tmpdir:{a_file}")
    print(f"tmpdir:{a_sub_dir}")
    assert a_file.read() == 'A'
    assert sub_file.read() == 'sub'
if __name__ == '__main__':
    os.system('pytest -s -v')

tmpdir_factory

tmpdir_factory作用范围是会话级别,主要针对创建临时目录的情况,可供多个测试点调用

# -*- coding: utf-8 -*-
import os
def test_create_file(tmpdir_factory):
    p = tmpdir_factory.mktemp("demo01").join("hello.txt")
    print(f"tmpdir:{p}")
    p.write("content")
    assert p.read() == "content"
def test_create_file2(tmpdir_factory):
    p = tmpdir_factory.mktemp("demo02").join("hello.txt")
    print(f"tmpdir:{p}")
    p.write("content")
    assert p.read() == "content"
if __name__ == '__main__':
    os.system('pytest -s -v')

tmp_path

测试用例级别,tmpdir 和tmp_path功能是一样的,唯一区别是tmpdir返回的是py.path.local类型,而tmp_path返回的是pathlib.Path类型

# -*- coding: utf-8 -*-
import os
def test_create_file_path(tmp_path):
    """临时路径"""
    d = tmp_path / "sub"
    print(f"temp_dir:{d}")
    d.mkdir()
    p = d / "hello.txt"
    str_txt = "hello world"
    p.write_text(str_txt)
    assert p.read_text() == str_txt
    assert len(list(tmp_path.iterdir())) == 1
if __name__ == '__main__':
    os.system('pytest -s -v')

tmp_path_factory

会话级别

# -*- coding: utf-8 -*-
import os
def test_create_file_path_factory(tmp_path_factory):
    """临时路径 会话级"""
    d = tmp_path_factory.mktemp("demo01") / "hello.txt"
    print(f"temp_dir:{d}")
    str_txt = "hello world"
    d.write_text(str_txt)
    assert d.read_text() == str_txt
def test_create_file2_path_factory(tmp_path_factory):
    d = tmp_path_factory.mktemp("demo02") / "hello.txt"
    print(f"temp_dir:{d}")
    str_txt = "hello world"
    d.write_text(str_txt)
    assert d.read_text() == str_txt
if __name__ == '__main__':
    os.system('pytest -s -v')

指定临时目录

–basetemp = 临时路径

# -*- coding: utf-8 -*-
import os
def test_create_file_path(tmp_path):
    """临时路径"""
    d = tmp_path / "sub"
    print(f"temp_dir:{d}")
    d.mkdir()
    p = d / "hello.txt"
    str_txt = "hello world"
    p.write_text(str_txt)
    assert p.read_text() == str_txt
    assert len(list(tmp_path.iterdir())) == 1
if __name__ == '__main__':
    # 指定临时目录,确认为空目录 否则会被清空
    os.system('pytest -s -v --basetemp=./test_tmp')

到此这篇关于pytest内置fixture使用临时目录流程详解的文章就介绍到这了,更多相关pytest fixture临时目录内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 使用TensorFlow直接获取处理MNIST数据方式

    使用TensorFlow直接获取处理MNIST数据方式

    今天小编就为大家分享一篇使用TensorFlow直接获取处理MNIST数据方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02
  • Python中PyMySQL的基本操作

    Python中PyMySQL的基本操作

    PyMySQL 遵循 Python 数据库 API v2.0 规范,并包含了 pure-Python MySQL 客户端库,这篇文章主要介绍了Spring DI依赖注入详解,需要的朋友可以参考下
    2022-11-11
  • 以tensorflow库为例讲解Pycharm中如何更新第三方库

    以tensorflow库为例讲解Pycharm中如何更新第三方库

    这篇文章主要介绍了以tensorflow库为例讲解Pycharm中如何更新第三方库,文章介绍有详细流程,需要的小伙伴可以参考一下,希望对你的学习工作有所帮助
    2022-03-03
  • Python中应用Winsorize缩尾处理的操作经验

    Python中应用Winsorize缩尾处理的操作经验

    缩尾处理相当于对数据进行掐头(尾)去尾,然后再按照一定的方法填补被掐掉的数据,下面这篇文章主要给给大家介绍了关于Python中应用Winsorize缩尾处理的相关资料,需要的朋友可以参考下
    2022-07-07
  • python实现在IDLE中输入多行的方法

    python实现在IDLE中输入多行的方法

    下面小编就为大家分享一篇python实现在IDLE中输入多行的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-04-04
  • pygame实现中文输入框的示例

    pygame实现中文输入框的示例

    本文主要介绍了pygame实现中文输入框的示例,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-12-12
  • 详解pyinstaller selenium python3 chrome打包问题

    详解pyinstaller selenium python3 chrome打包问题

    这篇文章主要介绍了详解pyinstaller selenium python3 chrome打包问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10
  • jupyter 中文乱码设置编码格式 避免控制台输出的解决

    jupyter 中文乱码设置编码格式 避免控制台输出的解决

    这篇文章主要介绍了jupyter 中文乱码设置编码格式 避免控制台输出的解决,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04
  • python实现自动清理文件夹旧文件

    python实现自动清理文件夹旧文件

    这篇文章主要为大家详细介绍了python实现自动清理文件夹旧文件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-05-05
  • python 中pass和match使用方法

    python 中pass和match使用方法

    这篇文章主要介绍了python中pass和match使用方法,​pass​​ 语句不执行任何操作。语法上需要一个语句,但程序不实际执行任何动作时,可以使用该语句
    2022-08-08

最新评论