标签:unit-testing python-3-x enums python class
这是一个令人不安的问题.对于功能:
def influencePositive(q1, q2):
if(q1.magnitude.greaterZero()):
q2.derivative.value = DValue.add(q2.derivative.value, 1)
以下单元测试运行没有问题:
def test_i_plus_active(self):
q1 = Quantity(Magnitude(MValue.PLUS), None)
q2 = Quantity(None, Derivative(DValue.ZERO))
r.influencePositive(q1, q2)
self.assertEqual(q2.derivative.value, DValue.PLUS)
但是对于其他功能:
def exogenous(q1, q2, value):
if type(value) == int:
q2.derivative.value = DValue.add(q1.derivative.value, value)
以下单元测试中断:
def test_ex_increase_minus(self):
q1 = Quantity(None, DValue.MINUS)
q2 = Quantity(None, DValue.MINUS)
r.exogenous(q1, q2, 1)
self.assertEqual(q2.derivative.value, DValue.ZERO)
它将引发Atribute错误:AttributeError:无法设置属性.那就是整个回溯:
Traceback (most recent call last):
File "C:/Users/Victor Zuanazzi/Documents/Artificial Intelligence/Knowledge Representation/Practicals/Qualitative_Reaoning/relationFunction_test.py", line 121, in test_ex_increase_minus
r.exogenous(q1, q2, 1)
File "C:\Users\Victor Zuanazzi\Documents\Artificial Intelligence\Knowledge Representation\Practicals\Qualitative_Reaoning\relationFunctions.py", line 31, in exogenous
q2.derivative.value = DValue.add(q1.derivative.value, value)
File "C:\ProgramData\Anaconda3\lib\types.py", line 146, in __set__
raise AttributeError("can't set attribute")
AttributeError: can't set attribute
这里是一些了解上面代码的背景知识.
DValue是一个IntEnum:
from enum import IntEnum
class DValue(IntEnum):
MINUS = -1
ZERO = 0
PLUS = 1
@staticmethod
def add(dvalue, delta):
return max(min(dvalue + delta, DValue.PLUS), DValue.MINUS)
我们使用它来设置派生类:
class Derivative:
def __init__(self, value):
#if value is type int, it is converted to Enum.
if value is int:
value = DValue(value)
self.value = value
数量是一类,具有为其实例设置的大小和派生值:
from magnitude import Magnitude, MValue
from derivative import Derivative, DValue
class Quantity:
def __init__(self, magnitude, derivative):
self.magnitude = magnitude
self.derivative = derivative
我不明白为什么ImpactPositive()可以正常工作,而exogenous()可以中断.它们都以相同的方式调用DValue.add().
解决方法:
这是您的问题:
在您的第一个测试中,派生对象是一个派生对象,其.value属性可以重新分配.在您的第二个测试中,派生对象是DValue(IntEnum)对象,其.value属性无法重新分配.
In [4]: d = Derivative(DValue.ZERO)
In [5]: d.value
Out[5]: <DValue.ZERO: 0>
In [6]: d.value = 1
In [7]: d.value
Out[7]: 1
In [8]: d = DValue.MINUS
In [9]: d.value
Out[9]: -1
In [10]: d.value = 1
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-10-3c0164b4d46d> in <module>()
----> 1 d.value = 1
/home/ethan/.local/lib/python2.7/site-packages/enum/__init__.pyc in __set__(self, instance, value)
54
55 def __set__(self, instance, value):
---> 56 raise AttributeError("can't set attribute")
57
58 def __delete__(self, instance):
AttributeError: can't set attribute
因此,我认为您的第二项测试应像这样设置q2:
q2 = Quantity(None, Derivative(DValue.MINUS))
标签:unit-testing,python-3-x,enums,python,class 来源: https://codeday.me/bug/20191210/2105255.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。