python (3.11.7)

(root)/
lib/
python3.11/
unittest/
test/
_test_warnings.py
       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      # unittest warnings will be printed at most once per type (max one message
      22      # for the fail* methods, and one for the assert* methods)
      23      def test_assert(self):
      24          self.assertEquals(2+2, 4)
      25          self.assertEquals(2*2, 4)
      26          self.assertEquals(2**2, 4)
      27  
      28      def test_fail(self):
      29          self.failUnless(1)
      30          self.failUnless(True)
      31  
      32      def test_other_unittest(self):
      33          self.assertAlmostEqual(2+2, 4)
      34          self.assertNotAlmostEqual(4+4, 2)
      35  
      36      # these warnings are normally silenced, but they are printed in unittest
      37      def test_deprecation(self):
      38          warnings.warn('dw', DeprecationWarning)
      39          warnings.warn('dw', DeprecationWarning)
      40          warnings.warn('dw', DeprecationWarning)
      41  
      42      def test_import(self):
      43          warnings.warn('iw', ImportWarning)
      44          warnings.warn('iw', ImportWarning)
      45          warnings.warn('iw', ImportWarning)
      46  
      47      # user warnings should always be printed
      48      def test_warning(self):
      49          warnings.warn('uw')
      50          warnings.warn('uw')
      51          warnings.warn('uw')
      52  
      53      # these warnings come from the same place; they will be printed
      54      # only once by default or three times if the 'always' filter is used
      55      def test_function(self):
      56  
      57          warnfun()
      58          warnfun()
      59          warnfun()
      60  
      61  
      62  
      63  if __name__ == '__main__':
      64      with warnings.catch_warnings(record=True) as ws:
      65          # if an arg is provided pass it to unittest.main as 'warnings'
      66          if len(sys.argv) == 2:
      67              unittest.main(exit=False, warnings=sys.argv.pop())
      68          else:
      69              unittest.main(exit=False)
      70  
      71      # print all the warning messages collected
      72      for w in ws:
      73          print(w.message)