打了很多字发现无法附加内容和修改内容,就在评论区回了
先简述下我当前项目把,大概就是 pytest+yaml 的项目,其中 yaml 中有一个含 SQL 语句的字段,该字段大概作用是:在执行中,把该语句执行后查询出来的结果存起来,供给其他地方用。
而这里只支持拿取数据库中单个值,比如:
SELECT `phone` FROM `test`.`users` WHERE `phone` = 'XXXXX'
如果此字段填写 SQL 语句外的字符,如:aaa,在执行中会报错,并且最后断言也是显示失败的。
common\test_Template.py [2021-05-10 23:09:34,485]-logs.py:46]-ERROR:执行sql语句报错:(1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'aa' at line 1")
Traceback (most recent call last):
File "C:\Users\86139\PycharmProjects\connector\common\execSql.py", line 85, in exec_sql
cursor.execute(sql)
File "C:\Users\86139\PycharmProjects\connector\venv\lib\site-packages\pymysql\cursors.py", line 148, in execute
result = self._query(query)
File "C:\Users\86139\PycharmProjects\connector\venv\lib\site-packages\pymysql\cursors.py", line 310, in _query
conn.query(q)
File "C:\Users\86139\PycharmProjects\connector\venv\lib\site-packages\pymysql\connections.py", line 548, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "C:\Users\86139\PycharmProjects\connector\venv\lib\site-packages\pymysql\connections.py", line 775, in _read_query_result
result.read()
File "C:\Users\86139\PycharmProjects\connector\venv\lib\site-packages\pymysql\connections.py", line 1156, in read
first_packet = self.connection._read_packet()
File "C:\Users\86139\PycharmProjects\connector\venv\lib\site-packages\pymysql\connections.py", line 725, in _read_packet
packet.raise_for_error()
File "C:\Users\86139\PycharmProjects\connector\venv\lib\site-packages\pymysql\protocol.py", line 221, in raise_for_error
err.raise_mysql_exception(self._data)
File "C:\Users\86139\PycharmProjects\connector\venv\lib\site-packages\pymysql\err.py", line 143, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'aa' at line 1")
但是如果此字段填写正确的 SQL 语句,但是查询到的值有多个的情况下,如:
SELECT * FROM `test`.`users` WHERE `phone` LIKE 'XXXXX'
在执行中控制台也会报错,但是断言结果却是通过。
用例执行失败!
用例路径:testcase\其他_测试_01.yaml
用例标题:登录测试
报错信息:
catching classes that do not inherit from BaseException is not allowed
Traceback (most recent call last):
File "C:\Users\86139\PycharmProjects\connector\common\customVar.py", line 82, in init_sql
sql_dict.update({keys: eval(sql_value)})
File "<string>", line 1
61e9326ec6e748bf67110e30ae8a04a4
^
SyntaxError: unexpected EOF while parsing
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\86139\PycharmProjects\connector\common\test_Template.py", line 123, in test_in_theaters
pre_sql = CustomVar().init_sql(test_content_list['pre_sql']) # 字典类型
File "C:\Users\86139\PycharmProjects\connector\common\customVar.py", line 84, in init_sql
except ExecSql as e:
TypeError: catching classes that do not inherit from BaseException is not allowed
因为我这边在断言前就会先输出好一些测试报告,这里我发现断言前的测试报告都没有输出,怀疑是报错后就没有继续执行了。
但很好奇同样都是报错,一个却能在报错后还能继续断言,并且正常输出测试报告,一个却在报错后没有继续向下输出测试报告,并且没有断言。
后来发现报错但断言通过的用例中,在报错后并没有继续往下执行代码,是不是这个原因导致的没有执行断言而默认成功?
而第一种情况失败了也会继续执行往下执行代码。
最后贴上执行错误的代码:
def init_sql(self, content):
try:
sql_dict = {}
for keys, value in content.items():
sql_value = ExecSql().exec_sql(value)
if isinstance(sql_value, dict) and sql_value is not None:
sql_dict.update({keys: list(sql_value)[1]})
if isinstance(sql_value, str) and sql_value is not None:
sql_dict.update({keys: eval(sql_value)})
return sql_dict
except ExecSql as e:
self.log.error('初始化SQL语句失败:{}'.format(e))
try:
XXXXX
pre_sql = CustomVar().init_sql(test_content_list['pre_sql']) # pre_sql就是用例中的SQL语句
XXXXX
except Exception as e:
self.log.error("\n\n用例执行失败!\n用例路径:{0}\n用例标题:{1}\n报错信息:\n{2}".format(path_file_list, test_content_list['title'], e))