43-pytest-内置fixture之临时目录使用


前言

  • 本篇来学习pytest中内置fixture中临时目录的使用

tmpdir

  • tmpdir作用范围是函数级别,创建临时文件供单个测试点调用
# -*- coding: utf-8 -*-
# @Time    : 2022/10/20
# @Author  : 大海

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 -*-
# @Time    : 2022/10/20
# @Author  : 大海

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 -*-
# @Time    : 2022/10/20
# @Author  : 大海

import os

def test_create_file_path(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

if __name__ == '__main__':
    os.system('pytest -s -v')

tmp_path_factory

  • 会话级别
# -*- coding: utf-8 -*-
# @Time    : 2022/10/20
# @Author  : 大海

import os

def test_create_file_path_factory(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_path_factory(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

if __name__ == '__main__':
    os.system('pytest -s -v')

指定临时目录

  • –basetemp = 临时路径
# -*- coding: utf-8 -*-
# @Time    : 2022/10/20
# @Author  : 大海

import os

def test_create_file_path(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

if __name__ == '__main__':
    # 指定临时目录,确认为空目录 否则会被清空
    os.system('pytest -s -v --basetemp=./test_tmp')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱学习de测试小白

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值