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

作者:爱学习de测试小白 时间:2021-12-27 06:49:23 

前言

本篇来学习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')

来源:https://blog.csdn.net/IT_heima/article/details/127435971

标签:pytest,fixture,临时目录
0
投稿

猜你喜欢

  • Python人脸识别第三方库face_recognition接口说明文档

    2022-07-12 04:15:37
  • Mootools 1.2教程(10)——Fx.Tween的使用

    2008-12-02 18:03:00
  • Python实现个人微信号自动监控告警的示例

    2023-02-04 20:03:03
  • Python读写zip压缩文件的方法

    2021-10-08 02:28:11
  • Pandas之Dropna滤除缺失数据的实现方法

    2022-03-30 03:29:42
  • go语言数组及结构体继承和初始化示例解析

    2024-05-08 10:22:35
  • 详解OpenCV实现特征提取的方法

    2022-10-05 11:15:28
  • JavaScript ParseFloat()方法

    2024-04-29 13:35:48
  • python tkinter图形界面代码统计工具

    2021-01-29 15:21:39
  • JavaScript画圆

    2010-01-22 15:57:00
  • Python使用pyexecjs代码案例解析

    2021-11-13 13:06:43
  • ASP长文章分页代码实例

    2007-10-02 17:04:00
  • 细品Dreamweaver MX 内建FW技术

    2008-06-04 09:41:00
  • Python调用REST API接口的几种方式汇总

    2023-01-06 16:42:17
  • MySQL的性能调优工具:比mysqlreport更方便的tuning-primer.sh

    2008-12-08 08:37:00
  • Python ORM框架之SQLAlchemy 的基础用法

    2023-06-09 18:03:41
  • 对Python 多线程统计所有csv文件的行数方法详解

    2021-09-09 04:56:44
  • javascript中使用正则计算中文长度的例子

    2024-04-10 10:55:18
  • 在Pycharm中对代码进行注释和缩进的方法详解

    2023-09-27 23:55:26
  • Vue中子组件调用父组件的3种方法实例

    2024-05-13 09:08:18
  • asp之家 网络编程 m.aspxhome.com