0

How to disable console print statement while running in Python Unittest framework.

I have a python function and filename is firstfile.py

def testfn():
    a = 5, b=5
    c = a + b
    print 'Addition result:', c
    return c

I am calling this function in my unittest python script that,

from firstfile import testfn
....
....
def func1():
    # calling this testfn() here in my unit test
    res = testfn()
    self.assertEqual(res, '10')

In console, I do not want to print this "print 'Addition result:', c" while running unittest. How to achieve this. Please help it. Thanks in advance

Jack
  • 21
  • 1
  • 6
  • 2
    Why are you printing in the first place? Remove that print statement, it has no place there. The responsibility of the function is to calculate; move the print statement to the code that uses the function. – Martijn Pieters Aug 24 '17 at 10:19
  • [mcve] would be nice as well. We cannot reproduce. I guess you could silence `print` by redirecting `sys.stdout`. plus the posted code has a typo when defining a & b – Jean-François Fabre Aug 24 '17 at 10:19
  • put `if __name__=="__main__":` over the print statement and check. – akp Aug 24 '17 at 10:22
  • If i run my firstfile, the o/p is: Addition result: 10 In my unittest file execution. I do not want to print that. Unittest framwork will display only the test execution time. – Jack Aug 24 '17 at 10:26
  • Thanks to all. I got the answer from https://stackoverflow.com/questions/24134343/is-there-a-way-to-suppress-printing-that-is-done-within-a-unit-test – Jack Aug 24 '17 at 13:45

0 Answers0