A pytest fixture for changing current working directory.
Using chtmpdir
provides not only creating temporary directory but also moving there automatically.
def test_move_to_tmpdir(chtmpdir):
assert Path.cwd() == chtmpdir
assert str(chtmpdir).startswith("/tmp")
By using define_chdir_fixture
, you can create a fixture to move to specified directory.
define_chdir_fixture("chetc", Path("/etc"), __name__)
def test_chetc(chetc):
assert Path.cwd() == Path("/etc")
If you have installed pytest-datadir, you can use chdatadir
and chshared_datadir
.
def test_chdatadir(chdatadir):
assert str(Path.cwd().stem) == "test_dir"
def test_chshared_datadir(chshared_datadir):
assert str(Path.cwd().stem) == "data"
This software is released under the MIT License, see LICENSE.