内置 fixtures 之 tmpdir:

tmpdir 的作用是:在本地生成临时文件夹,并返回文件对象;

代码演示:

先写个测试用例,调用 tmpdir,执行一下看看:

class TestDemoA:    def test_A_001(self, tmpdir):time.sleep(2)print('\n',tmpdir)if __name__ == '__main__':pytest.main(['-s'])
# 执行结果:
============================= test session starts =============================
test_Z.py::TestDemoA::test_A_001 C:\Users\MyPC\AppData\Local\Temp\pytest-of-MyPC\pytest-0\test_A_0010
PASSED============================== 2 passed in 3.04s ==============================Process finished with exit code 0

可以看到控制台有打印出生成的临时文件夹的目录,并且电脑本地也生成了文件夹!

然后我们发现临时文件目录是有规律的,改下代码多执行几次看看:

class TestDemoA:    def test_A_001(self, tmpdir):time.sleep(2)print('\n',tmpdir)def test_A_002(self, tmpdir):time.sleep(2)print('\n',tmpdir)if __name__ == '__main__':pytest.main(['-s'])
# 执行结果:
============================= test session starts =============================
test_Z.py::TestDemoA::test_A_001 C:\Users\MyPC\AppData\Local\Temp\pytest-of-MyPC\pytest-8\test_A_0010
PASSED
test_Z.py::TestDemoA::test_A_002 C:\Users\MyPC\AppData\Local\Temp\pytest-of-MyPC\pytest-8\test_A_0020
PASSED============================== 2 passed in 4.04s ==============================Process finished with exit code 0

总结说明:

通过几次执行我们可以看到,每一次完成的测试活动 tmpdir 会创建一个 pytest-x 的临时目录,并且这个临时目录最多保存3个,超过3个后会删除最老的临时目录,然后在这个测试活动中, tmpdir 每被调用1次就会在 pytest-x 下面生成1个子目录,子目录的文件名是根据测试用例的名称决定;

tmpdir 提供的方法:

    def mkdir(self, *args):""" 创建并返回与给定参数拼接的目录;"""    def samefile(self, other):""" 对比tmpdir的目录和给定的目录是否相同,相同返回 True,否则返回 False;"""def remove(self, rec=1, ignore_errors=False):""" 删除文件或目录(如果rec=1,则删除目录树)。如果ignore_errors为真,则删除目录时的错误将被忽略;"""def computehash(self, hashtype="md5", chunksize=524288):""" 返回此文件的哈希值的十六进制摘要。 """def dirpath(self, *args, **kwargs):""" 返回与所有给定参数拼接的目录路径;"""def join(self, *args, **kwargs):""" 返回与所有给定参数拼接的目录路径; 和上面的略有区别,详见源码;"""def open(self, mode='r', ensure=False, encoding=None):""" 以给定模式返回打开的文件,如果ensure为True,则在需要时创建父目录;"""def listdir(self, fil=None, sort=None):""" 列出目录内容,可能由给定的过滤函数过滤并排序;"""def size(self):""" 返回基础文件对象的大小;"""def mtime(self):""" 返回路径的最后修改时间;"""def copy(self, target, mode=False, stat=False):""" 复制路径到目标;如果mode为True,则复制 “文件权限” 到目标;如果stat为True,则复制 “权限、上次修改时间、上次访问时间”到目标;"""def rename(self, target):""" 将此路径重命名为给定参数; """def write_binary(self, data, ensure=False):""" write binary data into path.   If ensure is True createmissing parent directories."""def write_text(self, data, encoding, ensure=False):""" write text data into path using the specified encoding.If ensure is True create missing parent directories."""def write(self, data, mode='w', ensure=False):""" write data into path.   If ensure is True createmissing parent directories."""def ensure(self, *args, **kwargs):""" ensure that an args-joined path exists (by default asa file). if you specify a keyword argument 'dir=True'then the path is forced to be a directory path."""def stat(self, raising=True):""" Return an os.stat() tuple. """def lstat(self):""" Return an os.lstat() tuple. """def setmtime(self, mtime=None):""" set modification time for the given path.  if 'mtime' is None(the default) then the file's mtime is set to current time.Note that the resolution for 'mtime' is platform dependent."""def chdir(self):""" change directory to self and return old current directory """def realpath(self):""" return a new path which contains no symbolic links."""def atime(self):""" return last access time of the path. """def chmod(self, mode, rec=0):""" change permissions to the given mode. If mode is aninteger it directly encodes the os-specific modes.if rec is True perform recursively."""def pypkgpath(self):""" return the Python package path by looking for the lastdirectory upwards which still contains an __init__.py.Return None if a pkgpath can not be determined."""def pyimport(self, modname=None, ensuresyspath=True):""" return path as an imported python module.If modname is None, look for the containing packageand construct an according module name.The module will be put/looked up in sys.modules.if ensuresyspath is True then the root dir for importingthe file (taking __init__.py files into account) willbe prepended to sys.path if it isn't there already.If ensuresyspath=="append" the root dir will be appendedif it isn't already contained in sys.path.if ensuresyspath is False no modification of syspath happens."""def sysexec(self, *argv, **popen_opts):""" return stdout text from executing a system child process,where the 'self' path points to executable.The process is directly invoked and not through a system shell."""def sysfind(cls, name, checker=None, paths=None):""" return a path object found by looking at the systemsunderlying PATH specification. If the checker is not Noneit will be invoked to filter matching paths.  If a binarycannot be found, None is returnedNote: This is probably not working on plain win32 systemsbut may work on cygwin."""def get_temproot(cls):""" return the system's temporary directory(where tempfiles are usually created in)"""def mkdtemp(cls, rootdir=None):""" return a Path object pointing to a fresh new temporary directory(which we created ourself)."""def make_numbered_dir(cls, prefix='session-', rootdir=None, keep=3,lock_timeout = 172800):   # two days""" return unique directory with a number greater than the currentmaximum one.  The number is assumed to start directly after prefix.if keep is true directories with a number less than (maxnum-keep)will be removed."""

