# Unittest Fail: AssertionError

Page 1 of 1

## 2 Replies - 379 Views - Last Post: 23 August 2014 - 10:04 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=352467&amp;s=b95693607d0d936b30b5e4af356950d6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 MonkeyNipples

Reputation: 0
• Posts: 2
• Joined: 22-August 14

# Unittest Fail: AssertionError

Posted 22 August 2014 - 12:27 PM

Don't understand why my unittest fails. Purpose of the code is to input a list of integers and have a dictionary returned that tells me if each number is even or odd.

Original Code
```def IsEvenOrOddList(nlist):
''' (numberlist[]->dict{})

>>> IsEvenOrOddList([1,2,3])
{1: 'Odd', 2: 'Even', 3: 'Odd'}
>>> IsEvenOrOddList([])
String length 0
'''

numdic={}
'if length of list is 0, raise exception'
if len(nlist) == 0:
print "String length 0"
else:
'test if each item is type int'
for item in nlist:
if type(item) is int:

if item % 2 == 0:
numdic[item] = 'Even'
else:
numdic[item] = 'Odd'
else:
raise TypeError

print numdic

```

Unittest code
```import unittest
import IsEvenOrOddList

class TestList(unittest.TestCase):

def test_list1(self):
actual = IsEvenOrOddList.IsEvenOrOddList([1,2,3])
expected = {1: 'Odd', 2: 'Even', 3: 'Odd'}
self.assertEqual(actual,expected)

if __name__ == '__main__':

try: unittest.main()
except SystemExit: pass

```

Error
{1: 'Odd', 2: 'Even', 3: 'Odd'}
F
======================================================================
FAIL: test_list1 (__main__.TestList)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Documents and Settings\Chris\Desktop\evenListTest_unittest.py", line 9, in test_list1
self.assertEqual(actual,expected)
Assertionerror: None != {1: 'Odd', 2: 'Even', 3: 'Odd'}

----------------------------------------------------------------------
Ran 1 test in 0.016s

FAILED (failures=1)

The doctest passes no problem. It appears that actual = None?

Is This A Good Question/Topic? 0

## Replies To: Unittest Fail: AssertionError

### #2 andrewsw

• But the opposite, you said.

Reputation: 5542
• Posts: 21,925
• Joined: 12-December 12

## Re: Unittest Fail: AssertionError

Posted 22 August 2014 - 12:42 PM

Your function IsEvenOrOddList doesn't return anything so, yes, actual will be None.
By the name of the function I assume you want to return a boolean True/False value.

(unless printing in a unit-test does something special?)

This post has been edited by andrewsw: 22 August 2014 - 12:43 PM

### #3 MonkeyNipples

Reputation: 0
• Posts: 2
• Joined: 22-August 14

## Re: Unittest Fail: AssertionError

Posted 23 August 2014 - 10:04 AM

Ah ok. I made the fix, works ok. The function does this
IsEvenOrOddList([1,2,3]) -> {1: 'Odd', 2: 'Even', 3: 'Odd'}

I'm learning doctest and unittest so it's just an example.

Thanks