最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

pytest內(nèi)置fixture使用臨時(shí)目錄流程詳解

 更新時(shí)間:2022年12月17日 10:18:41   作者:愛(ài)學(xué)習(xí)de測(cè)試小白  
fixture是在測(cè)試函數(shù)運(yùn)行前后,由pytest執(zhí)行的外殼函數(shù)。fixture中的代碼可以定制,滿足多變的測(cè)試需求,包括定義傳入測(cè)試中的數(shù)據(jù)集、配置測(cè)試前系統(tǒng)的初始狀態(tài)、為批量測(cè)試提供數(shù)據(jù)源等等。fixture是pytest的精髓所在

前言

本篇來(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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

饶阳县| 阿克苏市| 清流县| 固原市| 岳普湖县| 宜君县| 广德县| 江华| 阳西县| 大洼县| 滨州市| 香格里拉县| 宜丰县| 贡嘎县| 兴隆县| 铁岭市| 繁昌县| 年辖:市辖区| 镇安县| 南涧| 安达市| 文登市| 贵阳市| 龙州县| 田阳县| 灵川县| 富民县| 资中县| 麻城市| 荆州市| 福建省| 辽源市| 探索| 武威市| 大厂| 称多县| 通海县| 广饶县| 霞浦县| 河津市| 牟定县|