Issue
I used to have new test cases from a database based on tests having this format:
TEST_F(CLASSNAME, TESTCASENAME)
Now there is a test case with such a long name that the automatic formatting looks like this:
TEST_F(
CLASSNAME,
TESTCASENAME)
Before I get each piece of information based on the format TEST_F(CLASSNAME, TESTCASENAME) as follows:
print("Looking for new tests")
for root, dirs, files in os.walk(cpp_tests_dir_path):
for file in files:
with open(os.path.join(root, file), encoding="utf8", errors='ignore') as f:
for line in f:
if "TEST_F(" in line:
data = line.split("TEST_F(")[1]
c_class = data.split(",")[0]
c_test_case = data.split(",")[1].split(")")[0].strip().split()[0]
But if there is a test case with such a long name and format as I previously mentioned. Which already separated into multiple lines
TEST_F(
CLASSNAME,
TESTCASENAME)
How can I adjust the logic to get the value of each value?
Solution
This is where you need to use regular expressions. I don't know what your test cases look like, but if they are made of letters and numbers without spaces you can use this pattern to find the test cases.
'TEST_F\(\s*\w*\s*,\s*\w*\s*\)'
You can rewrite your code like this :
for root, dirs, files in os.walk(cpp_tests_dir_path):
for file in files:
with open(os.path.join(root, file), encoding="utf8", errors='ignore') as f:
content = f.read()
test_cases = re.findall(r'TEST_F\(\s*\w*\s*,\s*\w*\s*\)',content)
print(test_cases)
Now if your file's content is like this :
TEST_F(CLASSNAME1, TESTCASENAME1)
TEST_F(CLASSNAME2, TESTCASENAME2)
TEST_F(
CLASSNAME3,
TESTCASENAME
)
You'll get this as output :
['TEST_F(CLASSNAME1, TESTCASENAME1)', 'TEST_F(CLASSNAME2, TESTCASENAME2)', 'TEST_F(\n CLASSNAME3,\n TESTCASENAME\n )']
At this step you have all of your test cases in a list, It's up to you to get classname and testcasename from it.
Assuming that classname and testcasename are made of letters and underscore, you can update your code like this :
print("Looking for new tests")
for root, dirs, files in os.walk(cpp_tests_dir_path):
for file in files:
with open(os.path.join(root, file), encoding="utf8", errors='ignore') as f:
content = f.read()
test_cases = re.findall(r'TEST_F\(\s*\w*\s*,\s*\w*\s*\)', content)
for test_case in test_cases:
cleaned = re.sub(r'[\s]','',test_case) # removing white spaces
_, c_class, data = re.findall('\w+',cleaned)
print(_ , c_class, data) # checking results
As truly mentioned in comments, It can also be done using groups in regular expressions. The code for it is written bellow:
print("Looking for new tests")
for root, dirs, files in os.walk(cpp_tests_dir_path):
for file in files:
with open(os.path.join(root, file), encoding="utf8", errors='ignore') as f:
content = f.read()
for c_class, data in re.findall(r'TEST_F\(\s*(\w*)\s*,\s*(\w*)\s*\)', content):
print(c_class, data) # checking results
Answered By - Mohsen_Fatemi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.