Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(17)

Side by Side Diff: Doc/library/unittest.rst

Issue 32080: [issue2578] additional unittest type equality methods SVN Base: http://svn.python.org/view/*checkout*/python/trunk/
Patch Set: minor cleanup Created 10 months, 2 weeks ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | Lib/test/test_gc.py » ('j') | Lib/test/test_unittest.py » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 :mod:`unittest` --- Unit testing framework 2 :mod:`unittest` --- Unit testing framework
3 ========================================== 3 ==========================================
4 4
5 .. module:: unittest 5 .. module:: unittest
6 :synopsis: Unit testing framework for Python. 6 :synopsis: Unit testing framework for Python.
7 .. moduleauthor:: Steve Purcell <stephen_purcell@yahoo.com> 7 .. moduleauthor:: Steve Purcell <stephen_purcell@yahoo.com>
8 .. sectionauthor:: Steve Purcell <stephen_purcell@yahoo.com> 8 .. sectionauthor:: Steve Purcell <stephen_purcell@yahoo.com>
9 .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> 9 .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
10 .. sectionauthor:: Raymond Hettinger <python@rcn.com> 10 .. sectionauthor:: Raymond Hettinger <python@rcn.com>
(...skipping 590 matching lines...) Expand 10 before | Expand all | Expand 10 after
601 Run the test without collecting the result. This allows exceptions raised 601 Run the test without collecting the result. This allows exceptions raised
602 by the test to be propagated to the caller, and can be used to support 602 by the test to be propagated to the caller, and can be used to support
603 running tests under a debugger. 603 running tests under a debugger.
604 604
605 The test code can use any of the following methods to check for and report 605 The test code can use any of the following methods to check for and report
606 failures. 606 failures.
607 607
608 608
609 .. method:: assert_(expr[, msg]) 609 .. method:: assert_(expr[, msg])
610 failUnless(expr[, msg]) 610 failUnless(expr[, msg])
611 assertTrue(expr[, msg]) 611 assertTrue(expr[, msg])
GvR 2009/03/31 15:01:06 Make assertTrue the first/preferred/recommended sp
gregory.p.smith 2009/03/31 16:37:02 On 2009/03/31 15:01:06, GvR wrote: > Make assertTr
612 612
613 Signal a test failure if *expr* is false; the explanation for the error 613 Signal a test failure if *expr* is false; the explanation for the error
614 will be *msg* if given, otherwise it will be :const:`None`. 614 will be *msg* if given, otherwise it will be :const:`None`.
615 615
616 616
617 .. method:: assertEqual(first, second[, msg]) 617 .. method:: assertEqual(first, second[, msg])
618 assertEquals(first, second[, msg])
618 failUnlessEqual(first, second[, msg]) 619 failUnlessEqual(first, second[, msg])
619 620
620 Test that *first* and *second* are equal. If the values do not compare 621 Test that *first* and *second* are equal. If the values do not compare
621 equal, the test will fail with the explanation given by *msg*, or 622 equal, the test will fail with the explanation given by *msg*, or
622 :const:`None`. Note that using :meth:`failUnlessEqual` improves upon 623 :const:`None`. Note that using :meth:`failUnlessEqual` improves upon
GvR 2009/03/31 15:01:06 failUnlessEqal -> assertEqual
gregory.p.smith 2009/03/31 16:37:02 On 2009/03/31 15:01:06, GvR wrote: > failUnlessEqa
623 doing the comparison as the first parameter to :meth:`failUnless`: the 624 doing the comparison as the first parameter to :meth:`failUnless`: the
GvR 2009/03/31 15:01:06 failUnless -> assertTrue (This kind of change may
gregory.p.smith 2009/03/31 16:37:02 On 2009/03/31 15:01:06, GvR wrote: > failUnless ->
624 default value for *msg* can be computed to include representations of both 625 default value for *msg* include representations of both *first* and
625 *first* and *second*. 626 *second*.
627
628 In addition, if *first* and *second* are the exact same type and one of
629 list, tuple, dict, set, or frozenset or any type that a subclass
630 registers :meth:`addTypeEqualityFunc` the type specific equality function
631 will be called in order to generate a more useful default error message.
632
633 .. versionchanged:: 2.7
634 Added the automatic calling of type specific equality function.
626 635
627 636
628 .. method:: assertNotEqual(first, second[, msg]) 637 .. method:: assertNotEqual(first, second[, msg])
629 failIfEqual(first, second[, msg]) 638 failIfEqual(first, second[, msg])
630 639
631 Test that *first* and *second* are not equal. If the values do compare 640 Test that *first* and *second* are not equal. If the values do compare
632 equal, the test will fail with the explanation given by *msg*, or 641 equal, the test will fail with the explanation given by *msg*, or
633 :const:`None`. Note that using :meth:`failIfEqual` improves upon doing 642 :const:`None`. Note that using :meth:`failIfEqual` improves upon doing
634 the comparison as the first parameter to :meth:`failUnless` is that the 643 the comparison as the first parameter to :meth:`failUnless` is that the
635 default value for *msg* can be computed to include representations of both 644 default value for *msg* can be computed to include representations of both
(...skipping 17 matching lines...) Expand all
653 failIfAlmostEqual(first, second[, places[, msg]]) 662 failIfAlmostEqual(first, second[, places[, msg]])
654 663
655 Test that *first* and *second* are not approximately equal by computing 664 Test that *first* and *second* are not approximately equal by computing
656 the difference, rounding to the given number of decimal *places* (default 665 the difference, rounding to the given number of decimal *places* (default
657 7), and comparing to zero. 666 7), and comparing to zero.
658 667
659 Note that comparing a given number of decimal places is not the same as 668 Note that comparing a given number of decimal places is not the same as
660 comparing a given number of significant digits. If the values do not 669 comparing a given number of significant digits. If the values do not
661 compare equal, the test will fail with the explanation given by *msg*, or 670 compare equal, the test will fail with the explanation given by *msg*, or
662 :const:`None`. 671 :const:`None`.
672
673
674 .. method:: assertGreater(first, second, msg=None)
675 assertGreaterEqual(first, second, msg=None)
676 assertLess(first, second, msg=None)
677 assertLessEqual(first, second, msg=None)
678
679 Test that *first* is respectively >, >=, < or <= than *second* depending
680 on the method name. If not, the test will fail with the nice explanation
681 or with the explanation given by *msg*::
682
683 >>> self.assertGreaterEqual(3, 4)
684 AssertionError: "3" unexpectedly not greater than or equal to "4"
685
686 .. versionadded:: 2.7
687
688
689 .. method:: assertMultiLineEquals(self, first, second, msg=None)
690
691 Test that the multiline string *first* is equal to the string *second*.
692 When not equal a diff of the two strings highlighting the differences
693 will be included in the error message.
694
695 If specified *msg* will be used as the error message on failure.
696
697 .. versionadded:: 2.7
698
699
700 .. method:: assertRegexpMatches(text, regexp[, msg=None]):
701
702 Verifies that a *regexp* search matches *text*. Fails with an error
703 message including the pattern and the *text*. *regexp* may be
704 a regular expression object or a string containing a regular expression
705 suitable for use by :func:`re.search`.
706
707 .. versionadded:: 2.7
708
709
710 .. method:: assertIn(first, second, msg=None)
711 assertNotIn(first, second, msg=None)
712
713 Tests that *first* is or is not in *second* with a nice explanitory error
714 message as appropriate.
715
716 If specified *msg* will be used as the error message on failure.
717
718 .. versionadded:: 2.7
719
720
721 .. method:: assertSameElements(expected, actual, msg=None)
722
723 Test that sequence *expected* contains the same elements as *actual*.
724 When they don't an error message listing the differences between the
725 sequences will be generated.
726
727 If specified *msg* will be used as the error message on failure.
728
729 .. versionadded:: 2.7
730
731
732 .. method:: assertSetEquals(set1, set2, msg=None)
733
734 Tests that two sets are equal. If not, an error message is constructed
735 that lists the differences between the sets.
736
737 Fails if either of *set1* or *set2* does not have a :meth:`set.difference`
738 method.
739
740 If specified *msg* will be used as the error message on failure.
741
742 .. versionadded:: 2.7
743
744
745 .. method:: assertDictEquals(expected, actual, msg=None)
746
747 Test that two dictionaries are equal. If not, an error message is
748 constructed that shows the differences in the dictionaries.
749
750 If specified *msg* will be used as the error message on failure.
751
752 .. versionadded:: 2.7
753
754
755 .. method:: assertDictContainsSubset(expected, actual, msg=None)
756
757 Tests whether the key value pairs in dictionary *actual* are a
758 superset of those in *expected*. If not, an error message listing
759 the missing keys and mismatched values is generated.
760
761 If specified *msg* will be used as the error message on failure.
762
763 .. versionadded:: 2.7
764
765
766 .. method:: assertListEquals(list1, list2, msg=None)
767 assertTupleEquals(tuple1, tuple2, msg=None)
768
769 Tests that two lists or tuples are equal. If not an error message is
770 constructed that shows only the differences between the two. An error
771 is also raised if either of the parameters are of the wrong type.
772
773 If specified *msg* will be used as the error message on failure.
774
775 .. versionadded:: 2.7
776
777
778 .. method:: assertSequenceEquals(seq1, seq2, msg=None, seq_type=None)
779
780 Tests that two sequences are equal. If a *seq_type* is supplied, both
781 *seq1* and *seq2* must be instances of *seq_type* or a failure will
782 be raised. If the sequences are different an error message is
783 constructed that shows the difference between the two.
784
785 If specified *msg* will be used as the error message on failure.
786
787 This method is used to implement :meth:`assertListEquals` and
788 :meth:`assertTupleEquals`.
789
790 .. versionadded:: 2.7
663 791
664 792
665 .. method:: assertRaises(exception[, callable, ...]) 793 .. method:: assertRaises(exception[, callable, ...])
666 failUnlessRaises(exception[, callable, ...]) 794 failUnlessRaises(exception[, callable, ...])
667 795
668 Test that an exception is raised when *callable* is called with any 796 Test that an exception is raised when *callable* is called with any
669 positional or keyword arguments that are also passed to 797 positional or keyword arguments that are also passed to
670 :meth:`assertRaises`. The test passes if *exception* is raised, is an 798 :meth:`assertRaises`. The test passes if *exception* is raised, is an
671 error if another exception is raised, or fails if no exception is raised. 799 error if another exception is raised, or fails if no exception is raised.
672 To catch any of a group of exceptions, a tuple containing the exception 800 To catch any of a group of exceptions, a tuple containing the exception
673 classes may be passed as *exception*. 801 classes may be passed as *exception*.
674 802
675 If *callable* is omitted or None, returns a context manager so that the 803 If *callable* is omitted or None, returns a context manager so that the
676 code under test can be written inline rather than as a function:: 804 code under test can be written inline rather than as a function::
677 805
678 with self.failUnlessRaises(some_error_class): 806 with self.failUnlessRaises(some_error_class):
679 do_something() 807 do_something()
680 808
681 .. versionchanged:: 2.7 809 .. versionchanged:: 2.7
682 Added the ability to use :meth:`assertRaises` as a context manager. 810 Added the ability to use :meth:`assertRaises` as a context manager.
811
812
813 .. method:: assertRaisesRegexp(exception, regexp[, callable, ...])
814
815 Like :meth:`assertRaises` but also tests that *regexp* matches
816 on the string representation of the raised exception. *regexp* may be
817 a regular expression object or a string containing a regular expression
818 suitable for use by :func:`re.search`. Examples::
819
820 self.assertRaisesRegexp(ValueError, 'invalid literal for.*XYZ$',
821 int, 'XYZ')
822
823 or::
824
825 with self.assertRaisesRegexp(ValueError, 'literal'):
826 int('XYZ')
827
828 .. versionadded:: 2.7
829
830
831 .. method:: assertIsNone(expr[, msg])
832
833 This signals a test failure if *expr* is not None.
834
835 .. versionadded:: 2.7
836
837
838 .. method:: assertIsNotNone(expr[, msg])
839
840 The inverse of the :meth:`assertIsNone` method.
841 This signals a test failure if *expr* is None.
842
843 .. versionadded:: 2.7
683 844
684 845
685 .. method:: failIf(expr[, msg]) 846 .. method:: failIf(expr[, msg])
686 assertFalse(expr[, msg]) 847 assertFalse(expr[, msg])
687 848
688 The inverse of the :meth:`failUnless` method is the :meth:`failIf` method. 849 The inverse of the :meth:`failUnless` method is the :meth:`failIf` method.
689 This signals a test failure if *expr* is true, with *msg* or :const:`None` 850 This signals a test failure if *expr* is true, with *msg* or :const:`None`
690 for the error message. 851 for the error message.
691 852
692 853
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
726 887
727 888
728 .. method:: id() 889 .. method:: id()
729 890
730 Return a string identifying the specific test case. This is usually the 891 Return a string identifying the specific test case. This is usually the
731 full name of the test method, including the module and class name. 892 full name of the test method, including the module and class name.
732 893
733 894
734 .. method:: shortDescription() 895 .. method:: shortDescription()
735 896
736 Returns a one-line description of the test, or :const:`None` if no 897 Returns a description of the test, or :const:`None` if no description
737 description has been provided. The default implementation of this method 898 has been provided. The default implementation of this method
738 returns the first line of the test method's docstring, if available, or 899 returns the first line of the test method's docstring, if available,
739 :const:`None`. 900 along with the method name.
901
902 .. versionchanged:: 2.7
903
904 In earlier versions this only returned the first line of the test
905 method's docstring, if available or the :const:`None`. That led to
906 undesirable behavior of not printing the test name when someone was
907 thoughtful enough to write a docstring.
908
909
910 .. method:: addTypeEqualityFunc(typeobj, function)
911
912 Registers a type specific :meth:`assertEquals` equality checking
913 function to be called by :meth:`assertEquals` when both objects it has
914 been asked to compare are exactly *typeobj* (not subclasses).
915 *function* must take two positional arguments and a third msg=None
916 keyword argument just as :meth:`assertEquals` does. It must raise
917 self.failureException when inequality between the first two
918 parameters is detected.
919
920 One good use of custom equality checking functions for a type
921 is to raise self.failureException with an error message useful
922 for debugging the by explaining the inequalities in detail.
923
924 .. versionadded:: 2.7
740 925
741 926
742 .. class:: FunctionTestCase(testFunc[, setUp[, tearDown[, description]]]) 927 .. class:: FunctionTestCase(testFunc[, setUp[, tearDown[, description]]])
743 928
744 This class implements the portion of the :class:`TestCase` interface which 929 This class implements the portion of the :class:`TestCase` interface which
745 allows the test runner to drive the test, but does not provide the methods wh ich 930 allows the test runner to drive the test, but does not provide the methods wh ich
746 test code can use to check and report errors. This is used to create test cas es 931 test code can use to check and report errors. This is used to create test cas es
747 using legacy test code, allowing it to be integrated into a :mod:`unittest`\ 932 using legacy test code, allowing it to be integrated into a :mod:`unittest`\
748 -based test framework. 933 -based test framework.
749 934
(...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after
1111 1296
1112 A command-line program that runs a set of tests; this is primarily for making 1297 A command-line program that runs a set of tests; this is primarily for making
1113 test modules conveniently executable. The simplest use for this function is to 1298 test modules conveniently executable. The simplest use for this function is to
1114 include the following line at the end of a test script:: 1299 include the following line at the end of a test script::
1115 1300
1116 if __name__ == '__main__': 1301 if __name__ == '__main__':
1117 unittest.main() 1302 unittest.main()
1118 1303
1119 The *testRunner* argument can either be a test runner class or an already 1304 The *testRunner* argument can either be a test runner class or an already
1120 created instance of it. 1305 created instance of it.
OLDNEW
« no previous file with comments | « no previous file | Lib/test/test_gc.py » ('j') | Lib/test/test_unittest.py » ('J')

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld r511