I created a simple demo to test my Django project.
when i invoke unittest, everything passed and i set logger.info to record log into debug.log.
There should be some records appeared in my target file but nothing in it.

here is my Logger.py
import logging.config
import os
fmt = "%(asctime)s|%(levelname)s|%(filename)s:%(lineno)d|%(message)s"
datefmt = "%Y-%m-%d %H:%M:%S"
# dir
base_path = os.path.dirname(os.path.dirname(__file__))
log_path = os.path.join(base_path, 'logs')
if not os.path.exists(log_path):
os.mkdir(log_path)
log_file = os.path.join(log_path, "debug.log")
file_handler = logging.handlers.RotatingFileHandler(
# 'debug.log',
log_file,
backupCount=10,
encoding='utf-8'
)
logging.basicConfig(
format=fmt,
datefmt=datefmt,
handlers=[file_handler],
level=logging.INFO
)
# logging.DEBUG
# logging.INFO
# logging.WARNING
# logging.ERROR
# logging.CRITICAL
logger = logging.getLogger()
also here is the main endpoint
import json
import unittest
from ddt import ddt, file_data
import api.KeywordApi as kwa
from commons.
Logger import logger
@ddt
class loginInterfaceTestCase(unittest.TestCase):
@file_data('../testDatas/testdata_interface_login.yaml')
def test_login(self, **params):
print("{:*^50s}".format("the first correct answer"))
path = "/dologin/"
data = {
"username": params['username'],
"pwd": params['password'],
"randomCode": "1234"
}
# 2:send request
res = kwa.do_post(path, data)
# 3:assert
self.assertEqual(200, res.status_code, "fail:{}".format(res.status_code))
print('text', res.text)
# print(type(res.text))
logger.info(type(res.text))
# logger.info(res.text)
print(json.dumps(res.json(), indent=2, ensure_ascii=False))
self.assertEqual(params['code'], res.json()['code'], 'failed')
if __name__ == '__main__':
unittest.main()
Any ideas? A
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
if type(datas[key]) == 'str' and datas[key].startswith('<') and datas[key].endswith('>'):
In the decoration use eval function to deal with datetime format
After i changed if to this it works:
if type(datas[key]) == str and datas[key].startswith('<') and datas[key].endswith('>'):
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0