python 断言方法_Python 断言方法: assert

本文详细介绍了Python中的assert系列方法,包括assertAlmostEqual、assertDictContainsSubset、assertEqual、assertFalse等,并解释了它们的用法和作用,帮助读者理解和掌握Python断言在测试中的应用。
摘要由CSDN通过智能技术生成

Python 断言方法: assert

|assertAlmostEqual(self,first,second,places=None,msg=None,delta=None)

|Failifthe two objects are unequalasdeterminedbytheir

|difference rounded to the given numberofdecimalplaces

|(default7)andcomparing to zero,orbycomparing that the

| between the two objects is more than the given delta.|

|Notethatdecimalplaces(fromzero)are usuallynotthe same

|assignificant digits(measuredfromthe most signficant digit).

|

|Ifthe two objects compare equalthenthey will automatically

| compare almost equal.|

|assertAlmostEquals=assertAlmostEqual(self,first,second,places=None,msg=None,delta=None)

|

|assertDictContainsSubset(self,expected,actual,msg=None)

| Checks whether actual is a superset of expected.|

|assertDictEqual(self,d1,d2,msg=None)

|

|assertEqual(self,first,second,msg=None)

|Failifthe two objects are unequalasdeterminedbythe'=='

| operator.|

|assertEquals=assertEqual(self,first,second,msg=None)

|

|assertFalse(self,expr,msg=None)

| Check that the expression is false.|

|assertGreater(self,a,b,msg=None)

|Justlikeself.assertTrue(a>b),butwitha nicerdefaultmessage.

|

|assertGreaterEqual(self,a,b,msg=None)

|Justlikeself.assertTrue(a>=b),butwitha nicerdefaultmessage.

|

|assertIn(self,member,container,msg=None)

|Justlikeself.assertTrue(ainb),butwitha nicerdefaultmessage.

|

|assertIs(self,expr1,expr2,msg=None)

|Justlikeself.assertTrue(aisb),butwitha nicerdefaultmessage.

|

|assertIsInstance(self,obj,cls,msg=None)

|Sameasself.assertTrue(isinstance(obj,cls)),witha nicer

| default message.|

|assertIsNone(self,obj,msg=None)

|Sameasself.assertTrue(objisNone),witha nicerdefaultmessage.

|

|assertIsNot(self,expr1,expr2,msg=None)

|Justlikeself.assertTrue(aisnotb),butwitha nicerdefaultmessage.

|

|assertIsNotNone(self,obj,msg=None)

| Included for symmetry with assertIsNone.|

|assertItemsEqual(self,expected_seq,actual_seq,msg=None)

|Anunordered sequence specific comparison.Itasserts that

| actual_seq and expected_seq have the same element counts.|Equivalentto::

|

|self.assertEqual(Counter(iter(actual_seq)),

|Counter(iter(expected_seq)))

|

| Asserts that each element has the same count in both sequences.|Example:

|-[0,1,1]and[1,0,1]compare equal.

|-[0,0,1]and[0,1]compare unequal.

|

|assertLess(self,a,b,msg=None)

|Justlikeself.assertTrue(a

|

|assertLessEqual(self,a,b,msg=None)

|Justlikeself.assertTrue(a<=b),butwitha nicerdefaultmessage.

|

|assertListEqual(self,list1,list2,msg=None)

| A list-specific equality assertion.

|

| Args:

| list1: The first list to compare.

| list2: The second list to compare.

| msg: Optional message to use on failure instead of a list of

| differences.|

|assertMultiLineEqual(self,first,second,msg=None)

| Assert that two multi-line strings are equal.|

|assertNotAlmostEqual(self,first,second,places=None,msg=None,delta=None)

|Failifthe two objects are equalasdeterminedbytheir

|difference rounded to the given numberofdecimalplaces

|(default7)andcomparing to zero,orbycomparing that the

| between the two objects is Less than the given delta.|

|Notethatdecimalplaces(fromzero)are usuallynotthe same

|assignificant digits(measuredfromthe most signficant digit).

|

| Objects that are equal automatically fail.|

|assertNotAlmostEquals=assertNotAlmostEqual(self,first,second,places=None,msg=None,delta=None)

|

|assertNotEqual(self,first,second,msg=None)

|Failifthe two objects are equalasdeterminedbythe'!='

| operator.|

|assertNotEquals=assertNotEqual(self,first,second,msg=None)

|

|assertNotIn(self,member,container,msg=None)

|Justlikeself.assertTrue(anotinb),butwitha nicerdefaultmessage.

|

|assertNotIsInstance(self,obj,cls,msg=None)

| Included for symmetry with assertIsInstance.|

|assertNotRegexpMatches(self,text,unexpected_regexp,msg=None)

|Failthe testifthe text matches the regular expression.

|

|assertRaises(self,excClass,callableObj=None,*args,**kwargs)

|Failunlessan exceptionofclassexcClassisraised

|bycallableObjwheninvokedwitharguments argsandkeyword

|arguments kwargs.Ifa different typeofexceptionis

|raised,it willnotbe caught,andthe testcasewill be

|deemed to have suffered an error,exactlyasforan

| unexpected exception.|

|IfcalledwithcallableObj omittedorNone,willreturna

|contextobjectused likethis::

|

|withself.assertRaises(SomeException):

|do_something()

|

|Thecontext manager keeps a reference to the exceptionas

|the'exception'attribute.Thisallows you to inspect the

|exception after the assertion::

|

|withself.assertRaises(SomeException)ascm:

|do_something()

|the_exception=cm.exception

|self.assertEqual(the_exception.error_code,3)

|

|assertRaisesRegexp(self,expected_exception,expected_regexp,callable_obj=None,*args,**kwargs)

| Asserts that the message in a raised exception matches a regexp.

|

| Args:

| expected_exception: Exception class expected to be raised.

| expected_regexp: Regexp (re pattern object or string) expected

| to be found in error message.

| callable_obj: Function to be called.

| args: Extra args.

| kwargs: Extra kwargs.|

|assertRegexpMatches(self,text,expected_regexp,msg=None)

| Fail the test unless the text matches the regular expression.|

|assertSequenceEqual(self,seq1,seq2,msg=None,seq_type=None)

|Anequality assertionforordered sequences(like listsandtuples).

|

|Forthe purposesofthisfunction,a valid ordered sequence typeisone

| which can be indexed, has a length, and has an equality operator.

|

| Args:

| seq1: The first sequence to compare.

| seq2: The second sequence to compare.

| seq_type: The expected datatype of the sequences, or None if no

| datatype should be enforced.

| msg: Optional message to use on failure instead of a list of

| differences.|

|assertSetEqual(self,set1,set2,msg=None)

| A set-specific equality assertion.

|

| Args:

| set1: The first set to compare.

| set2: The second set to compare.

| msg: Optional message to use on failure instead of a list of

| differences.|

|assertSetEqual uses ducktyping to support different typesofsets,and

|isoptimizedforsets specifically(parameters must support a

| difference method).|

|assertTrue(self,expr,msg=None)

| Check that the expression is true.|

|assertTupleEqual(self,tuple1,tuple2,msg=None)

| A tuple-specific equality assertion.

|

| Args:

| tuple1: The first tuple to compare.

| tuple2: The second tuple to compare.

| msg: Optional message to use on failure instead of a list of

| differences.

来源: http://www.bubuko.com/infodetail-3101135.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值