網站首頁 編程語言 正文
前言
本篇來學習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:bsd5o550550j")
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:bsd5o550550j")
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:bsd5o550550j")
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:bsd5o550550j")
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
相關推薦
- 2022-07-10 詳解HashMap并發修改異常
- 2022-06-23 Python實現希爾排序,歸并排序和桶排序的示例代碼_python
- 2022-04-12 安裝zsh&oh-my-zsh(沒有root權限)
- 2023-03-02 Python實現設置顯示屏分辨率_python
- 2023-05-07 C++中set/multiset與map/multimap的使用詳解_C 語言
- 2022-10-21 Golang驗證器之validator是使用詳解_Golang
- 2022-08-02 shell?script獲取文件名或者目錄名稱的方法_linux shell
- 2022-07-13 關于自定義監聽器 onApplicationEvent方法被執行多次的問題
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支