Python的unittest库如何匹配通过-p参数传递的模式?

问题描述 投票:2回答:2

我正在运行以下命令,仅运行位于名为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

我想指定已运行的测试。例如,如果我只想测试水箱和二氧化碳代码,该怎么办?我想到了通过以下正则表达式:\w*(CO2|tank)\w*.py找不到任何测试。

我认为传递给-p选项的模式不接受正则表达式。如何,然后我可以指定要运行的测试?

我正在运行以下命令,仅运行位于名为test_CO2.py的文件中的测试python3.7 -m unittest discover -s some_path / tests / -p“ * CO2 *”我的文件夹结构看起来像...

python regex python-3.x python-unittest
2个回答
2
投票

[通常,您通过-p参数传递到unittest的所有内容都会通过TestLoader::_match_path() method处理,然后调用TestLoader::_match_path()fnmatch()fnmatchcase()_compile_pattern()函数链。] >

translate()函数将原始的fnmatch library参数转换为正则表达式,然后将其用于名称匹配。


0
投票

我想出了如何绕过unittest的限制。

© www.soinside.com 2019 - 2024. All rights reserved.