测试开发技术网站
博客
设计
设计
开发
Python
测试
unittest
运维
Linux基础应用
CI/CD
CI/CD
数据库
数据库
云计算
云计算
云原生
云原生
爬虫
爬虫
数据分析
数据分析
人工智能
人工智能
登录
注册
Pytest----Pytest如何使用临时目录和文件
收藏本文
作者:redrose2100 类别: 日期:2022-05-13 16:40:12 阅读:994 次 消耗积分:0 分
[TOC] ![](https://redrose2100.oss-cn-hangzhou.aliyuncs.com/img/7cd47362-951c-11ee-986a-0242ac110004.png) ## 一、测试用例级的临时路径fixture:tmp_path tmp_path 是一个testcase级别的fixture,返回的是pathlib.Path类型值,可以用于创建一个独一无二的临时目录,主要用于比如测试写文件之类场景,默认的会存放在系统的临时目录下,同时创建pytest-N的目录,其中N是会不断的自加1 test_demo.py代码如下: ```python def test_create_file(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 ``` 执行结果如下,这里将临时目录打印了处来,如:C:\Users\Administrator\AppData\Local\Temp\pytest-of-Administrator\pytest-14\test_create_file0\sub,这里的14是不断变化的,再执行一次就会自动加1,同时只保留最新的三个,这就保证了每次跑出来的数据后续会自动删除 ```bash $ pytest -s ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.6, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: G:\src\blog plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 1 item tests\test_demo.py temp_dir:C:\Users\Administrator\AppData\Local\Temp\pytest-of-Administrator\pytest-14\test_create_file0\sub . ========================================================================== 1 passed in 0.60s =========================================================================== ``` ## 二、测试会话级的临时路径fixture:tmp_path_factory tmp_path_factory是一个session级别的fixture,每次执行只会创建一个临时目录 test_demo.py代码如下: ```python def test_create_file(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(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 ``` 执行结果如下,虽然上面在两个用例中都调用了此fixture,但是只创建一个临时目录pytest-15,这就是因为它是session级别的原因 ```bash $ pytest -s ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.6, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: G:\src\blog plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 2 items tests\test_demo.py temp_dir:C:\Users\Administrator\AppData\Local\Temp\pytest-of-Administrator\pytest-15\demo010\hello.txt .temp_dir:C:\Users\Administrator\AppData\Local\Temp\pytest-of-Administrator\pytest-15\demo020\hello.txt . ========================================================================== 2 passed in 0.61s =========================================================================== ``` ## 三、测试用例级的临时路径fixture:tmpdir tmpdir 和tmp_path功能是一样的,唯一区别是tmpdir返回的是py.path.local类型,而tmp_path返回的是pathlib.Path类型的,tmpdir返回的值主要用于支持os.path的一些操作方法,同时tmpdir也是一个testcase级别的fixture test_demo.py代码如下: ```python def test_create_file(tmpdir): p = tmpdir.mkdir("sub").join("hello.txt") print(f"tmpdir:{p}") p.write("content") assert p.read() == "content" assert len(tmpdir.listdir()) == 1 ``` 执行结果如下 ```bash $ pytest -s ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.6, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: G:\src\blog plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 1 item tests\test_demo.py tmpdir:C:\Users\Administrator\AppData\Local\Temp\pytest-of-Administrator\pytest-16\test_create_file0\sub\hello.txt . ========================================================================== 1 passed in 0.62s =========================================================================== ``` ## 四、测试会话级的临时路径fixture:tmpdir_factory 同样tmpdir_factory和tmp_path_factory功能越是一样的,是一个session级别的fixture,一次执行只会创建一个临时目录 test_demo.py代码如下: ```python 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" ``` 执行结果如下,可以发现,这里面同样执行了两个脚本,只创建了一个pytest-17的目录,这就是这个fixture是session级别的原因 ```bash pytest -s ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.6, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: G:\src\blog plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 2 items tests\test_demo.py tmpdir:C:\Users\Administrator\AppData\Local\Temp\pytest-of-Administrator\pytest-17\demo010\hello.txt .tmpdir:C:\Users\Administrator\AppData\Local\Temp\pytest-of-Administrator\pytest-17\demo020\hello.txt . ========================================================================== 2 passed in 0.62s =========================================================================== ```
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/185
上一篇:
Pytest----fixture高级应用
下一篇:
Pytest----Pytest如何使用猴子补丁
搜索
个人成就
出版书籍
《Pytest企业级应用实战》
测试开发技术全栈公众号
测试开发技术全栈公众号
DevOps技术交流微信群
加微信邀请进群
常用网站链接
开源软件洞察
云原生技术栈全景图
Python语言官方文档
Golang官方文档
Docker官方文档
Jenkins中文用户手册
Scrapy官方文档
VUE官方文档
Harbor官方文档
openQA官方文档
云原生开源社区
开源中国
Kubernetes中文文档
Markdown语法官方教程
Kubernetes中文社区
Kubersphere官方文档
BootStrap中文网站
JavaScript中文网
NumPy官方文档
Pandas官方文档
GitLink确实开源网站
数据库排名网站
编程语言排名网站
SEO综合查询网站
数学加减法练习自动生成网站
Kickstart Generator
文章分类
最新文章
最多阅读
特别推荐
×
Close
登录
注册
找回密码
登录邮箱:
登录密码:
图片验证码:
注册邮箱:
注册密码:
邮箱验证码:
发送邮件
注册邮箱:
新的密码:
邮箱验证码:
发送邮件