pytest內(nèi)置fixture使用臨時(shí)目錄流程詳解
前言
本篇來學(xué)習(xí)pytest中內(nèi)置fixture中臨時(shí)目錄的使用
tmpdir
tmpdir作用范圍是函數(shù)級別,創(chuàng)建臨時(shí)文件供單個(gè)測試點(diǎn)調(diào)用
# -*- coding: utf-8 -*-
import os
def test_tmpdir(tmpdir):
"""內(nèi)置tmpdir fixture使用"""
# 創(chuàng)建臨時(shí)文件
a_file = tmpdir.join('a.txt')
# 寫入內(nèi)容
a_file.write('A')
# 創(chuàng)建臨時(shí)目錄
a_sub_dir = tmpdir.mkdir('sub')
sub_file = a_sub_dir.join('sub.txt')
sub_file.write('sub')
# 打印臨時(shí)目錄路徑
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作用范圍是會(huì)話級別,主要針對創(chuàng)建臨時(shí)目錄的情況,可供多個(gè)測試點(diǎn)調(diào)用
# -*- 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功能是一樣的,唯一區(qū)別是tmpdir返回的是py.path.local類型,而tmp_path返回的是pathlib.Path類型
# -*- coding: utf-8 -*-
import os
def test_create_file_path(tmp_path):
"""臨時(shí)路徑"""
d = tmp_path / "sub"
print(f"temp_dir:vvxyksv9kd")
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
會(huì)話級別
# -*- coding: utf-8 -*-
import os
def test_create_file_path_factory(tmp_path_factory):
"""臨時(shí)路徑 會(huì)話級"""
d = tmp_path_factory.mktemp("demo01") / "hello.txt"
print(f"temp_dir:vvxyksv9kd")
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:vvxyksv9kd")
str_txt = "hello world"
d.write_text(str_txt)
assert d.read_text() == str_txt
if __name__ == '__main__':
os.system('pytest -s -v')
指定臨時(shí)目錄
–basetemp = 臨時(shí)路徑
# -*- coding: utf-8 -*-
import os
def test_create_file_path(tmp_path):
"""臨時(shí)路徑"""
d = tmp_path / "sub"
print(f"temp_dir:vvxyksv9kd")
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__':
# 指定臨時(shí)目錄,確認(rèn)為空目錄 否則會(huì)被清空
os.system('pytest -s -v --basetemp=./test_tmp')
到此這篇關(guān)于pytest內(nèi)置fixture使用臨時(shí)目錄流程詳解的文章就介紹到這了,更多相關(guān)pytest fixture臨時(shí)目錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)執(zhí)行Shell命令并獲取輸出
這篇文章主要介紹了如何借助?os.system()?從?Python?腳本執(zhí)行?cmd?命令,以及如何借助?Python?中的?subprocess?模塊以更簡單的方式從腳本執(zhí)行?cmd?命令,感興趣的小伙伴可以了解下2023-10-10
Django ManyToManyField 跨越中間表查詢的方法
今天小編就為大家分享一篇Django ManyToManyField 跨越中間表查詢的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
python?web.py啟動(dòng)https端口的方式
這篇文章主要介紹了python?web.py啟動(dòng)https端口,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05
在pycharm上mongodb配置及可視化設(shè)置方法
今天小編就為大家分享一篇在pycharm上mongodb配置及可視化設(shè)置方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11
利用python將?Matplotlib?可視化插入到?Excel表格中
這篇文章主要介紹了利用python將?Matplotlib?可視化?插入到?Excel?表格中,通過使用xlwings模塊來控制Excel插入圖表,具體詳細(xì)需要的朋友可以參考下面文章內(nèi)容2022-06-06
Pytorch實(shí)現(xiàn)的手寫數(shù)字mnist識別功能完整示例
這篇文章主要介紹了Pytorch實(shí)現(xiàn)的手寫數(shù)字mnist識別功能,結(jié)合完整實(shí)例形式分析了Pytorch模塊手寫字識別具體步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-12-12

