Python的unittest框架的断言总结

时间:2023-02-14 07:29:57

常用的断言方法如下:

assertFalse:为假时返回True;self.assertFalse(表达式,“表达式为true时打印的message”)

assertTrue:为真时返回True;self.assertTrue(表达式,“表达式为false时打印的message”)

asserEqual:相等返回True;self.assertEqual(表达式1,表达式2,“表达式1不等于表达式2时打印的message”)

assertNotEqual:不相等返回True;self.assertNotEqual(表达式1,表达式2,“表达式1等于表达式2时打印的message”)

assertIsNone:不存在,则pass;self.assertIsNone(表达式,“如果表达存在打印的message”)

assertIsNotNone:存在,则pass;self.assertIsNotNone(表达式,“如果表达式不存在打印的message”)

在官方文档里面看到的整理一下,有些还来不及翻译。

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.

Python的unittest框架的断言总结的更多相关文章

  1. selenium &plus; python自动化测试unittest框架学习(一)selenium原理及应用

    unittest框架的学习得益于虫师的<selenium+python自动化实践>这一书,该书讲得很详细,大家可以去看下,我也只学到一点点用于工作中,闲暇时记录下自己所学才能更加印象深刻. ...

  2. python之unittest框架实现接口测试实例

    python之unittest框架实现接口测试实例 接口测试的方法有很多种,具体到工具有postman,jmeter,fiddler等,但是工具的局限性是测试数据的组织较差,接口的返回工具的判断有限, ...

  3. selenium &plus; python自动化测试unittest框架学习(二)

    1.unittest单元测试框架文件结构 unittest是python单元测试框架之一,unittest测试框架的主要文件结构: File >report >all_case.py &g ...

  4. python的unittest框架中的assert断言

    unittest框架自带断言,如果想用assert断言,一定要引入unittest.TestCase框架才行,不然不会自动识别assert断言

  5. selenium自动化测试、Python单元测试unittest框架以及测试报告和日志输出

    部分内容来自:https://www.cnblogs.com/klb561/p/8858122.html 一.基础介绍 核心概念:test case, testsuite, TestLoder,Tex ...

  6. python之unittest框架使用

    一.unittest框架 unittest属于python内置的单元测试框架. 二.unittest框架的核心概念 test case:指测试用例.unittest中提供了一个基本类TestCase, ...

  7. python selenium --unittest 框架

    转自:http://www.cnblogs.com/fnng/p/3300788.html 学习unittest 很好的一个切入点就是从selenium IDE 录制导出脚本.相信不少新手学习sele ...

  8. Unittest 框架之断言,你学会了吗??

    unittest断言 Python在 unittest.TestCase 类中提供了很多断言方法.断言方法检查你认为应该满足的条件是否确实满足.如果该条件确实满足,你对程序行为的假设就得到了确认,你就 ...

  9. 2019&period;3&period;23 python的unittest框架与requests

    (明天学测试用例集合及输出测试报告!!!) import unittest import requests import json class Test_get(unittest.TestCase): ...

随机推荐

  1. PDF&period;NET框架学习篇之SQL-MAP使用存储过程

    最近一直在学习“深蓝医生”的PDF.NET框架,对Sql-Map使用存储过程有了点小小的体会.基础知识请到http://www.cnblogs.com/bluedoctor/archive/2010/ ...

  2. Bootstrap中glyphicons-halflings-regular&period;woff字体报404错notfound

    今天查看网站的源代码,发现有个glyphicons-halflings-regular.woff文件没有找到,因为我的网站使用了bootstrap的Glyphicons 字体图标,因此需要加载Glyp ...

  3. bzoj 1493&colon; &lbrack;NOI2007&rsqb;项链工厂(线段树)

    1493: [NOI2007]项链工厂 Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 1256  Solved: 545[Submit][Status] ...

  4. Spring事务-三种实现方式

    一.引入JAR文件: 二.开始搭建分层架构---创建账户(Account)和股票(Stock)实体类 Account: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...

  5. C&plus;&plus;入门程序作业1

    将一个int A[]={ ,  ,  ,}定义的可能重复的数字去掉重复的元素. 了解向量,容器如何使用,size,地址的关系,理解unique erase函数的返回值是什么参数 结果:将1,1,1,2 ...

  6. MySQL数据库事务各隔离级别加锁情况--read committed &amp&semi;&amp&semi; MVCC

    之前已经转载过几篇相关的文章,此次基于mysql 5.7 版本,从测试和源码角度解释一下RR,RC级别为什么看到的数据不一样 先补充一下基础知识 基本知识 假设对于多版本(MVCC)的基础知识,有所了 ...

  7. js实现进度条

    不多说,直接上代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quot ...

  8. Django2&period;0引入css、js、img文件

    Django2.0引入css.js.img文件 一.文件结构 二.settings.py的配置 # Static files (CSS, JavaScript, Images) # https://d ...

  9. SpringBoot入坑-请求参数传递

    前一篇我们探讨了关于springboot的配置文件和Controller的使用,本篇我们来一起探讨一下关于springboot如何传递参数的知识. 参数传递我们最常见的就是在url后通过?/& ...

  10. 异常&plus;远程控制Linux-14

    什么是异常 a=8950/0              ZeroDivisioonError: division by zero print (a) ************** b = [1,2] ...