Remove/replace some unneeded usages of py.path

This commit is contained in:
Ran Benita
2021-03-13 21:22:54 +02:00
parent cdbeb03aef
commit 59251e8a2a
18 changed files with 102 additions and 105 deletions

View File

@@ -148,10 +148,10 @@ For information about fixtures, see :ref:`fixtures`. To see a complete list of a
on warning categories.
tmpdir_factory [session scope]
Return a :class:`_pytest.tmpdir.TempdirFactory` instance for the test session.
Return a :class:`pytest.TempdirFactory` instance for the test session.
tmp_path_factory [session scope]
Return a :class:`_pytest.tmpdir.TempPathFactory` instance for the test session.
Return a :class:`pytest.TempPathFactory` instance for the test session.
tmpdir
Return a temporary directory path object which is unique to each test

View File

@@ -5,9 +5,9 @@ failure_demo = os.path.join(os.path.dirname(__file__), "failure_demo.py")
pytest_plugins = ("pytester",)
def test_failure_demo_fails_properly(testdir):
target = testdir.tmpdir.join(os.path.basename(failure_demo))
def test_failure_demo_fails_properly(pytester):
target = pytester.path.joinpath(os.path.basename(failure_demo))
shutil.copy(failure_demo, target)
result = testdir.runpytest(target, syspathinsert=True)
result = pytester.runpytest(target, syspathinsert=True)
result.stdout.fnmatch_lines(["*44 failed*"])
assert result.ret != 0

View File

@@ -12,8 +12,8 @@ pythonlist = ["python3.5", "python3.6", "python3.7"]
@pytest.fixture(params=pythonlist)
def python1(request, tmpdir):
picklefile = tmpdir.join("data.pickle")
def python1(request, tmp_path):
picklefile = tmp_path / "data.pickle"
return Python(request.param, picklefile)
@@ -30,8 +30,8 @@ class Python:
self.picklefile = picklefile
def dumps(self, obj):
dumpfile = self.picklefile.dirpath("dump.py")
dumpfile.write(
dumpfile = self.picklefile.with_name("dump.py")
dumpfile.write_text(
textwrap.dedent(
r"""
import pickle
@@ -46,8 +46,8 @@ class Python:
subprocess.check_call((self.pythonpath, str(dumpfile)))
def load_and_is_true(self, expression):
loadfile = self.picklefile.dirpath("load.py")
loadfile.write(
loadfile = self.picklefile.with_name("load.py")
loadfile.write_text(
textwrap.dedent(
r"""
import pickle

View File

@@ -768,8 +768,8 @@ case we just write some information out to a ``failures`` file:
mode = "a" if os.path.exists("failures") else "w"
with open("failures", mode) as f:
# let's also access a fixture for the fun of it
if "tmpdir" in item.fixturenames:
extra = " ({})".format(item.funcargs["tmpdir"])
if "tmp_path" in item.fixturenames:
extra = " ({})".format(item.funcargs["tmp_path"])
else:
extra = ""
@@ -781,7 +781,7 @@ if you then have failing tests:
.. code-block:: python
# content of test_module.py
def test_fail1(tmpdir):
def test_fail1(tmp_path):
assert 0
@@ -804,9 +804,9 @@ and run them:
================================= FAILURES =================================
________________________________ test_fail1 ________________________________
tmpdir = local('PYTEST_TMPDIR/test_fail10')
tmp_path = Path('PYTEST_TMPDIR/test_fail10')
def test_fail1(tmpdir):
def test_fail1(tmp_path):
> assert 0
E assert 0

View File

@@ -213,24 +213,24 @@ Request a unique temporary directory for functional tests
.. code-block:: python
# content of test_tmpdir.py
def test_needsfiles(tmpdir):
print(tmpdir)
# content of test_tmp_path.py
def test_needsfiles(tmp_path):
print(tmp_path)
assert 0
List the name ``tmpdir`` in the test function signature and ``pytest`` will lookup and call a fixture factory to create the resource before performing the test function call. Before the test runs, ``pytest`` creates a unique-per-test-invocation temporary directory:
List the name ``tmp_path`` in the test function signature and ``pytest`` will lookup and call a fixture factory to create the resource before performing the test function call. Before the test runs, ``pytest`` creates a unique-per-test-invocation temporary directory:
.. code-block:: pytest
$ pytest -q test_tmpdir.py
$ pytest -q test_tmp_path.py
F [100%]
================================= FAILURES =================================
_____________________________ test_needsfiles ______________________________
tmpdir = local('PYTEST_TMPDIR/test_needsfiles0')
tmp_path = Path('PYTEST_TMPDIR/test_needsfiles0')
def test_needsfiles(tmpdir):
print(tmpdir)
def test_needsfiles(tmp_path):
print(tmp_path)
> assert 0
E assert 0
@@ -238,10 +238,10 @@ List the name ``tmpdir`` in the test function signature and ``pytest`` will look
--------------------------- Captured stdout call ---------------------------
PYTEST_TMPDIR/test_needsfiles0
========================= short test summary info ==========================
FAILED test_tmpdir.py::test_needsfiles - assert 0
FAILED test_tmp_path.py::test_needsfiles - assert 0
1 failed in 0.12s
More info on tmpdir handling is available at :ref:`Temporary directories and files <tmpdir handling>`.
More info on temporary directory handling is available at :ref:`Temporary directories and files <tmpdir handling>`.
Find out what kind of builtin :ref:`pytest fixtures <fixtures>` exist with the command:

View File

@@ -194,7 +194,7 @@ It is possible to use fixtures using the ``getfixture`` helper:
.. code-block:: text
# content of example.rst
>>> tmp = getfixture('tmpdir')
>>> tmp = getfixture('tmp_path')
>>> ...
>>>

View File

@@ -284,9 +284,9 @@ Example of a fixture requiring another fixture:
.. code-block:: python
@pytest.fixture
def db_session(tmpdir):
fn = tmpdir / "db.file"
return connect(str(fn))
def db_session(tmp_path):
fn = tmp_path / "db.file"
return connect(fn)
For more details, consult the full :ref:`fixtures docs <fixture>`.

View File

@@ -190,21 +190,22 @@ and define the fixture function in the context where you want it used.
Let's look at an ``initdir`` fixture which makes all test methods of a
``TestCase`` class execute in a temporary directory with a
pre-initialized ``samplefile.ini``. Our ``initdir`` fixture itself uses
the pytest builtin :ref:`tmpdir <tmpdir>` fixture to delegate the
the pytest builtin :fixture:`tmp_path` fixture to delegate the
creation of a per-test temporary directory:
.. code-block:: python
# content of test_unittest_cleandir.py
import os
import pytest
import unittest
class MyTest(unittest.TestCase):
@pytest.fixture(autouse=True)
def initdir(self, tmpdir):
tmpdir.chdir() # change to pytest-provided temporary directory
tmpdir.join("samplefile.ini").write("# testdata")
def initdir(self, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path) # change to pytest-provided temporary directory
tmp_path.joinpath("samplefile.ini").write_text("# testdata")
def test_method(self):
with open("samplefile.ini") as f: