我正在运行以下命令,以仅运行位于名为test_CO2.py
的文件中的测试
python3.7 -m unittest discover -s some_path/tests/ -p "*CO2*"
我的文件夹结构如下所示:
some_path/tests/
CO2
test_CO2.py
battery
test_battery.py
tank
test_tank.py
我想指定运行的测试。例如,如果我只想测试储罐和CO2代码,我该怎么做?我想通过以下正则表达式:
\w*(CO2|tank)\w*.py
找不到任何测试
我认为传递给-p
选项的模式不接受正则表达式。那么,如何指定希望运行的测试
通常,通过} method 处理,然后调用函数链} library
-p
参数传递到unittest
的所有内容都通过^{fnmatch()
→fnmatchcase()
→_compile_pattern()
→translate()
来自^{函数
translate()
将原始-p
参数转换为正则表达式,然后用于名称匹配。fnmatch()
函数的文档说明:据我所见,这就是它所能做到的程度所有其他字符都将转义,以便按字面进行匹配
示例:我将regex
a|b
作为模式传递。translate()
函数返回形式为(?s:p\|m)\Z
的最终正则表达式。管道字符在那里被转义如果您特别好奇,请查看
fnmatch
lib的translate()
函数here——如果您想知道将“glob-like”模式转换为最终正则表达式的确切过程我从unittest中找到了绕过这个限制的方法
我可以使用
python3.7 -m unittest path_to_some_test
运行特定的测试相关问题 更多 >
编程相关推荐