英文:
How to get pytest to not delete successful test directories
问题
I'm using pytest and it's been great. My tests use the tmp_path fixture to hold a bunch of outputs (log files, plots, etc.). I want to keep these so I have a record of what the performance of all tests are, not just pass/fail. That way as I make changes I can see whether things are faster or slower, diff the logs, etc. Currently pytest only keeps the directories for tests that fail. I've been through the documentation and can't find any flags that will convince it to do otherwise.
Here's a sample test:
from pathlib import Path
def test_file_to_keep(tmp_path):
# write some outputs from the test.
my_file_to_keep = Path(tmp_path) / "my_file.txt";
with open(my_file_to_keep, 'w') as out_file:
out_file.writelines("some good output.")
assert my_file_to_keep.exists()
I want to be able to look at the tmp_path directory and examine the contents of my_file.txt.
英文:
I'm using pytest and it's been great. My tests use the tmp_path fixture to hold a bunch of outputs (log files, plots, etc.). I want to keep these so I have a record of what the performance of all tests are, not just pass/fail. That way as I make changes I can see whether things are faster or slower, diff the logs, etc. Currently pytest only keeps the directories for tests that fail. I've been through the documentation and can't find any flags that will convince it to do otherwise.
Here's a sample test:
from pathlib import Path
def test_file_to_keep(tmp_path):
# write some outputs from the test.
my_file_to_keep = Path(tmp_path) / "my_file.txt"
with open(my_file_to_keep, 'w') as out_file:
out_file.writelines("some good output.")
assert my_file_to_keep.exists()
I want to be able to look at the tmp_path directory and examine the contents of my_file.txt.
答案1
得分: 2
使用tmp_path创建的临时目录会在每次测试运行后保留,即使测试通过。
运行你的示例代码,我可以在终端中执行:
marco@sol:/tmp/pytest-of-marco/pytest-current/test_file_to_keep0$ cat my_file.txt
some good output.
请注意,保留策略可以通过tmp_path_retention_count和tmp_path_retention_policy变量进行配置,如文档中所述 here。
还请注意,你不需要使用 Path(tmp_path) 创建一个 Path 对象,因为 tmp_path 是一个 pathlib.Path 对象,如文档中描述的 documentation。
你甚至可以简化它:
def test_file_to_keep(tmp_path):
# 写入测试中的一些输出。
my_file_to_keep = tmp_path / "my_file.txt"
my_file_to_keep.write_text("some good output.")
assert my_file_to_keep.exists()
英文:
The temporary directories created with tmp_path are kept after each test run even if the test passes.
Running your example code, I can go to a terminal and do:
marco@sol:/tmp/pytest-of-marco/pytest-current/test_file_to_keep0$ cat my_file.txt
some good output.
Please note that the retention policy can be configured with the tmp_path_retention_count and tmp_path_retention_policy variables, as described in the documentation here.
Also please note that you don't need to create a Path object with Path(tmp_path), as tmp_path is a pathlib.Path object, as described on the documentation.
You could even simplify it with:
def test_file_to_keep(tmp_path):
# write some outputs from the test.
my_file_to_keep = tmp_path / "my_file.txt"
my_file_to_keep.write_text("some good output.")
assert my_file_to_keep.exists()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论