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.

转载于:https://www.cnblogs.com/mias/p/7718247.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值