loguru的使用

from loguru import logger
from unittest import TestCase

# 将日志输出到demo1.log文件中 logger.add(sink='demo1.log', encoding='utf-8', level='ERROR', rotation='10 B' ) # logger .add ( "file_1.log" , rotation="500 MB") 每500MB生成个新文件 # logger .add ( "file_2.log " , rotation="12:00")每天12点生成新的文件 # logger .add ( "file_3.log " , rotation="1 week ")一周生成一个新文件 # logger.add ( "file_x.log" , retention="10 days")10天生成一个新文件 def login(username=None, password=None): if username is None or password is None: return {'为空'} elif username == 'aa' and password == '123': return {'通过'} return {'错误'} # 类遵守规则:必须继承unittest.TestCase class TestLogin(TestCase): def test_login_1(self): """测试用例方法""" username = 'aa' password = '123' ex = {'通过'} logger.info("正在执行测试用例1...") ac = login(username, password) # assert ex != ac self.assertEqual(ex, ac) logger.info("执行测试用例1完毕...") def test_login_2(self): """测试用例方法""" username = 'test' password = '123' ex = {'通过'} logger.info("正在执行测试用例2...") ac = login(username, password) try: assert ex == ac except AssertionError as e: # 在日志中记录断言异常 logger.error("测试用例2断言失败") raise e # 抛出异常,不然被捕获了就pass了 logger.info("执行测试用例2完毕...")

 

上一篇:sublime 插件推荐: Nettuts+ Fetch


下一篇:弃繁就简,一行代码教你摆平Python 日志