0

I am new to unit testing, and I am trying to test a function that adds two numbers. THe program works fine if I want to test for the right result, but when I want to make the function fail the test, I get an AssertionError although I'm using try/except to catch that exception. I don't know what I'm doing wrong. Would someone please point that out?

import unittest
import sumXY
from random import randint

class Test(unittest.TestCase):
        def test_add(self):
                for a in range(1,5):
                        x = 2
                        y = 3
                        z = 6
                        try:
                                self.assertEqual(sumXY.sum(x,y), z)
                                print "%d + %d = %d" % (x, y, z) + " -> PASSED"
                        except:
                                print "%d + %d = %d" % (x, y, z) + " -> FAILED"
                                pass

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

Output:

AssertionError: 5 != 6

I would like the output to be: 2 + 3 = 6 -> FAILED

5
  • From what it seems, except is nested inside the try block. Move the except to be on the same level as try. Commented Apr 18, 2014 at 14:39
  • Actually, everything is properly indented on my script. I couldn't outdent on this site Commented Apr 18, 2014 at 14:40
  • Please fix the indentation then. Just paste the code, select it and press ctrl/command + k to mark it as a code block. See stackoverflow.com/editing-help for more info. Commented Apr 18, 2014 at 14:50
  • 1
    You should not catch AssertionErrors! They exist so that the unittest module knows whether the program failed or not. Also you didn't post the output you got. Commented Apr 18, 2014 at 15:05
  • I removed AssertionError so the except is by itself. The output I get is: AssertionError: 5 != 6. The number that the randint generates are x = 2, y = 3, but if you change z to be something else so the test fails, the AssertionError will occur Commented Apr 18, 2014 at 15:12

1 Answer 1

1

Why are you putting your own try...except block in the unit test? The whole point of using the unittest module is that this kind of machinery is provided for you. Just do something like this:

class Test(unittest.TestCase):
    def test_add(self):
        for a in range(1,5):
            x = 2
            y = 3
            z = 6
            self.assertEqual(sumXY.sum(x, y), z)

If z is not equal to sum(x, y) then the unittest module will display an error. This is the way the framework is intended to be used.

Sign up to request clarification or add additional context in comments.

2 Comments

But how do I display my 'prints' whether the test fails or passes? As you see in my code, I have two print statements. If the test passes when z = 5 it displays: 2 + 3 = 5 -> PASSED. However, if you change z = 6, it should display 2 + 3 = 6 -> FAILED, but it doesn't
You can make some adjustments to the unittest output, as mentioned in the documentation. If you really want something totally different from what unittest provides, though, then you should probably not use that module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.