2

I'm running some unit tests in Python3 but the print() doesn't seem to work.

Here are snippets of my code:

import unittest

def lexer(line):
        ... #Lots of code and print statements here
    return tokens

class lexerBasicTestCases(unittest.TestCase):
    def Test1(self):
        test = "9+4"
        output = ["9", "+", "4"]
        self.assertEqual(lexer(test), output)   
    def Test2(self):
        test = "9 + 4"
        output = ["9", "+", "4"]
        self.assertEqual(lexer(test), output)   
    def Test2(self):
        test = "9    +  4"
        output = ["9", "+", "4"]
        self.assertEqual(lexer(test), output)   

def lexerBasicTestSuite():
    suite = unittest.TestSuite()
    suite.addTest(lexerBasicTestCases('Test1'))
    suite.addTest(lexerBasicTestCases('Test2'))
    suite.addTest(lexerBasicTestCases('Test3'))
    unittest.TextTestRunner(verbosity=2).run(suite)

#Main program
lexerBasicTestSuite()

I run my code using:

python \interpreter.py

When the unit tests run, the print() statements inside lexer() are ignored. This is frustrating when the test cases fail and I don't have print statements to analyze. When I'm debugging I'd rather not comment out the unit tests and write a counterpart function that solely runs the tests for their print statements. How can I make the print statements print out?

Jaitnium
  • 621
  • 1
  • 13
  • 27
  • 1
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. Instead, you *removed* the problem code and *retained* superfluous code. – Prune May 19 '17 at 17:03
  • 1
    You don't want to have functions printing stuff. What you want is to have the unit tests check the return values from functions. You can easily have other functions which print the return values during the run of your production code. Printing whilst running unit tests is a no-no. – quamrana May 19 '17 at 17:24
  • 1
    You can use logging module for this. May be this could help - https://stackoverflow.com/questions/284043/outputting-data-from-unit-test-in-python – Shashank May 24 '17 at 11:34

1 Answers1

3

Unit tests run in parallel so you can't rely on the print function because it is not thread safe. Use the logger instead, or even better pass a custom message to the assert method.

class lexerBasicTestCases(unittest.TestCase):
    def Test1(self):
        """
        Docstrings are printed in most test runners.
        """
        test = "9+4"
        expect = ["9", "+", "4"]
        failure_msg = 'lexer({0}) should return {1} but returned {2}'
        actual = lexer(test)
        self.assertListEqual(expect, actual, failure_msg.format(test, expect, actual)) 
Dan
  • 1,874
  • 1
  • 16
  • 21