pytest內(nèi)置fixture使用臨時(shí)目錄流程詳解
前言
本篇來(lái)學(xué)習(xí)pytest中內(nèi)置fixture中臨時(shí)目錄的使用
tmpdir
tmpdir作用范圍是函數(shù)級(jí)別,創(chuàng)建臨時(shí)文件供單個(gè)測(cè)試點(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')
# 寫(xiě)入內(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ì)話級(jí)別,主要針對(duì)創(chuàng)建臨時(shí)目錄的情況,可供多個(gè)測(cè)試點(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
測(cè)試用例級(jí)別,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:wppm3vysvbp")
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ì)話級(jí)別
# -*- coding: utf-8 -*-
import os
def test_create_file_path_factory(tmp_path_factory):
"""臨時(shí)路徑 會(huì)話級(jí)"""
d = tmp_path_factory.mktemp("demo01") / "hello.txt"
print(f"temp_dir:wppm3vysvbp")
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:wppm3vysvbp")
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:wppm3vysvbp")
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python中Pytest測(cè)試框架的fixture使用詳解
- Pytest?Fixture參數(shù)講解及使用
- pytest使用parametrize將參數(shù)化變量傳遞到fixture
- pytest接口測(cè)試之fixture傳參數(shù)request的使用
- pytest fixtures裝飾器的使用和如何控制用例的執(zhí)行順序
- Pytest框架之fixture的詳細(xì)使用教程
- python+pytest接口自動(dòng)化之token關(guān)聯(lián)登錄的實(shí)現(xiàn)
- Pytest使用fixture實(shí)現(xiàn)token共享的方法
相關(guān)文章
python在windows下實(shí)現(xiàn)ping操作并接收返回信息的方法
這篇文章主要介紹了python在windows下實(shí)現(xiàn)ping操作并接收返回信息的方法,實(shí)例分析了Python實(shí)現(xiàn)ping操作的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
如何搜索查找并解決Django相關(guān)的問(wèn)題
每個(gè)程序員都會(huì)在開(kāi)發(fā)過(guò)程中遇到這樣或那樣的問(wèn)題, 有時(shí)光靠一個(gè)人是無(wú)法解決所有問(wèn)題的, 所以我們應(yīng)該找到適當(dāng)?shù)牡胤教釂?wèn).2014-06-06
Python實(shí)現(xiàn)圖像尺寸和格式轉(zhuǎn)換處理的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)圖像尺寸獲取和格式轉(zhuǎn)換處理的功能,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2023-04-04
pandas 使用apply同時(shí)處理兩列數(shù)據(jù)的方法
下面小編就為大家分享一篇pandas 使用apply同時(shí)處理兩列數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
Python實(shí)現(xiàn)GUI計(jì)算器(附源碼)
這篇文章主要為大家詳細(xì)介紹了如何利用Python語(yǔ)言實(shí)現(xiàn)GUI計(jì)算器,可執(zhí)行復(fù)雜運(yùn)算,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2022-11-11
用Python展示動(dòng)態(tài)規(guī)則法用以解決重疊子問(wèn)題的示例
這篇文章主要介紹了用Python展示動(dòng)態(tài)規(guī)則法用以解決重疊子問(wèn)題的一個(gè)棋盤(pán)游戲的示例,動(dòng)態(tài)規(guī)劃常常適用于有重疊子問(wèn)題和最優(yōu)子結(jié)構(gòu)性質(zhì)的問(wèn)題,且耗時(shí)間往往遠(yuǎn)少于樸素解法,需要的朋友可以參考下2015-04-04
python3獲取兩個(gè)日期之間所有日期,以及比較大小的實(shí)例
下面小編就為大家分享一篇python3獲取兩個(gè)日期之間所有日期,以及比較大小的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04

