Rajat Saxena
Rajat Saxena

Reputation: 3925

Python:What's wrong in my unit testing code?

I have two files "testable.py":

def joiner(x,y):
 return x+y 

"test_testable.py":

import unittest
import testable

class TestTestable(unittest.TestCase):
 def setUp(self):
  self.seq = ['a','b','1']
  self.seq2 = ['b','c',1]

 def test_joiner(self):
  for each in self.seq:
   for eachy in self.seq2:
    self.assertRaises(TypeError,testable.joiner(each,eachy))

if __name__ == '__main__':
 unittest.main()

Now when I run the test I get:

ERROR: test_joiner (test_testable.TestTestable)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/rajat/collective_knowledge/test_testable.py", line 16, in test_joiner
    self.assertRaises(TypeError,testable.joiner(each,eachy),(each,eachy))
  File "/home/rajat/collective_knowledge/testable.py", line 11, in joiner
    return x+y
TypeError: cannot concatenate 'str' and 'int' objects

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

What am I doing wrong?

Upvotes: 0

Views: 89

Answers (2)

mouad
mouad

Reputation: 70021

You're miss using assertRaises it should be:

self.assertRaises(TypeError,testable.joiner, (each,eachy))

Or just use it as a context manager if you're using python2.7 and above or unittest2:

with self.assertRaises(TypeError):
     testable.joiner(each,eachy)

EDIT :

You should also replace self.seq2 = [1,2,3] for example.

Upvotes: 4

Ramchandra Apte
Ramchandra Apte

Reputation: 4079

In

for each in self.seq:
    for eachy in self.seq2

each could be 'a' and eachy could be 1

You can't add 'a' and 1 so the test fails.

Upvotes: 0

Related Questions