这是我想要测试的虚拟课程:
formatter: function() {
var points = this.points,
each = Highcharts.each,
txt = '<span style="font-size: 10px">' + points[0].key + '</span><br/>',
H = Highcharts,
value,
numericSymbolDetector;
each(points, function(point, i) {
value = point.y;
numericSymbolDetector = Math.abs(point.y);
if (numericSymbolDetector > 1000) {
value = H.numberFormat(value / 1000, 2, '.','' ) + 'k';
}
txt += '<span style="color:' + point.series.color + '">' + point.series.name + ': <b>' + value + '</b><br/>';
});
return txt;
}
这是我的单位测试:
class Meter:
def __init__(self, meterInfo):
self.b1pos = meterInfo['b1pos']
def setMeter(self):
print 'Initializing'
try:
print 'Connecting'
except IOError as message:
print message
def readSignal(self, ele_name):
try:
if ele_name in ("b1", "b2"):
readValue = 0.0
readValue1 = 0.0
return readValue, readValue1
else:
readValue = 0.0
readValue1 = 0.0
return readValue, readValue1
except IOError as message:
print message
对于testSetMeter()我没有任何问题,但对于testReadSignal()它给了我:&#34; TypeError:&#39; tuple&#39;对象不可调用&#34; 我知道这个错误与radSignal的返回有关,但我该如何解决呢? 我尝试过:
class TestMeter(unittest.TestCase):
def testSetMeter(self):
m = meter.Meter({'b1pos': 'b1pos'})
self.assertRaises(IOError, m.setMeter())
def testReadSignal(self):
m = meter.Meter({'b1pos': 'b1pos'})
a, b = m.readSignal('b1')
self.assertTrue(isinstance(a, numbers.Number))
self.assertTrue(isinstance(b, numbers.Number))
self.assertRaises(IOError, m.readSignal('b1'))
if __name__ == '__main__':
unittest.main()
但这是输出:
def testReadSignal(self):
m = meter.Meter({'b1pos': 'b1pos'})
a, b = m.readSignal('b1')
self.assertTrue(isinstance(a, numbers.Number))
self.assertTrue(isinstance(b, numbers.Number))
with self.assertRaises(IOError):
m.readSignal('b1')
答案 0 :(得分:0)
必须使用pytest.raises。请参阅docs。
with self.raises(IOError):
m.readSignal('b1')
似乎测试会失败,因为方法正在捕获异常,而不是提升它们。
答案 1 :(得分:0)
带
self.assertRaises(Exception, m.readSignal('b1'))
我解决了问题