python 斷言大全

來源:互聯網
上載者:User

標籤:條件   分享   dict   圖片   fonts   技術   equals   ted   second   

參考連結:51668672

 

1. 小數位模糊等於

 

 

 

 

 

自動化指令碼最重要的是斷言,正確設定斷言以後才能協助我們判斷測試案例執行結果。

在小同事的協助下,整理了一份比較詳細的斷言方法。

一、先說說unittest常用的斷言吧

常用的就以下幾個,網上一搜一大堆。python版本2.7以上都可以調用了。

斷言文法 解釋
assertEqual(a, b)  判斷a==b
assertNotEqual(a, b) 判斷a!=b
assertTrue(x) bool(x) is True
assertFalse(x) bool(x) is False
assertIs(a, b) a is b
assertIsNot(a, b)  a is not b
assertIsNone(x)  x is None
assertIsNotNone(x) x is not None
assertIn(a, b)  a in b
assertNotIn(a, b) a not in b
assertIsInstance(a, b) isinstance(a, b) 
assertNotIsInstance(a, b)  not isinstance(a, b)

二、最近整理的一些

在官方文檔裡面看到的整理一下,有些還來不及翻譯。

assertAlmostEqual(first, second[, places, ...])

適用於小數,place是應最少幾位相等布爾值才為1(預設為7),如果在place位以內不同則宣告失敗。

assertDictContainsSubset(expected, actual[, msg])

檢查實際是否超預期

assertDictEqual(d1, d2[, msg])

前後字典是否相同

assertEqual(first, second[, msg])

前後兩個數不等的話,失敗

assertFalse(expr[, msg])

檢查運算式是否為假

assertGreater(a, b[, msg])

和self.assertTrue(a > b)用法一樣,但是多了設定條件 .

assertGreaterEqual(a, b[, msg])

和self.assertTrue(a > =b)用法一樣,但是多了設定條件 .

assertIn(member, container[, msg])

self.assertTrue(a in b)

assertIs(expr1, expr2[, msg])

assertTrue(a is b)

assertIsInstance(obj, cls[, msg])

Isinstance(a,b)

assertIsNone(obj[, msg])

Obj is none.

assertIsNot(expr1, expr2[, msg])

a is not b.

assertIsNotNone(obj[, msg])

Obj is not none.

assertItemsEqual(expected_seq, actual_seq[, msg])

一個無序的序列特異性的比較。

assertLess(a, b[, msg])

Just like self.assertTrue(a < b), but with a nicer default message.

assertLessEqual(a, b[, msg])

Just like self.assertTrue(a <= b), but with a nicer default message.

assertListEqual(list1, list2[, msg])

List1與list2是否相等.

assertMultiLineEqual(first, second[, msg])

斷言,2個多行字串是相等的

assertNotAlmostEqual(first, second[, ...])

Fail if the two objects are equal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero, or by comparing that the between the two objects is less than the given delta.

assertNotAlmostEquals(first, second[, ...])

Fail if the two objects are equal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero, or by comparing that the between the two objects is less than the given delta.

assertNotEqual(first, second[, msg])

Fail if the two objects are equal as determined by the ‘==’

assertNotEquals(first, second[, msg])

Fail if the two objects are equal as determined by the ‘==’

assertNotIn(member, container[, msg])

Just like self.assertTrue(a not in b), but with a nicer default message.

assertNotIsInstance(obj, cls[, msg])

Included for symmetry with assertIsInstance.

assertNotRegexpMatches(text, unexpected_regexp)

如果文本匹配Regex,將失敗。

assertRaises(excClass[, callableObj])

除非excclass類拋出異常失敗

assertRaisesRegexp(expected_exception, ...)

認為在引發異常的情況下訊息匹配一個Regex。

assertRegexpMatches(text, expected_regexp[, msg])

測試失敗,除非文本匹配Regex。

assertSequenceEqual(seq1, seq2[, msg, seq_type])

有序序列的相等斷言 (like lists and tuples).

assertSetEqual(set1, set2[, msg])

A set-specific equality assertion.

assertTrue(expr[, msg])

Check that the expression is true.

assertTupleEqual(tuple1, tuple2[, msg])

A tuple-specific equality assertion.

assert_(expr[, msg])

Check that the expression is true.


三、在unittest包裡面看到的比較全的斷言


 

python 斷言大全

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.