Pytest----如何使用临时目录和文件

简介: Pytest----如何使用临时目录和文件

一、测试用例级的临时路径fixture:tmp_path

tmp_path 是一个testcase级别的fixture,返回的是pathlib.Path类型值,可以用于创建一个独一无二的临时目录,主要用于比如测试写文件之类场景,默认的会存放在系统的临时目录下,同时创建pytest-N的目录,其中N是会不断的自加1

test_demo.py代码如下:

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

执行结果如下,这里将临时目录打印了处来,如:C:\Users\Administrator\AppData\Local\Temp\pytest-of-Administrator\pytest-14\test_create_file0\sub,这里的14是不断变化的,再执行一次就会自动加1,同时只保留最新的三个,这就保证了每次跑出来的数据后续会自动删除

$ pytest -s
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.6, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: G:\src\blog
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 1 item                                                                                                                                                        

tests\test_demo.py temp_dir:C:\Users\Administrator\AppData\Local\Temp\pytest-of-Administrator\pytest-14\test_create_file0\sub
.

========================================================================== 1 passed in 0.60s ===========================================================================

二、测试会话级的临时路径fixture:tmp_path_factory

tmp_path_factory是一个session级别的fixture,每次执行只会创建一个临时目录

test_demo.py代码如下:

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

执行结果如下,虽然上面在两个用例中都调用了此fixture,但是只创建一个临时目录pytest-15,这就是因为它是session级别的原因

$ pytest -s
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.6, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: G:\src\blog
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 2 items                                                                                                                                                       

tests\test_demo.py temp_dir:C:\Users\Administrator\AppData\Local\Temp\pytest-of-Administrator\pytest-15\demo010\hello.txt
.temp_dir:C:\Users\Administrator\AppData\Local\Temp\pytest-of-Administrator\pytest-15\demo020\hello.txt
.

========================================================================== 2 passed in 0.61s ===========================================================================

三、测试用例级的临时路径fixture:tmpdir

tmpdir 和tmp_path功能是一样的,唯一区别是tmpdir返回的是py.path.local类型,而tmp_path返回的是pathlib.Path类型的,tmpdir返回的值主要用于支持os.path的一些操作方法,同时tmpdir也是一个testcase级别的fixture

test_demo.py代码如下:

def test_create_file(tmpdir):
    p = tmpdir.mkdir("sub").join("hello.txt")
    print(f"tmpdir:{p}")
    p.write("content")
    assert p.read() == "content"
    assert len(tmpdir.listdir()) == 1

执行结果如下

$ pytest -s
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.6, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: G:\src\blog
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 1 item                                                                                                                                                        

tests\test_demo.py tmpdir:C:\Users\Administrator\AppData\Local\Temp\pytest-of-Administrator\pytest-16\test_create_file0\sub\hello.txt
.

========================================================================== 1 passed in 0.62s ===========================================================================

四、测试会话级的临时路径fixture:tmpdir_factory

同样tmpdir_factory和tmp_path_factory功能越是一样的,是一个session级别的fixture,一次执行只会创建一个临时目录

test_demo.py代码如下:

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"

执行结果如下,可以发现,这里面同样执行了两个脚本,只创建了一个pytest-17的目录,这就是这个fixture是session级别的原因

pytest -s
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.6, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: G:\src\blog
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 2 items                                                                                                                                                       

tests\test_demo.py tmpdir:C:\Users\Administrator\AppData\Local\Temp\pytest-of-Administrator\pytest-17\demo010\hello.txt
.tmpdir:C:\Users\Administrator\AppData\Local\Temp\pytest-of-Administrator\pytest-17\demo020\hello.txt
.

========================================================================== 2 passed in 0.62s ===========================================================================
目录
相关文章
|
9月前
|
Python
Python代码扫描目录下的文件并获取路径
【5月更文挑战第12天】Python代码扫描目录下的文件并获取路径
157 1
|
4月前
|
Python
Python实用记录(六):如何打开txt文档并删除指定绝对路径下图片
这篇文章介绍了如何使用Python打开txt文档,删除文档中指定路径的图片,并提供了一段示例代码来展示这一过程。
49 1
|
6月前
|
Java
java中实现File文件的重命名(renameTo)、将文件移动到其他目录下、文件的复制(copy)、目录和文件的组合(更加灵活方便)
这篇文章介绍了Java中使用`renameTo()`、`Files.copy()`等方法对文件进行重命名、移动和复制的操作,并提供了代码实例和测试效果。
java中实现File文件的重命名(renameTo)、将文件移动到其他目录下、文件的复制(copy)、目录和文件的组合(更加灵活方便)
|
7月前
|
存储 Python
`tempfile`模块在Python中用于创建临时文件和目录。
`tempfile`模块在Python中用于创建临时文件和目录。
php案例:用代码的方式创建目录+文件+写入数据(都由你定)
php案例:用代码的方式创建目录+文件+写入数据(都由你定)
php案例:用代码的方式创建目录+文件+写入数据(都由你定)
|
数据挖掘 Python
一日一技:在Python中创建临时文件用于记录临时数据
一日一技:在Python中创建临时文件用于记录临时数据
131 0
|
数据采集 Shell 开发工具
[oeasy]python0028_直接运行_修改py文件执行权限_设置py文件打开方式
[oeasy]python0028_直接运行_修改py文件执行权限_设置py文件打开方式
102 0
[oeasy]python0028_直接运行_修改py文件执行权限_设置py文件打开方式
|
Go Python
Go-文件目录操作分类详解(创建、打开、关闭、读取、写入、判断等)
Go-文件目录操作分类详解(创建、打开、关闭、读取、写入、判断等)
467 0
Go-文件目录操作分类详解(创建、打开、关闭、读取、写入、判断等)
|
Python
【实用小脚本】Python实现文件/目录的复制
【实用小脚本】Python实现文件/目录的复制
188 0
|
测试技术
Pytest----自动化脚本的加载原理
Pytest----自动化脚本的加载原理
500 1