我是python的新手。我必须将此python代码作为机器人自动化框架的一部分运行。我有两个挑战
如果文件中的字符串匹配pass,我需要通过regex读取文件,这对我有用。但是,由于很多行也不匹配,因此对我来说也显示失败
我尝试使用异常处理,因为如果没有找到string,我需要通知Robot框架使此测试用例失败。
不幸的是,我以多种方式编写了代码片段,但是我的代码根本无法正常工作
code 1:
with open("C:/Users/diwak/Desktop/1.txt", "r") as a1:
for line in a1:
b = re.search('loopback4 is up, line protocol is up',line)
if b:
print ('found')
else:
print ("not found") -----> first issue displays all failed lines , i dont want this and second robot script simply passes as no exception was used here
code2:
import os
import re
with open("C:/Users/diwak/Desktop/1.txt", "r") as a1:
for line in a1:
b = re.search('loopback4 is up, line protocol is up',line)
if b:
print ('found')
else:
raise Exception ("not found") # if above line is not found robot is informed that test case is failed which is ok , but program exits midway without running further which i dont want
输出1:
python脚本说:
b找不到找不到
机器人脚本:
简单说“通过”
输出2:
python脚本:找不到
机器人脚本成功失败,但未执行下一个代码
请帮助