GlaceCelery
GlaceCelery

Reputation: 1043

Copy contents of tmp_path -> tmp_path_factory for use in the next test

Test_1 writes out a file tree to the standard tmp_path fixture.

Test_2 requires this file tree as the input to its own test.

I am using shutil.copytree() however I am getting an error:

import pytest
import shutil
import os

def test_1(tmp_path, tmp_path_factory):
    p = tmp_path / "me"
    shutil.copytree(p, tmp_path_factory, dirs_exist_ok=True)
    assert True

def test_2(tmp_path_factory):
    l = list(tmp_path_factory.iterdir())
    print(l)
    assert False

Error:

p = TempPathFactory(_given_basetemp=None, _trace=<pluggy._tracing.TagTracerSub object at 0x000002161F3F4BE0>, _basetemp=WindowsPath('C:/Users/David/AppData/Local/Temp/pytest-of-David/pytest-1125'))

    def split(p):
        """Split a pathname.
    
        Return tuple (head, tail) where tail is everything after the final slash.
        Either part may be empty."""
>       p = os.fspath(p)
E       TypeError: expected str, bytes or os.PathLike object, not TempPathFactory

Any ideas? Many thanks :)

Upvotes: 2

Views: 2503

Answers (1)

GlaceCelery
GlaceCelery

Reputation: 1043

Thanks to @hoefling, this works

import pytest
import shutil
from directory_tree import display_tree


@pytest.fixture(scope="session")
def cache_dir(tmp_path_factory):
    return tmp_path_factory.mktemp("cache")#, numbered=False)


def test_1(tmp_path, cache_dir):
    p = tmp_path / "me.txt"
    p.touch()
    shutil.copytree(p.parent, cache_dir, dirs_exist_ok=True)
    assert True


def test_2(cache_dir):
    display_tree(cache_dir)
    assert False

Upvotes: 2

Related Questions