0
0
PyTesttesting~5 mins

tmp_path and tmp_path_factory in PyTest - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is tmp_path in pytest?

tmp_path is a built-in pytest fixture that provides a temporary directory as a pathlib.Path object for tests to use. It is unique for each test and is deleted after the test finishes.

Click to reveal answer
intermediate
How does tmp_path_factory differ from tmp_path?

tmp_path_factory is a pytest fixture that allows you to create multiple temporary directories during a test session. It is useful when you need more than one temporary directory, unlike tmp_path which provides only one per test.

Click to reveal answer
intermediate
How do you create a new temporary directory using tmp_path_factory?

You call tmp_path_factory.mktemp('name') to create a new temporary directory with the given name prefix. It returns a pathlib.Path object pointing to the new directory.

Click to reveal answer
beginner
Why is it better to use tmp_path or tmp_path_factory instead of hardcoding temp directories in tests?

Using tmp_path or tmp_path_factory ensures tests run in isolated, unique temporary directories. This avoids conflicts, keeps tests independent, and cleans up automatically after tests finish.

Click to reveal answer
beginner
What type of object do tmp_path and tmp_path_factory provide for temporary directories?

Both provide pathlib.Path objects, which are easy to use for file and directory operations in Python tests.

Click to reveal answer
What does the pytest fixture tmp_path provide?
AA permanent directory shared across tests
BA temporary directory as a pathlib.Path object unique to each test
CA temporary file object
DA database connection
Which pytest fixture allows creating multiple temporary directories during a test session?
Atmp_dir
Btmp_path
Ctmp_file
Dtmp_path_factory
How do you create a new temporary directory named 'data' using tmp_path_factory?
Atmp_path_factory.mktemp('data')
Btmp_path_factory.make('data')
Ctmp_path_factory.new_dir('data')
Dtmp_path_factory.create('data')
What type of object do tmp_path and tmp_path_factory return?
Apathlib.Path
Bos.DirEntry
Cstr
Dfile object
Why should tests use tmp_path instead of hardcoded temp directories?
ATo use less memory
BTo speed up tests
CTo avoid conflicts and ensure cleanup
DTo connect to a database
Explain how tmp_path and tmp_path_factory help manage temporary directories in pytest tests.
Think about how tests need isolated folders to avoid conflicts.
You got /4 concepts.
    Describe the steps to create and use a new temporary directory inside a test using tmp_path_factory.
    Focus on the method to create directories and how to use the path.
    You got /4 concepts.