python3_unittest单元测试框架
看见英文懵逼,强迫学习英语
The Unittest suppots test automation,sharing of setup and shutdown code of tests, aggregation of tests into collections, and independence of the tests from the reporting framework
(支持测试自动化,为测试共享设置和关闭代码,将测试集合到集合中,以及从报告框架中独立测试。)
To achieve this,unittest supports some important concepts in an object_oriented way:
(为了实现这一点,unittest以面向对象的方式支持一些重要的概念:)
整理结构:unittest库提供了testcase,test suites,test fixtures, test runner:
test fixture:
A test fixture represents the preparation needed to perform one or more tests, and any associate cleanup actions. This may involve, for example, creating temporary or proxy databases, directories, or starting a server process.
test fixture代表执行一个或多个测试所需的准备工作,以及任何关联清理操作。这可能涉及到,例如创建临时代理数据库、目录或启动服务器进程。
test case :
- A test case is the individual unit of testing. It checks for a specific response to a particular set of inputs.
unittestprovides a base class,TestCase, which may be used to create new test cases.
测试用例是测试的各个单元。它检查对特别输入集合的特定响应。 unittest提供了一个基类, TestCase可以用来创建新的测试用例。
test suite:
A test suite is a collection of test cases, test suites, or both. It is used to aggregate tests that should be executed together.
一个测试套件是测试案例,测试套件,或两者的集合。它被用来聚合应该一起执行的测试。
test runner:
A test runner is a component which orchestrates the execution of tests and provides the outcome to the user. The runner may use a graphical interface, a textual interface, or return a special value to indicate the results of executing the tests.
测试运行是编排的测试的执行,并提供结果给用户的部件。test runner可以使用图形界面,文本界面,或返回一个特殊的值来表示执行测试的结果。

