目錄html
fixture
tmp_path
tmp_path
是一個用例級別的fixture
,其做用是返回一個惟一的臨時目錄對象(pathlib.Path
);node
咱們看下面的例子:python
# src/chapter-6/test_tmp_path.py 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 # 爲了展現,強制置爲失敗
執行:git
λ pipenv run pytest -q -s src/chapter-6/test_tmp_path.py F ==================================== FAILURES ===================================== ________________________________ test_create_file _________________________________ tmp_path = WindowsPath('C:/Users/luyao/AppData/Local/Temp/pytest-of-luyao/pytest-4/test_create_file0') 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 # 爲了展現,強制置爲失敗 E assert 0 src\chapter-6\test_tmp_path.py:32: AssertionError 1 failed in 0.06s
能夠看出:github
tmp_path
在不一樣的操做系統中,返回的是不一樣類型的pathlib.Path
對象,這裏Windows
系統下返回的是WindowsPath
對象,它是Path
的子類對象;Path
對象能夠使用/
操做符代替經常使用的os.path.join()
的方法;更多關於pathlib
的使用方法能夠查看:https://docs.python.org/3.7/library/pathlib.htmltmp_path_factory
tmp_path_factory
是一個會話級別的fixture
,其做用是在其它fixture
或者用例中建立任意的臨時目錄;api
查看上一章tmp_path fixture
的源碼,咱們可以看到tmp_path
就是使用tmp_path_factory
的一個例子:bash
# _pytest.tmpdir @pytest.fixture def tmp_path(request, tmp_path_factory): """Return a temporary directory path object which is unique to each test function invocation, created as a sub directory of the base temporary directory. The returned object is a :class:`pathlib.Path` object. .. note:: in python < 3.6 this is a pathlib2.Path """ return _mk_tmp(request, tmp_path_factory) @pytest.fixture(scope="session") def tmp_path_factory(request): """Return a :class:`_pytest.tmpdir.TempPathFactory` instance for the test session. """ return request.config._tmp_path_factory
能夠看出:session
tmp_path
調用了tmp_path_factory
;tmp_path_factory
返回一個_pytest.tmpdir.TempPathFactory
對象;進一步查看_mk_tmp
的源碼:測試
def _mk_tmp(request, factory): name = request.node.name name = re.sub(r"[\W]", "_", name) MAXVAL = 30 name = name[:MAXVAL] return factory.mktemp(name, numbered=True)
能夠看出,tmp_path
最終調用了TempPathFactory.mktemp()
方法,它返回的是一個pathlib.Path
對象;ui
tmpdir
tmp_path
是一個用例級別的fixture
,其做用是返回一個惟一的臨時目錄對象(py.path.local),它提供os.path
的方法;
上面的例子也能夠修改爲以下這樣:
# src/chapter-6/test_tmpdir.py CONTENT = "content" def test_create_file(tmpdir): p = tmpdir.mkdir("sub").join("hello.txt") # 建立子文件夾,並新建文件 p.write(CONTENT) assert p.read() == CONTENT assert len(tmpdir.listdir()) == 1 # iterdir() 迭代目錄,返回列表 assert 0 # 爲了展現,強制置爲失敗
執行:
λ pipenv run pytest -q -s src/chapter-6/test_tmpdir.py F ==================================== FAILURES ===================================== ________________________________ test_create_file _________________________________ tmpdir = local('C:\\Users\\luyao\\AppData\\Local\\Temp\\pytest-of-luyao\\pytest-6\\test_create_file0') def test_create_file(tmpdir): p = tmpdir.mkdir("sub").join("hello.txt") # 建立子文件夾,並新建文件 p.write(CONTENT) assert p.read() == CONTENT assert len(tmpdir.listdir()) == 1 # iterdir() 迭代目錄,返回列表 > assert 0 # 爲了展現,強制置爲失敗 E assert 0 src\chapter-6\test_tmpdir.py:30: AssertionError 1 failed in 0.06s
其實,tmpdir
也調用了tmp_path
,只是對返回值作了一次py.path.local()
封裝:
# _pytest.tmpdir @pytest.fixture def tmpdir(tmp_path): """Return a temporary directory path object which is unique to each test function invocation, created as a sub directory of the base temporary directory. The returned object is a `py.path.local`_ path object. .. _`py.path.local`: https://py.readthedocs.io/en/latest/path.html """ return py.path.local(tmp_path)
tmpdir_factory
tmpdir_factory
是一個會話級別的fixture
,其做用是在其它fixture
或者用例中建立任意的臨時目錄;
假設,一個測試會話須要使用到一個很大的由程序生成的圖像文件,相比於每一個測試用例生成一次文件,更好的作法是每一個會話只生成一次:
import pytest @pytest.fixture(scope="session") def image_file(tmpdir_factory): img = compute_expensive_image() fn = tmpdir_factory.mktemp("data").join("img.png") img.save(str(fn)) return fn def test_histogram(image_file): img = load_image(image_file) # compute and test histogram
fixture | 做用域 | 返回值類型 |
---|---|---|
tmp_path | 用例級別(function) | pathlib.Path |
tmp_path_factory | 會話級別(session) | TempPathFactory |
tmpdir | 用例級別(function) | py.local.path |
tmpdir_factory | 會話級別(session) | TempDirFactory |
上述fixture
在建立臨時目錄時,都是建立在系統默認的臨時目錄(例如:Windows
系統的%temp%
目錄)下;你能夠經過指定--basetemp=mydir
選項自定義默認的基本臨時目錄;
λ pipenv run pytest -q -s --basetemp="/d/temp" src/chapter-6/test_tmpdir.py F ==================================== FAILURES ===================================== ________________________________ test_create_file _________________________________ tmpdir = local('D:\\temp\\test_create_file0') def test_create_file(tmpdir): p = tmpdir.mkdir("sub").join("hello.txt") # 建立子文件夾,並新建文件 p.write(CONTENT) assert p.read() == CONTENT assert len(tmpdir.listdir()) == 1 # iterdir() 迭代目錄,返回列表 > assert 0 # 爲了展現,強制置爲失敗 E assert 0 src\chapter-6\test_tmpdir.py:30: AssertionError 1 failed in 0.04s