2 Replies - 171 Views - Last Post: 23 August 2014 - 10:04 AM Rate Topic: -----

#1 MonkeyNipples  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3746
  • View blog
  • Posts: 13,116
  • 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

Was This Post Helpful? 1
  • +
  • -

#3 MonkeyNipples  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1