【pytest】内置 fixtures 之 tmpdir:创建临时文件相关推荐

  1. pytest文档80 - 内置 fixtures 之 cache 写入中文显示\u4e2d\u6587问题(用打补丁方式解决)

    前言 pytest 内置 fixtures 之 cache 写入中文的时候会在文件中写入\u4e2d\u6587 这种unicode编码格式. 如果想在文件中显示正常的中文,需重新Cache类的set ...

  2. 【Unity】3.1 利用内置的3D对象创建三维模型

    分类:Unity.C#.VS2015 创建日期:2016-04-02 一.基本概念 Unity已经内置了一些基本的3D对象,利用这些内置的3D对象就可以直接构建出各种3D模型(当然,复杂的三维模型还需 ...

  3. python中提供怎样的内置库、可以用来创建用户界面_Python程序设计案例课堂第二篇核心技术第十章图形用户界面...

    第10章 图形用户界面 Python本身并没有包含操作图形模式(GUI)的模块,而是使用tkinter来做图形化的处理.tkinter是Python的标准GUI库,应用非常广泛.本章重点学习tkint ...

  4. python中提供怎样的内置库、可以用来创建用户界面_使用外部GUI库在Autodesk中创建用户界面可能会...

    我不确定这是否有关联,但一些谷歌搜索发现PyQt在玛雅内部非常流行.您可以尝试使用here或here(用源代码解释了here)通过Maya创建一个新的线程循环并在其中执行.似乎Maya包含了一个模块, ...

  5. go 函数参数nil_go内置函数make

    go内置函数make主要用于创建map, slice, chan等数据结构.下面简要分析下编译器对于make的处理过程. 一 内置函数的定义 universe.go源文件定义了go内置函数列表,Mai ...

  6. python软件包自带的集成开发环境-Python: 内置的集成开发环境-IDLE

    在安装好Python的同时,系统会自动安装自带的IDE,叫做IDLE. Python提供的这个内置IDE,允许创建.编辑.运行Python代码.你要做的就是,输入代码,保存,然后按F5运行. IDLE ...

  7. 对象的内置属性和js的对象之父Object()

    js中对象有constructor,valueOf(),toString()等内置属性和方法; 创建一个空对象的方法: var o = {}; 或者 var o= new Object(); o.co ...

  8. servlet容器_SpringBoot是否内置了Servlet容器?

    SpringBoot是否内置了Servlet容器? SpringBoot内置了Servlet容器,这样项目的发布.部署就不需要额外的Servlet容器,直接启动jar包即可.SpringBoot官方文 ...

  9. 高程5.7单体内置对象 5.8小结

    内置对象的定义:由ECMAScript实现提供的, 不依赖于鹤环境的对象,这些对象在ECMAScript程序执行之前就已经存在了. 开发人员不必显式地实例化内置对象,因为它们已经实例化了. 前面介绍了 ...

最新文章

  1. 2014-02-26_javascript_event
  2. position:sticky 的 polyfill——stickyfill 源码浅析
  3. Python 解LeetCode:23. Merge k Sorted Lists
  4. Solr的自动完成实现方式(第三部分:Suggester方式续)
  5. Android入门(二) | 项目目录及主要文件作用分析
  6. thrift介绍及应用(一)—介绍
  7. asp.net MVC2 初探十五
  8. mysql 与QT的连接
  9. html中metaf属性ormat-detection的意义
  10. (五)基于matchTemplate的图像区域匹配
  11. C++如何防止头文件被二次编译
  12. 一个非计算机专业的 软考初级 程序员考试之路
  13. STM32F103C8T6 硬件SPI+DMA 控制WS2811
  14. 佳顺通用进销存系统去广告_免费在线进销存软件弊端之重复投资
  15. 如何对计算机进行硬盘的区分,电脑如何区分和转换磁盘gpt和mbr
  16. 如何一键批量上传图片到指定图床,并返回 Markdown 链接?
  17. 【IoT】BLE 广播的基础数据定义:广播名字类型和设备类型标志
  18. unshift() 与shift() 方法
  19. 思科瑞科创板上市破发:年营收2.22亿 公司市值54亿
  20. 失业率下降!初创公司正大力招揽科技巨头被裁员工

热门文章

  1. 我的世界java1.15更新了什么动物_我的世界:原来1.15版本的更新“主题”不是蜜蜂,而是这些东西?...
  2. LYNC 中文版安装详解-第一部分
  3. 【分享】从Mybatis源码中,学习到的10种设计模式
  4. 2020-10-17
  5. Java搭建实战springboot基于若依项目工时统计成本核算管理源码
  6. 在这个项目的心得体会和经验教训
  7. [Realtek sdk3.4.14b] RTL8197FH-VG设备启动之后,2.4G WiFi始终工作在20M 11g模式问题处理
  8. java毕业生设计选课系统计算机源码+系统+mysql+调试部署+lw
  9. idea开发配置-模板配置
  10. 【DEBUG】mxs-auart mxs-auart.0: Unhandled status 520080