【发布时间】:2022-01-19 07:24:21
【问题描述】:
我有一个存储在文本文件中的文件列表,如果在该列表中找到 Python 文件。我想用Pytest对应的测试文件。
我的文件如下所示:
/folder1/file1.txt
/folder1/file2.jpg
/folder1/file3.md
/folder1/file4.py
/folder1/folder2/file5.py
当找到第 4/5 个文件时,我想运行命令 pytest,如:
pytest /folder1/test_file4.py
pytest /folder1/folder2/test_file5.py
目前,我正在使用这个命令:
cat /workspace/filelist.txt | while read line; do if [[ $$line == *.py ]]; then exec "pytest test_$${line}"; fi; done;
这不能正常工作,因为我在文本中也有文件路径。知道如何实现吗?
【问题讨论】:
-
为什么你的文件中有这个列表?
pytest folder1或find /folder1 -type f -name '*.py' -exec pytest {} \;呢?如果确实需要用到文件,可以sed -n 's/.*\.py$/pytest "&"/p' | bash -
我正在拉取 Github Pull Request 中包含的文件列表,并检查这些更改的文件中是否存在 Python 文件,如果存在,则需要执行其对应的 pytest 文件。
-
为什么
pytest /folder1/folder2/file5.py没有test_前缀?是不是打错字了? -
是的。已更正。