I have developed a Tkinter application which will basically display the test functions inside a test file and the user can select the particular test functions and run pytest on it. It was working well so far as I had only test functions and no classes. Now, there are classes and functions inside it. How do I capture that those functions come inside that particular class? I thought of using regex but there might be functions outside the class too. So, I dont know how to solve this issue.
So far I have something like this:
Test file:
def test_x():
....
def test_y():
....
Source Code:
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])
Based on which all selected:
#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)
From the above code, it will run: pytest testFile.py::test_x
if test_x
is selected.
Now if the test file is like this:
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_():
....
Now, if test_l
is selected, it should run: pytest testFile.py::test_Xyz::test_l
.
But how do I get the test_Xyz
above?
if test_j
is selected, it should run: pytest testFile.py::test_j
.
So, how do I capture the class name right outside a particular set of test functions and not capture if it's not inside the class?