我目前正在使用pytest来自动化我的测试套件。我的测试分布在以下几个目录中:
|-Root
| |-Dir1
| | |-Test1
| | |-Test2
| | |-Test3
| |-Dir2
| | |-Testa
| | |-Testb
| | |-Testc
| |-Dir3
| | |-TestI
| | |-TestII
| | |-TestIII
我希望能够通过排除其他目录在一组目录中运行测试。某些目录具有类似名称,如“test_set_1”和“test_set_1_extended”。我想排除“test_set_1”但保留“test_set_1_extended”,因此我需要将完整路径用作关键字。我注意到在使用关键字(-k选项)时pytest不能很好地处理目录结构字符串作为参数。例如,如果我尝试windows风格
py.test -k "not (root\dir1 or dir2)"
我收到语法错误:
SyntaxError:行继续符后的意外字符
另一方面,如果我尝试unix风格
py.test -k "not (root/dir1 or dir2)"
我得到ZeroDivisionError:
ZeroDivisionError:整数除法或模数为零
我尝试过的事情包括: 用单引号或双引号括住目录字符串:
py.test -k "not ('root/dir1' or dir2)"
py.test -k "not ("root/dir1' or dir2)"
使用单独的'-k'':
py.test -k "not root/dir1" -k not dir2)
使用当前目录'。'
py.test -k "not (./root/dir1 or dir2)"
逃避斜线:
py.test -k "not (root\\dir1 or dir2)"
py.test -k "not (root\/dir1 or dir2)"
这些都没有解决问题。有没有人对如何让pytest正确处理我的路径字符串有任何建议?
答案 0 :(得分:1)
为什么不为自己保存解决方法,只为不同的测试组使用唯一标记,然后使用
py.test -m "not extended"
这样,测试/目录/模块名称可以更改,并且没有任何中断。
答案 1 :(得分:0)
我正在使用非常类似的设置,并且我能够重现您描述的行为(pytest 3.4.0
,python 3.6.4
)。
我找到了一个可能对您有用的解决方法,具体取决于您的确切布局)。基本上,我使用and
而不是路径分隔符。
因此py.test -k "not (root/dir1 or dir2)"
变为py.test -k "not ((root and dir1) or dir2)"
。如果dir1
下没有任何root/dir1
路径元素,则两个表达式应该是等效的。
由于包含/
的表达式的行为似乎有点不一致(py.test -k root/dir1
按预期工作),因此提交错误可能是有意义的。