The unittest module provides a rich set of tools for constructing and running tests. This section demonstrates that a small subset of the tools suffice to meet the needs of most users.
该unittest模块为构建和运行测试提供了一套丰富的工具。本节表明,一小部分工具足以满足大多数用户的需求。
1、简单的例子
Here is a short script to test three string methods:
import unittest class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO') def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper()) def test_split(self):
s = 'hello word'
self.assertEqual(s.split(), ['hello', 'word'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2) if __name__ == '__main__':
unittest.main()
运行结果:
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s OK
Passing the-voption to your test script will instructunittest.main()to enable a higher level of verbosity, and produce the following output:
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok ----------------------------------------------------------------------
Ran 3 tests in 0.001s OK
2、命令行:
从命令行中可以运行单元测试的模块,类,甚至单独的测试方法。
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
测试模块也可以通过文件路径指定:
python -m unittest tests/test_something.py
You can run tests with more detail (higher verbosity) by passing in the -v flag(显示更详细的测试结果的说明使用[-v]flag:)
ython3_server/python3_unittest# python3 -m unittest -v demo1.py
test_isupper (demo1.TestStringMethods) ... ok
test_split (demo1.TestStringMethods) ... ok
test_upper (demo1.TestStringMethods) ... ok ----------------------------------------------------------------------
Ran 3 tests in 0.000s OK
查看所有的命令行选项使用命令:
python -m unittest -h
The -s, -p, and -t options can be passed in as positional arguments in that order. The following two command lines are equivalent:
python -m unittest discover -s project_directory -p "*_test.py"
python -m unittest discover project_directory "*_test.py"
本TestCase类提供了一些断言方法来检查并报告故障。下表列出了最常用的方法:
| 方法 | 检查 |
|---|---|
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) |
所有的assert方法都接受一个msg参数,如果指定的话,这个参数将被用作失败时的错误消息
3、实际运用:
参考:
https://docs.python.org/3.6/library/unittest.html
https://www.jianshu.com/p/8e22c1213260
python3_unittest单元测试框架的更多相关文章
- javascript单元测试框架mochajs详解
关于单元测试的想法 对于一些比较重要的项目,每次更新代码之后总是要自己测好久,担心一旦上线出了问题影响的服务太多,此时就希望能有一个比较规范的测试流程.在github上看到牛逼的javascript开 ...
- Google C++单元测试框架GoogleTest(总)
之前一个月都在学习googletest框架,对googletest的文档都翻译了一遍,也都发在了之前的博客里,另外其实还有一部分的文档我没有发,就是GMock的CookBook部分:https://g ...
- Google C++单元测试框架GoogleTest---GMock的CheatSheet文档
CheatSheet文档中包含了GMock所有常用的东西,看了这个基本上就可以用它了,本文接上篇博文:Google C++单元测试框架GoogleTest---Google Mock简介--概念及基础 ...
- Google C++单元测试框架GoogleTest---AdvancedGuide(译文)下
因为AdvancedGuide文档太长,分上下两部分,本文档接googletest--AdvancedGuide(译文)上:Google C++单元测试框架GoogleTest---AdvancedG ...
- JavaScript单元测试框架JsUnit基本介绍和使用
JavaScript单元测试框架JsUnit基本介绍和使用 XUnit framework XUnit是一套标准化的独立于语言的概念和结构集合,用于编写和运行单元测试(Unit tests). 每一个 ...
- Google C++单元测试框架GoogleTest---GTest的Sample1和编写单元测试的步骤
如果你还没有搭建gtest框架,可以参考我之前的博客:http://www.cnblogs.com/jycboy/p/6001153.html.. 1.The first sample: sample ...
- JavaScript单元测试框架-Jasmine
Jasmine的开发团队来自PivotalLabs,他们一开始开发的JavaScript测试框架是JsUnit,来源于著名的JAVA测试框架JUnit.JsUnit是xUnit的JavaScript实 ...
- Python单元测试框架之pytest -- 断言
对于测试来讲,不管是功能测试,自动化测试,还是单元测试.一般都会预设一个正确的预期结果,而在测试执行的过程中会得到一个实际的结果.测试的成功与否就是拿实际的结果与预期的结果进行比较.这个比的过程实际就 ...
- Python单元测试框架之pytest -- 生成测试报告
继续pytest单元测试框架的学习,pytest可以生成多种类型的测试报告.这一节就来学习pytest如何生成测试报告. 创建test_calss.py 测试用例文件,这里以测试该文件为例. #cod ...
随机推荐
- 复习及总结--.Net线程篇(1)
老是没耐心写这些东西,最近想想也工作两年了,该对自己的东西做个整理了,不知道这次能坚持写几篇,总得来说尽量督促自己吧 言归正传,.net中的多线程主要可以使用两种方法进行调用 1,异步调用 2,Thr ...
- selenium的常用方法
1.常用定位方法 find_element_by_id()find_element_by_name()find_element_by_class_name()find_element_by_tag_n ...
- Python 打包程序
一.打包成exe 1.安装pyinstaller #只要你能FQ连接https://pypi.python.org/pypi下载会很快,不用担心超时问题. https://pypi.python.or ...
- cobbler上部署centos系统修改网卡地址成eth0
编辑cobbler的profile文件: cobbler profile edit --name=CentOS-7.2-x86_64 --kopts='net.ifnames=0 biosdevn ...
- globalToLocal和localToGlobal
官方API: groupOut全局坐标(50,50) gourpIn全局坐标(100,100),并嵌套在groupOut里 btn全局坐标(150,150),并嵌套在groupIn里 获取组件全局坐标 ...
- 《从零开始学Swift》学习笔记(Day 66)——Cocoa Touch设计模式及应用之通知机制
原创文章,欢迎转载.转载请注明:关东升的博客 通知(Notification)机制是基于观察者(Observer)模式也叫发布/订阅(Publish/Subscribe)模式,是 MVC( 模型-视图 ...
- android sdk 中跨平台部分
跨平台部分: platforms samples sources system-images 这几个可以直接拷贝即可
- mysql - json - look up subobjects or nested values directly by key or array index without reading all values
w https://dev.mysql.com/doc/refman/5.7/en/json.html
- 前端开发 - HTML
1.index2.head标签相关内容3.常用标签一4.常用标签二 table5.常用标签二 form6.标签分类 1.index <!--声明文档的类型 标记该文档为HTML5的文件--> ...
- 剑指Offer——把数组排成最小的数
题目描述: 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. 分析: 排 ...