一、测试用例级的临时路径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 ===========================================================================