1  # helper module for test_runner.Test_TextTestRunner.test_warnings
       2  
       3  """
       4  This module has a number of tests that raise different kinds of warnings.
       5  When the tests are run, the warnings are caught and their messages are printed
       6  to stdout.  This module also accepts an arg that is then passed to
       7  unittest.main to affect the behavior of warnings.
       8  Test_TextTestRunner.test_warnings executes this script with different
       9  combinations of warnings args and -W flags and check that the output is correct.
      10  See #10535.
      11  """
      12  
      13  import sys
      14  import unittest
      15  import warnings
      16  
      17  def warnfun():
      18      warnings.warn('rw', RuntimeWarning)
      19  
      20  class ESC[4;38;5;81mTestWarnings(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
      21      def test_other_unittest(self):
      22          self.assertAlmostEqual(2+2, 4)
      23          self.assertNotAlmostEqual(4+4, 2)
      24  
      25      # these warnings are normally silenced, but they are printed in unittest
      26      def test_deprecation(self):
      27          warnings.warn('dw', DeprecationWarning)
      28          warnings.warn('dw', DeprecationWarning)
      29          warnings.warn('dw', DeprecationWarning)
      30  
      31      def test_import(self):
      32          warnings.warn('iw', ImportWarning)
      33          warnings.warn('iw', ImportWarning)
      34          warnings.warn('iw', ImportWarning)
      35  
      36      # user warnings should always be printed
      37      def test_warning(self):
      38          warnings.warn('uw')
      39          warnings.warn('uw')
      40          warnings.warn('uw')
      41  
      42      # these warnings come from the same place; they will be printed
      43      # only once by default or three times if the 'always' filter is used
      44      def test_function(self):
      45  
      46          warnfun()
      47          warnfun()
      48          warnfun()
      49  
      50  
      51  
      52  if __name__ == '__main__':
      53      with warnings.catch_warnings(record=True) as ws:
      54          # if an arg is provided pass it to unittest.main as 'warnings'
      55          if len(sys.argv) == 2:
      56              unittest.main(exit=False, warnings=sys.argv.pop())
      57          else:
      58              unittest.main(exit=False)
      59  
      60      # print all the warning messages collected
      61      for w in ws:
      62          print(w.message)