Complete the code to use the pytest fixture that provides a temporary directory as a pathlib.Path object.
def test_create_file([1]): file = [1] / "test.txt" file.write_text("hello") assert file.read_text() == "hello"
tmp_path_factory directly in the test function instead of tmp_path.tmpdir which returns a py.path.local object, not pathlib.Path.The tmp_path fixture provides a temporary directory as a pathlib.Path object for the test.
Complete the code to create a new temporary directory inside the base temporary directory using tmp_path_factory.
def test_new_temp_dir(tmp_path_factory): new_dir = tmp_path_factory.[1]("mydir") assert new_dir.exists() and new_dir.is_dir()
mkdir which is a pathlib method, not a tmp_path_factory method.mktemp.The mktemp method of tmp_path_factory creates a new temporary directory with the given name.
Fix the error in the test code to correctly create a file inside the temporary directory provided by tmp_path.
def test_write_file(tmp_path): file_path = tmp_path / "data.txt" with open([1], "w") as f: f.write("data") assert file_path.read_text() == "data"
tmp_path to open().The variable file_path holds the full path to the file and should be used in open().
Fill both blanks to create a new temporary directory and a file inside it using tmp_path_factory.
def test_tmp_path_factory(tmp_path_factory): temp_dir = tmp_path_factory.[1]("session") file = temp_dir / [2] file.write_text("hello") assert file.read_text() == "hello"
mkdir instead of mktemp for tmp_path_factory.Use mktemp to create the directory and a string filename like "test.txt" for the file.
Fill all three blanks to create a temporary directory, a file inside it, and assert the file content using tmp_path_factory.
def test_full_tmp_path_factory(tmp_path_factory): dir_path = tmp_path_factory.[1]("run") file_path = dir_path / [2] file_path.write_text([3]) assert file_path.read_text() == "pytest rocks"
mkdir instead of mktemp.Use mktemp to create the directory, a filename string for the file, and the string content to write.