我开发了一个Tkinter应用程序,它基本上会在测试文件中显示测试函数,用户可以选择特定的测试函数并在其上运行pytest。它工作得很好,因为我只有测试函数,没有类。现在,它里面有类和函数。我如何捕获这些函数位于特定类中?我曾想过使用正则表达式,但类之外可能也有函数。所以,我不知道如何解决这个问题
到目前为止,我有这样的想法:
测试文件:
def test_x():
....
def test_y():
....
源代码:
with open("{}.py".format(testFile), "r") as fp:
line = fp.readline()
while line:
line = fp.readline()
if ("#" not in line) and ("def" and "test_" in line):
x = line.split()[1].split('(')[0]
gFunctionList.append([testName, x])
根据选择的所有选项:
#var2State is the checkbutton states
for j in range(len(var2State)):
if var2State[j].get() == 1:
runString += "{}.py::{} ".format(gFunctionList[j][0],
gFunctionList[j][1])
else:
continue
if runString != "":
res = os.system("pytest " + runString)
从上面的代码中,如果选择了test_x
,它将运行:pytest testFile.py::test_x
现在,如果测试文件如下所示:
Class test_Abc():
def test_x():
....
def test_y():
....
def test_j():
....
Class test_Xyz():
def k():
....
def test_l():
....
Class test_Rst():
def test_k():
....
def ltest_():
....
现在,如果选择了test_l
,它应该运行:pytest testFile.py::test_Xyz::test_l
但是我如何得到上面的test_Xyz
如果选择了test_j
,它应该运行:pytest testFile.py::test_j
那么,如何在一组特定的测试函数之外捕获类名,而如果类名不在类内部,如何不捕获呢
我对Tkinter或事物是如何被选择的并不太熟悉,但这可能会为你指明正确的方向
正如我在注释中提供的链接所示,实例(即类)没有名称。如果要为实例命名,只需将其包含在
def __init__(self):
中即可。当调用来自test_ABC
(也继承self
)的任何方法(即函数)时,您将始终可以访问self.name
返回:
相关问题 更多 >
编程相关推荐