[英]TypeError : strptime() argument 1 must be str, not None
I'm new in Python.我是 Python 新手。 I have a
util.py
file to check the value of datetime.我有一个
util.py
文件来检查日期时间的值。 I writed a test case for it with value is None.我为它写了一个测试用例,值为 None。 But I got error
strptime() argument 1 must be str, not None
.但是我得到了错误
strptime() argument 1 must be str, not None
。 Would someone please tell me how to fix it?有人会告诉我如何解决它吗? Thank you.
谢谢你。
My util.py
:我的
util.py
:
from datetime import date, datetime
import traceback
import time
from analyzer.analyzerlogging import setup_stopwatch, setup_logger
def validate_date(logger, val, parameter_name, date_format, date_format_info):
""" Check if val is the date with correct format, then return the date
"""
_val = None
try:
_val = datetime.strptime(val, date_format)
except ValueError:
msg = f"Invalid value {val} of parameter {parameter_name}. {parameter_name.capitalize()} should be at format '{date_format_info}'"
logger.error(msg)
raise Exception(msg)
return _val
My test case :我的测试用例:
import unittest
from analyzer.util import validate_date
from datetime import date
import datetime
from analyzer.analyzerlogging import setup_logger
class TestUtilValidateDate(unittest.TestCase):
#val is None and date_format_info is YYYY/MM/DD HH:mm:ss
def test_UT_UTIL_VALIDATE_DATE_005(self):
try:
validate_date(self.logger, None, 'param_name', '%Y/%m/%d %H:%M:%S', 'YYYY/MM/DD HH:mm:ss')
except Exception as e:
result = "Invalid value None of parameter param_name. Param_name should be at format 'YYYY/MM/DD HH:mm:ss'"
self.assertEqual(str(e),result)
Error :错误 :
test_UT_UTIL_VALIDATE_DATE_005 (test_util_validate_date.TestUtilValidateDate) ... FAIL
NoneType: None
======================================================================
FAIL: test_UT_UTIL_VALIDATE_DATE_005 (test_util_validate_date.TestUtilValidateDate)
----------------------------------------------------------------------
Traceback (most recent call last):
File "c:\Users\duongnb\Documents\SVN\trunk\src\Analyzer\toshiba\yieldnews\ut\test_util_validate_date.py", line 44, in test_UT_UTIL_VALIDATE_DATE_005
validate_date(self.logger, None, 'param_name', '%Y/%m/%d %H:%M:%S', 'YYYY/MM/DD HH:mm:ss')
TypeError: strptime() argument 1 must be str, not None
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\Users\duongnb\Documents\SVN\trunk\src\Analyzer\toshiba\yieldnews\ut\test_util_validate_date.py", line 47, in test_UT_UTIL_VALIDATE_DATE_005
self.assertEqual(str(e),result)
AssertionError: 'strptime() argument 1 must be str, not None' != "Invalid value None of parameter param_na[52 chars]:ss'"
- strptime() argument 1 must be str, not None
+ Invalid value None of parameter param_name. Param_name should be at format 'YYYY/MM/DD HH:mm:ss'
----------------------------------------------------------------------
Ran 1 test in 0.002s
FAILED (failures=1)
The unittest has worked correctly in your code - it indicates your try/except is not capturing the correct exception.单元测试在您的代码中正常工作 - 它表明您的 try/except 没有捕获正确的异常。 Change your exception to
TypeError
and it will be caught and the message returned.将您的异常更改为
TypeError
,它将被捕获并返回消息。
You should use self.assertRaisesMesssage
if your aim is to check the expected error and error message that passing in None
as arg1 produces.如果您的目标是检查预期的错误和错误消息,则应使用
self.assertRaisesMesssage
作为 arg1 传入None
。
mock_val = None
parameter_name = 'Foo'
date_format_info = 'YYYY/MM/DD HH:mm:ss'
expected_msg = f"Invalid value {mock_val} of parameter {parameter_name}. {parameter_name.capitalize()} should be at format '{date_format_info}'"
with self.assertRaisesMessage(TypeError, expected_msg):
validate_date(self.logger, mock_val, parameter_name, '%Y/%m/%d %H:%M:%S', date_format_info)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.