1. 日志中中文无法正常显示,显示unicode编码
可以在httprunner/client.py 文件中添加 ensure_ascii=False修改以下代码如下:
def log_print(req_or_resp, r_type):
msg = f"\n================== {r_type} details ==================\n"
for key, value in req_or_resp.dict().items():
if isinstance(value, dict):
value = json.dumps(value, indent=4, ensure_ascii=False)
2. 无法生成报告
需要给定参数
生成html的报告
hrun testcases\login_test.py --html=reports\report.html, 如果要生成自包含的html,可以使用参数 --self-contained-html
如果要生成allure的报告,需要安装 pip install "allure-pytest", 另外,需要安装allure, 参考 https://www.cnblogs.com/ella-li/p/11770129.html
运行:hrun testcases\login_test.py --alluredir=reports --clean-alluredir --allure-no-capture
--alluredir=DIR
: Generate Allure report in the specified directory (may not exist)--clean-alluredir
: Clean alluredir folder if it exists--allure-no-capture
: Do not attach pytest captured logging/stdout/stderr to report
运行:allure serve reports 这个仅仅是看
运行:allure.bat generate --output=allure-report reports 可以将报告文件保存到allure-report
但是这样保存后,需要有个httpserver,然后才能从浏览器看
npm install -g http-server
在allure-report文件夹下, 运行 http-server -p 端口号, 然后在浏览器中输入 http:\\localhost:端口号, 就可以看到报告了
3. 在Config中,export 变量时,如果这个变量没有在step中使用
with_jmespath去赋值的话,在引用该case的其他用例中是无法export这个password的
config = (
Config("Login with giving name and password")
.base_url('http://testapi.smindu.com')
.variables(**{"username": "root", "password": "123456"})
.export(*["token", "password"]) #此处export password是没用的
.verify(False)
)
teststeps = [
Step(
RunRequest("login with parameters")
.post("/hospital/api/v1/admin/session")
.with_headers(**{"Content-Type": "application/json"})
.with_json({"username": "$username", "password": "$password"})
.extract()
.with_jmespath("cookies.JSESSIONID", "token")
.validate()
.assert_equal("body.code", 0)
.assert_equal("body.message", "成功")
)
]
4. httprunner生成的测试报告中,仅有错误信息(与用例没啥关系的信息),没有正常的日志信息,应该与loguru这个日志库有关系
在runner.py中的test_start函数中,添加6行代码 【这种修改httpruner库代码的方式不是很好,具体的原因我也不知道】
def test_start(self, param: Dict = None) -> "HttpRunner":
"""main entrance, discovered by pytest"""
self.__init_tests__()
self.__project_meta = self.__project_meta or load_project_meta(
self.__config.path
)
self.__case_id = self.__case_id or str(uuid.uuid4())
self.__log_path = self.__log_path or os.path.join(
self.__project_meta.RootDir, "logs", f"{self.__case_id}.run.log"
)
import logging #添加的
class PropogateHandler(logging.Handler): #添加的
def emit(self, record): #添加的
logging.getLogger(record.name).handle(record) #添加的
log_handler = logger.add( self.__log_path, level="DEBUG")
log_handler2 = logger.add(PropogateHandler(), level="DEBUG") #添加的
# parse config name
config_variables = self.__config.variables
if param:
config_variables.update(param)
config_variables.update(self.__session_variables)
self.__config.name = parse_data(
self.__config.name, config_variables, self.__project_meta.functions
)
if USE_ALLURE:
# update allure report meta
allure.dynamic.title(self.__config.name)
allure.dynamic.description(f"TestCase ID: {self.__case_id}")
logger.info(
f"Start to run testcase: {self.__config.name}, TestCase ID: {self.__case_id}"
)
try:
return self.run_testcase(
TestCase(config=self.__config, teststeps=self.__teststeps)
)
finally:
logger.remove(log_handler)
logger.remove(log_handler2) #添加的
logger.info(f"generate testcase log: {self.__log_path}")
5. 上面的问题解决后,我们会发现测试报告中,我们打开日志时会出现下面红框中的数据
这个应该是给日志进行着色的,但是allure中貌似没有进行着色处理,也许是解决第四问题时引入的,暂时还没有解决方法
6. config中,仅能调用debugtalk中的函数,不能使用debugtalk中定义的变量。
7. 直到3.x, 依然不支持在debugtalk中定义generator