Reputation: 911
I made the following test case using unittest
:
if __name__ == "__main__":
class TestList(unittest.TestCase):
def setUp(self):
self.li = List(["ABC", 5.6, (3, 6)])
def test_append(self):
self.li.append(1)
self.assertEqual(self.li, List(["ABC", 5.6, (3, 6), 1]))
def test_insert(self):
self.li.insert(1, "ABC")
This is my List
class (some methods ommited for brevity):
class List(MutableSequence):
def __init__(self, seq=None):
self.seq = {} if seq is None else self._dict_from_seq(seq)
def __getitem__(self, item):
try:
return self.seq[item]
except KeyError:
raise IndexError("list index out of range") from None
def __setitem__(self, key, value, *, usage=None):
if key > max(self.seq.keys()) and usage != "append":
raise IndexError("list index out of range")
self.seq[key] = value
def __delitem__(self, key):
try:
del self.seq[key]
except KeyError:
raise IndexError("list index out of range") from None
@classmethod
def _dict_from_seq(cls, seq):
return OrderedDict(enumerate(seq))
def _next_available_slot(self):
return max(self.seq) + 1
def append(self, item):
self.__setitem__(self._next_available_slot(), item, usage="append")
def insert(self, index, value):
if index > max(self.seq.keys()):
raise IndexError("list index out of range")
self[index] = value
And when I ran unittest.main()
, I got the following error:
File "C:\...\AppData\Local\Programs\Python\Python38\lib\unittest\loader.py", line 34, in testFailure
raise self._exception
File "C:\...\AppData\Local\Programs\Python\Python38\lib\unittest\loader.py", line 169, in loadTestsFromName
parent, obj = obj, getattr(obj, part)
AttributeError: module 'fastseq' has no attribute 'TestList'
Why is this happening?
Upvotes: 1
Views: 102
Reputation: 911
I solved the problem. Apparently, loader.py
could not get the TestList class because __name__
was not __main__
. So I just moved the if __name__ == "__main__":
line to the spot where I call unittest.main()
.
Upvotes: 1