1- from datetime import datetime , timedelta
21import re
3- from unittest import TestCase
42import sys
3+ from datetime import datetime , timedelta
4+ from unittest import TestCase
5+ from warnings import warn , catch_warnings
56
67from asserts import (
78 fail ,
3435 assert_raises_regex ,
3536 assert_raises_errno ,
3637 assert_succeeds ,
37- )
38+ assert_warns ,
39+ assert_warns_regex )
3840
3941
4042class _DummyObject (object ):
@@ -562,6 +564,8 @@ def test_assert_raises_errno__wrong_errno__custom_message(self):
562564 with assert_raises_errno (OSError , 20 , msg = "test message" ):
563565 raise OSError (1 , "Test error" )
564566
567+ # assert_succeeds()
568+
565569 def test_assert_succeeds__no_exception_raised (self ):
566570 with assert_succeeds (KeyError ):
567571 pass
@@ -584,3 +588,132 @@ def test_assert_succeeds__unexpected_exception(self):
584588 pass
585589 else :
586590 raise AssertionError ("KeyError was not raised" )
591+
592+ # assert_warns()
593+
594+ def test_assert_warns__warned (self ):
595+ with assert_succeeds (AssertionError ):
596+ with assert_warns (FutureWarning ):
597+ warn ("foo" , FutureWarning )
598+
599+ def test_assert_warns__not_warned (self ):
600+ with assert_raises (AssertionError ):
601+ with assert_warns (ImportWarning ):
602+ pass
603+
604+ def test_assert_warns__wrong_type (self ):
605+ with assert_raises (AssertionError ):
606+ with assert_warns (ImportWarning ):
607+ warn ("foo" , UnicodeWarning )
608+
609+ def test_assert_warns__multiple_warnings (self ):
610+ with assert_succeeds (AssertionError ):
611+ with assert_warns (UserWarning ):
612+ warn ("foo" , UnicodeWarning )
613+ warn ("bar" , UserWarning )
614+ warn ("baz" , FutureWarning )
615+
616+ def test_assert_warns__warning_handler_deinstalled_on_success (self ):
617+ with catch_warnings (record = 1 ) as warnings :
618+ with assert_warns (UserWarning ):
619+ warn ("foo" , UserWarning )
620+ assert_equal (0 , len (warnings ))
621+ warn ("bar" , UserWarning )
622+ assert_equal (1 , len (warnings ))
623+
624+ def test_assert_warns__warning_handler_deinstalled_on_failure (self ):
625+ with catch_warnings (record = 1 ) as warnings :
626+ try :
627+ with assert_warns (UserWarning ):
628+ pass
629+ except AssertionError :
630+ pass
631+ assert_equal (0 , len (warnings ))
632+ warn ("bar" , UserWarning )
633+ assert_equal (1 , len (warnings ))
634+
635+ def test_assert_warns__default_message (self ):
636+ with assert_raises_regex (AssertionError ,
637+ r"^ImportWarning not issued" ):
638+ with assert_warns (ImportWarning ):
639+ pass
640+
641+ def test_assert_warns__custom_message (self ):
642+ with assert_raises_regex (AssertionError , r"^Custom Test Message$" ):
643+ with assert_warns (ImportWarning , msg = "Custom Test Message" ):
644+ pass
645+
646+ # assert_warns_regex()
647+
648+ def test_assert_warns_regex__warned (self ):
649+ with assert_succeeds (AssertionError ):
650+ with assert_warns_regex (FutureWarning , r"fo+" ):
651+ warn ("foo" , FutureWarning )
652+
653+ def test_assert_warns_regex__warning_text_matches_in_the_middle (self ):
654+ with assert_succeeds (AssertionError ):
655+ with assert_warns_regex (FutureWarning , r"o" ):
656+ warn ("foo" , FutureWarning )
657+
658+ def test_assert_warns_regex__not_warned (self ):
659+ with assert_raises (AssertionError ):
660+ with assert_warns_regex (UserWarning , r"foo" ):
661+ pass
662+
663+ def test_assert_warns_regex__wrong_type (self ):
664+ with assert_raises (AssertionError ):
665+ with assert_warns_regex (ImportWarning , r"foo" ):
666+ warn ("foo" , UnicodeWarning )
667+
668+ def test_assert_warns_regex__wrong_message (self ):
669+ with assert_raises (AssertionError ):
670+ with assert_warns_regex (UnicodeWarning , r"foo" ):
671+ warn ("bar" , UnicodeWarning )
672+
673+ def test_assert_warns_regex__multiple_warnings (self ):
674+ with assert_succeeds (AssertionError ):
675+ with assert_warns_regex (UserWarning , r"bar2" ):
676+ warn ("foo" , UnicodeWarning )
677+ warn ("bar1" , UserWarning )
678+ warn ("bar2" , UserWarning )
679+ warn ("bar3" , UserWarning )
680+ warn ("baz" , FutureWarning )
681+
682+ def test_assert_warns_regex__warning_handler_deinstalled_on_success (self ):
683+ with catch_warnings (record = 1 ) as warnings :
684+ with assert_warns_regex (UserWarning , r"foo" ):
685+ warn ("foo" , UserWarning )
686+ assert_equal (0 , len (warnings ))
687+ warn ("bar" , UserWarning )
688+ assert_equal (1 , len (warnings ))
689+
690+ def test_assert_warns_regex__warning_handler_deinstalled_on_failure (self ):
691+ with catch_warnings (record = 1 ) as warnings :
692+ try :
693+ with assert_warns_regex (UserWarning , r"" ):
694+ pass
695+ except AssertionError :
696+ pass
697+ assert_equal (0 , len (warnings ))
698+ warn ("bar" , UserWarning )
699+ assert_equal (1 , len (warnings ))
700+
701+ def test_assert_warns_regex__default_message_not_issued (self ):
702+ with assert_raises_regex (
703+ AssertionError ,
704+ r"^no UserWarning matching 'foo.*bar' issued$" ):
705+ with assert_warns_regex (UserWarning , r"foo.*bar" ):
706+ pass
707+
708+ def test_assert_warns_regex__default_message_wrong_message (self ):
709+ with assert_raises_regex (
710+ AssertionError ,
711+ r"^no UserWarning matching 'foo.*bar' issued$" ):
712+ with assert_warns_regex (UserWarning , r"foo.*bar" ):
713+ pass
714+
715+ def test_assert_warns_regex__custom_message (self ):
716+ with assert_raises_regex (AssertionError , r"^Custom Test Message$" ):
717+ with assert_warns_regex (
718+ ImportWarning , r"" , msg = "Custom Test Message" ):
719+ pass
0 commit comments