python的unittest單元測試框架斷言整理


先介紹下unittest的基本使用方法:

1.import unittest
2.定義一個繼承自unittest.TestCase的測試用例類
3.定義setUp和tearDown,在每個測試用例前后做一些輔助工作。
4.定義測試用例,名字以test開頭。
5.一個測試用例應該只測試一個方面,測試目的和測試內容應很明確。主要是調用assertEqual、assertRaises等斷言方法判斷程序執行結果和預期值是否相符。
6.調用unittest.main()啟動測試
7.如果測試未通過,會輸出相應的錯誤提示。如果測試全部通過則不顯示任何東西,這時可以添加-v參數顯示詳細信息。

 

import random
import unittest

class TestSequenceFunctions(unittest.TestCase):

def setUp(self):
self.seq = range(10)

def test_shuffle(self):
# make sure the shuffled sequence does not lose any elements
random.shuffle(self.seq)
self.seq.sort()
self.assertEqual(self.seq, range(10))

# should raise an exception for an immutable sequence
self.assertRaises(TypeError, random.shuffle, (1,2,3))

def test_choice(self):
element = random.choice(self.seq)
self.assertTrue(element in self.seq)

def test_error(self):
element = random.choice(self.seq)
self.assertTrue(element not in self.seq)


if __name__ == '__main__':
unittest.main()


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

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

 在unittest包里面看到的比較全的斷言:

 其他:


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)
如果文本匹配正則表達式,將失敗。
assertRaises(excClass[, callableObj])
除非excclass類拋出異常失敗
assertRaisesRegexp(expected_exception, ...)
認為在引發異常的情況下消息匹配一個正則表達式。
assertRegexpMatches(text, expected_regexp[, msg])
測試失敗,除非文本匹配正則表達式。
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.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM