mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-17 15:27:35 +08:00
Merge branch 'master' into web
This commit is contained in:
@@ -1,5 +1,16 @@
|
|||||||
# Release History
|
# Release History
|
||||||
|
|
||||||
|
## 2.5.7 (2020-02-21)
|
||||||
|
|
||||||
|
**Changed**
|
||||||
|
|
||||||
|
- feat: validate with python script, display print message
|
||||||
|
|
||||||
|
**Fixed**
|
||||||
|
|
||||||
|
- fix: validate script missing indents in html report
|
||||||
|
- fix: validate with python script, display lineno error
|
||||||
|
|
||||||
## 2.5.6 (2020-02-19)
|
## 2.5.6 (2020-02-19)
|
||||||
|
|
||||||
**Added**
|
**Added**
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
__version__ = "2.5.6"
|
__version__ = "2.5.7"
|
||||||
__description__ = "One-stop solution for HTTP(S) testing."
|
__description__ = "One-stop solution for HTTP(S) testing."
|
||||||
|
|
||||||
__all__ = ["__version__", "__description__"]
|
__all__ = ["__version__", "__description__"]
|
||||||
|
|||||||
@@ -302,16 +302,16 @@
|
|||||||
{% if validate_script %}
|
{% if validate_script %}
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<th>validate script</th><th>exception</th>
|
<th>validate script</th><th>output</th>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{validate_script.validate_script | safe}}</td>
|
<td><pre>{{validate_script.validate_script | safe}}</pre></td>
|
||||||
{% if validate_script.check_result == "pass" %}
|
{% if validate_script.check_result == "pass" %}
|
||||||
<td class="passed">
|
<td class="passed">
|
||||||
{% elif validate_script.check_result == "fail" %}
|
{% elif validate_script.check_result == "fail" %}
|
||||||
<td class="failed">
|
<td class="failed">
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{{validate_script.exception}}
|
{{validate_script.output}}
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import traceback
|
||||||
|
|
||||||
from httprunner import exceptions, logger, parser
|
from httprunner import exceptions, logger, parser
|
||||||
|
|
||||||
|
|
||||||
@@ -60,54 +63,66 @@ class Validator(object):
|
|||||||
def validate_script(self, script):
|
def validate_script(self, script):
|
||||||
""" make validation with python script
|
""" make validation with python script
|
||||||
"""
|
"""
|
||||||
validator_dict = {
|
result = {
|
||||||
"validate_script": "<br/>".join(script),
|
"validate_script": "<br/>".join(script),
|
||||||
"check_result": "fail",
|
"check_result": "pass",
|
||||||
"exception": ""
|
"output": ""
|
||||||
}
|
}
|
||||||
|
|
||||||
script = "\n ".join(script)
|
script = "\n ".join(script)
|
||||||
code = """
|
code = """
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
|
||||||
try:
|
def run_validate_script():
|
||||||
{}
|
{}
|
||||||
except Exception as ex:
|
|
||||||
import traceback
|
|
||||||
import sys
|
|
||||||
_type, _value, _tb = sys.exc_info()
|
|
||||||
# filename, lineno, name, line
|
|
||||||
_, _lineno, _, line_content = traceback.extract_tb(_tb, 1)[0]
|
|
||||||
|
|
||||||
line_no = _lineno - 4
|
|
||||||
|
|
||||||
c_exception = _type.__name__ + "\\n"
|
|
||||||
c_exception += "\\tError line number: " + str(line_no) + "\\n"
|
|
||||||
c_exception += "\\tError line content: " + str(line_content) + "\\n"
|
|
||||||
|
|
||||||
if _value.args:
|
|
||||||
c_exception += "\\tError description: " + str(_value)
|
|
||||||
else:
|
|
||||||
c_exception += "\\tError description: " + _type.__name__
|
|
||||||
|
|
||||||
raise _type(c_exception)
|
|
||||||
""".format(script)
|
""".format(script)
|
||||||
|
|
||||||
variables = {
|
variables = {
|
||||||
"status_code": self.resp_obj.status_code,
|
"status_code": self.resp_obj.status_code,
|
||||||
"response_json": self.resp_obj.json,
|
"response_json": self.resp_obj.json,
|
||||||
"response": self.resp_obj
|
"response": self.resp_obj
|
||||||
}
|
}
|
||||||
variables.update(self.session_context.test_variables_mapping)
|
variables.update(self.session_context.test_variables_mapping)
|
||||||
|
variables.update(globals())
|
||||||
|
|
||||||
try:
|
try:
|
||||||
code = compile(code, '<string>', 'exec')
|
|
||||||
exec(code, variables)
|
exec(code, variables)
|
||||||
validator_dict["check_result"] = "pass"
|
except SyntaxError as ex:
|
||||||
return validator_dict, ""
|
logger.log_warning("SyntaxError in python validate script: {}".format(ex))
|
||||||
|
result["check_result"] = "fail"
|
||||||
|
result["output"] = "<br/>".join([
|
||||||
|
"ErrorMessage: {}".format(ex.msg),
|
||||||
|
"ErrorLine: {}".format(ex.lineno),
|
||||||
|
"ErrorText: {}".format(ex.text)
|
||||||
|
])
|
||||||
|
return result
|
||||||
|
|
||||||
|
try:
|
||||||
|
# run python validate script
|
||||||
|
variables["run_validate_script"]()
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
validator_dict["check_result"] = "fail"
|
logger.log_warning("run python validate script failed: {}".format(ex))
|
||||||
validator_dict["exception"] = "<br/>".join(str(ex).splitlines())
|
result["check_result"] = "fail"
|
||||||
return validator_dict, str(ex)
|
|
||||||
|
_type, _value, _tb = sys.exc_info()
|
||||||
|
|
||||||
|
_lineno = -1
|
||||||
|
if _tb.tb_next:
|
||||||
|
_lineno = _tb.tb_next.tb_lineno
|
||||||
|
line_no = _lineno - 4
|
||||||
|
elif len(traceback.extract_tb(_tb)) > 0:
|
||||||
|
# filename, lineno, name, line
|
||||||
|
_, _lineno, _, _ = traceback.extract_tb(_tb)[-1]
|
||||||
|
line_no = _lineno - 4
|
||||||
|
else:
|
||||||
|
line_no = "N/A"
|
||||||
|
|
||||||
|
result["output"] = "<br/>".join([
|
||||||
|
"ErrorType: {}".format(_type.__name__),
|
||||||
|
"ErrorLine: {}".format(line_no)
|
||||||
|
])
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
def validate(self, validators):
|
def validate(self, validators):
|
||||||
""" make validation with comparators
|
""" make validation with comparators
|
||||||
@@ -125,12 +140,12 @@ except Exception as ex:
|
|||||||
|
|
||||||
if isinstance(validator, dict) and validator.get("type") == "python_script":
|
if isinstance(validator, dict) and validator.get("type") == "python_script":
|
||||||
script = self.session_context.eval_content(validator["script"])
|
script = self.session_context.eval_content(validator["script"])
|
||||||
validator_dict, ex = self.validate_script(script)
|
result = self.validate_script(script)
|
||||||
if ex:
|
if result["check_result"] == "fail":
|
||||||
validate_pass = False
|
validate_pass = False
|
||||||
failures.append(ex)
|
failures.append(result["output"])
|
||||||
|
|
||||||
self.validation_results["validate_script"] = validator_dict
|
self.validation_results["validate_script"] = result
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if "validate_extractor" not in self.validation_results:
|
if "validate_extractor" not in self.validation_results:
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[tool.poetry]
|
[tool.poetry]
|
||||||
name = "httprunner"
|
name = "httprunner"
|
||||||
version = "2.5.6"
|
version = "2.5.7"
|
||||||
description = "One-stop solution for HTTP(S) testing."
|
description = "One-stop solution for HTTP(S) testing."
|
||||||
license = "Apache-2.0"
|
license = "Apache-2.0"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
|||||||
Reference in New Issue
Block a user