python - 创建提供类型错误 Python 的 UInt16 类函数的问题
问题描述
我想做这个测试程序
test_UInt16.py
from data_types import UInt16
def test_UInt16_constructor():
# default value should be 0
assert (int(UInt16()) == 0)
# initialize with arbitrary integer value
assert (int(UInt16(1 << 8)) == (1 << 8))
数据类型.py
class UInt16:
def __init__(self, initial_value=0):
self.value = initial_value
TypeError: int() 参数必须是字符串、类似字节的对象或数字,而不是“UInt16”
解决方案
您不能将UInt16
您定义的对象转换为int
对象。
你需要改变你的代码才能变成这样。
test_UInt16
from data_types import UInt16
def test_UInt16_constructor():
# default value should be 0
assert (UInt16().value == 0)
# initialize with arbitrary integer value
assert (UInt16(1 << 8).value == (1 << 8))
UInt16
class UInt16:
def __init__(self, initial_value=0):
self.value = initial_value
使用内置测试方法(unittest
类)也是一种更好的做法