在线客服
扫描二维码
下载博学谷APP
扫描二维码
关注博学谷微信公众号
今天我们来梳理一下自动化测试中的Python logging源码学习笔记,主要内容分为三个部分,分别是基本使用、文件存储与日志打印和API。感兴趣的小伙伴赶紧一起来看看吧~

1、基本使用
import logging
# logging 日志配置
logging.basicConfig(filename='exampe.log',level=logging.DEBUG)
logging.debug("helloworld - debug")
logging.info('hello info')
logging.warning('hello warning')
logging.error('hello error')logging.critical('hello critical')
2、文件存储与日志打印
import logging
# create logger
logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
fl = logging.FileHandler('app.log')
fl.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# add formatter to ch
ch.setFormatter(formatter)
fl.setFormatter(formatter)
# add ch to logger
logger.addHandler(ch)
logger.addHandler(fl)
# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warning('warn message')
logger.error('error message')
logger.critical('critical message')
3、API
import requests
import logging
logging.basicConfig(format='%(levelname)s %(asctime)s %(message)s',level=logging.DEBUG)
base_url = "http://39.107.96.138:3000/api/v1/"
testdata = {
"accesstoken":"49b2e830-4305-475d-b6b5-52287
cc5daaa",
"title":"2313131231231232",
"tab":"ask",
"content":"xxxxxxxxxxxxx"
}
def test_new_topic():
"""
测试发布话题
:return:
"""
url = base_url+'topics'
logging.info(f"开始发送Post请求{url},请求数据为{str(testdata)}")
r = requests.post(url,json=testdata)
jsonData = r.json()
logging.info(f'发送请求完成,结果为{str(jsonData)}')
assert r.status_code == 200
assert jsonData['success']
logging.info(f"test_new_topic, topicid: {jsonData['topic_
id']}")
assert jsonData['topic_id'] is not None
return jsonData['topic_id']
以上就是Python logging源码学习笔记的全部内容,如果想学习更多关于自动化测试的知识点,可以在博学谷官网报名申请相关免费课程的试听~
— 申请免费试学名额 —
在职想转行提升,担心学不会?根据个人情况规划学习路线,闯关式自适应学习模式保证学习效果
讲师一对一辅导,在线答疑解惑,指导就业!
相关推荐 更多
高薪的软件测试工程师要掌握哪些专业技能?
软件测试开发需要掌握软件测试基础知识、测试工具的使用、操作系统相关知识、数据库知识、计算机硬件知识、代码编写等技能。同时软件测试还需要较强的沟通能力。
12852
2019-05-23 15:56:31
软件测试工作中有哪些常用的工具?
软件测试工作中有哪些常用的工具?一般来说软件测试工具有开源测试管理工具、开源功能自动化测试工具、开源性能自动化测试工具、Quality Center、QuickTest Professional、LoadRunner等。
10455
2019-05-15 17:43:21
黑盒测试是什么?黑盒测试的优缺点分析
在软件测试中,黑盒测试被频繁地提起,那么到底黑盒测试是什么?本文就黑盒测试的优缺点来详细分析黑盒测试。
13890
2019-07-30 20:00:45
软件测试能做到多少岁?
很多人都说做互联网技术岗位,都是吃青春饭,大部分人到了35岁之后如果还没有晋升到管理岗位基本就达到了职业的天花板了。那如果从事软件测试行业是不是同样呢?
9951
2019-10-16 18:48:54
Newman的使用讲解
Newman是postman命令行集成工具,使用Newman可以在命令行中运行postman的脚本,方便与Jenkins等CI&CD工具集成使用,今天我们来看看Newman的使用讲解,主要内容包括Newman的安装、基本使用、常用运行参数、生成报告等等。
6879
2020-06-23 11:01:11
