1.相关的fixture
1.1 tmp_path
tmp_path是一个用例级别的fixture,其作用是返回一个唯一的临时目录对象(pathlib.Path)
CONTENT = "content"
def test_create_file(tmp_path):
d = tmp_path / "sub"
d.mkdir() #创建一个子目录
p = d/"hello.txt"
p.write_text(CONTENT)
assert p.read_text() == CONTENT
assert len(list(tmp_path.iterdir())) == 1 #iterdir()迭代目录,返回迭代器
assert 0 #为了展示,强制为失败
返回结果:
D:\pytest\exercise\chapter6>pytest test_tmp_path.py
================================================================= test session starts ==================================================================
platform win32 -- Python 3.8.5, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: D:\pytest\exercise\chapter6
plugins: allure-pytest-2.8.36
collected 1 item
test_tmp_path.py F [100%]
======================================================================= FAILURES =======================================================================
_________________