class Sequence(object): def __init__(self): self.first = None self.len = 0 def add(self,new): self.len += 1 if self.first is None: self.first = element(new) else: self.first = element(new, self.first) def find(self, index): i = self.first num = 0 while num is not index: i = i.nex num += 1 print i.value
Класс самого элемента
class element(object): def __init__(self, value = None, nex = None): self.value = value self.nex = nex
В unit тесте
from list import Sequence as s import unittest class testsequence (unittest.TestCase): def setUp(self): self.lst = s() def testadd(self): self.lst.add(1) self.lst.add(2) self.lst.add(3) self.lst.add(4) self.lst.printall() def testfind(self): index = input ("Введите индекс") self.lst.find(index) if __name__ == "__main__": unittest.main()
Выводит следующее:
.4
3
2
1
Введите индекс1
E
======================================================================
ERROR: testfind (__main__.testsequence)
———————————————————————-
Traceback (most recent call last):
File “C:\eclipse\workspace\test\test.py”, line 21, in testfind
self.lst.find(index)
File “C:\eclipse\workspace\test\list.py”, line 38, in find
i = i.nex
AttributeError: ‘NoneType’ object has no attribute ‘nex’
———————————————————————-
Ran 2 tests in 2.251s
FAILED (errors=1)
Почему первый тест проходит, а второй нет?