25 【python入门指南】如何编写测试代码
python如何编写测试代码
python内置了unittest,使得写应用层的单元测试变得超乎寻常的简单。
1,执行单个测试函数
#!/bin/python import unittest class TestMathFunc(unittest.TestCase):
def test_add(self):
self.assertEqual(3, 1+2)
self.assertEqual(4, 2+2)
self.assertNotEqual(3, 1+3) def runTest(self):
self.test_add() suite = unittest.TestSuite()
testCase = [TestMathFunc()]
suite.addTests(testCase)
runner = unittest.TextTestRunner(verbosity = 2)
runner.run(suite)
单元测试,只需要仿照上面的例子写就可以完成自己的测试代码。
其中包含几个部分:
a,测试类(要继承unittest.TestCase);
b,测试小组件,unittest.TestSuite,负责将测试类装入进来;
c,执行类,unittest.TextTestRunner;
2,执行多个测试函数
#!/bin/python import unittest class TestMathFunc(unittest.TestCase):
def test_add(self):
self.assertEqual(3, 1+2)
self.assertEqual(4, 2+2)
self.assertNotEqual(3, 1+3) def test_minus(self):
self.assertEqual(-1, 1-2) def runTest(self):
self.test_add()
self.test_minus() suite = unittest.TestSuite()
#testCase = [TestMathFunc('test_add'), TestMathFunc('test_minus')]
testCase = [TestMathFunc()]
suite.addTests(testCase)
runner = unittest.TextTestRunner(verbosity = 2)
runner.run(suite)
修改runTest函数,将测试函数包含进来即可。
3,执行多个测试类
#!/bin/python import unittest class TestStringFunc(unittest.TestCase):
def test_str_concat(self):
self.assertEqual("abcabc", "abc" + "abc") def runTest(self):
self.test_str_concat() class TestMathFunc(unittest.TestCase):
def test_add(self):
self.assertEqual(3, 1+2)
self.assertEqual(4, 2+2)
self.assertNotEqual(3, 1+3) def test_minus(self):
self.assertEqual(-1, 1-2) def runTest(self):
self.test_add()
self.test_minus() suite = unittest.TestSuite()
testCase = [TestMathFunc(), TestStringFunc()]
suite.addTests(testCase)
runner = unittest.TextTestRunner(verbosity = 2)
runner.run(suite)
在suite添加多个测试类,即可。
当然还有另外的方式:
... suite = unittest.TestSuite() testCase = [TestMathFunc()]
suite.addTests(testCase) testCase2 = TestStringFunc()
suite.addTest(testCase2) runner = unittest.TextTestRunner(verbosity = 2)
runner.run(suite)
通过以上代码片段,即可实现,自主地控制多个测试类,自主地控制多个函数执行测试,十分方便。
4,有时候我们不想每个测试函数都显示调用,而是加载所有test_开头的测试函数,怎么做?
构建两个文件:t3.py,main.py
main文件存放执行代码,t3文件仅存放测试类,不存放任何执行代码
main.py
#!/bin/python
#main.py
import unittest
from t3 import TestMathFunc, TestStringFunc suite2 = unittest.TestLoader().discover('.', 't3.py') runner = unittest.TextTestRunner(verbosity = 2)
runner.run(suite2)
t3.py
#!/bin/python
#t3.py import unittest class TestStringFunc(unittest.TestCase):
def test_str_concat(self):
self.assertEqual("abcabc", "abc" + "abc") # def runTest(self):
# self.test_str_concat() class TestMathFunc(unittest.TestCase):
def test_add(self):
self.assertEqual(3, 1+2)
self.assertEqual(4, 2+2)
self.assertNotEqual(3, 1+3) def test_minus(self):
self.assertEqual(-1, 1-2) # def runTest(self):
# self.test_add()
# self.test_minus()
执行main.py,输出结果:
test_add (t3.TestMathFunc) ... ok
test_minus (t3.TestMathFunc) ... ok
test_str_concat (t3.TestStringFunc) ... ok ----------------------------------------------------------------------
Ran 3 tests in 0.001s
通过 unittest.TestLoader().discover('.', 't3.py') 将t3文件中包含的所有测试类导入到suite中。
依赖这个功能,我们可以将所有的需要测试的文件,导入到main主函数中,进行集中管理和测试。注意:这里不再依赖runTest函数,只要函数名中包含test前缀就会被包含进来。
5,有时候测试用例需要提供上下文环境,怎么做?
我们针对例子4,仅修改t3.py测试类。
class TestMathFunc(unittest.TestCase):
def setUp(self):
print("\nTestMathFunc:setup")
self.a = 1
self.b = 2 def test_add(self):
self.assertEqual(3, 1+2)
self.assertEqual(4, 2+2)
self.assertNotEqual(3, 1+3)
self.assertEqual(3, self.a + self.b) def test_minus(self):
self.assertEqual(-1, 1-2)
这里我们准备了环境信息(准备两个变量a和b),使用这两个变量进行单元测试。
test_add (t3.TestMathFunc) ...
TestMathFunc:setup
ok
test_minus (t3.TestMathFunc) ...
TestMathFunc:setup
ok
test_str_concat (t3.TestStringFunc) ... ok ----------------------------------------------------------------------
Ran 3 tests in 0.002s OK
函数的输出如上。
类的单元测试,每个函数执行之前都会执行setUp函数。
对应的是tearDown,用来销毁构建的环境信息。可以自己尝试下。
参考网站:
更详细的解释:https://www.jianshu.com/p/38948d0d73f5
最详细的官方文档:https://docs.python.org/3/library/unittest.html#module-unittest
25 【python入门指南】如何编写测试代码的更多相关文章
- 使用 xunit 编写测试代码
使用 xunit 编写测试代码 Intro xunit 是 .NET 里使用非常广泛的一个测试框架,有很多测试项目都是在使用 xunit 作为测试框架,不仅仅有很多开源项目在使用,很多微软的项目也在使 ...
- AngularJS快速入门指南19:示例代码
本文给出的大部分示例都可以直接运行,通过点击运行按钮来查看结果,同时支持在线编辑代码. <div ng-app=""> <p>Name: <input ...
- 《Three.js 入门指南》3.0 - 代码构建的最基本结构。
3.0 代码构建的最基本结构 说明: 我们必需首先知道,Three.js 的一些入门级概念: 我们需要知道,OpenGL 是一套三维实现的标准,为什么说是标准,因为它是跨平台,跨语言的.甚至CAD以及 ...
- Python入门指南(超详细)
Python 是一门非常容易上手的语言,通过查阅资料和教程,也许一晚上就能写出一个简单的爬虫.但 Python 也是一门很难精通的语言,因为简洁的语法背后隐藏了许多黑科技.本文主要针对的读者是: 毫无 ...
- python入门机器学习,3行代码搞定线性回归
本文着重是重新梳理一下线性回归的概念,至于几行代码实现,那个不重要,概念明确了,代码自然水到渠成. “机器学习”对于普通大众来说可能会比较陌生,但是“人工智能”这个词简直是太火了,即便是风云变化的股市 ...
- Python 入门指南
Release: 3.4 Date: March 29, 2014 Python 是一门简单易学且功能强大的编程语言. 它拥有高效的高级数据结构,并且能够用简单而又高效的方式进行面向对象编程. Pyt ...
- 24 【python入门指南】class
一.类 1.1,构造函数,析构函数 #!/bin/python class dog(): def __init__(self, age, name): self.age = age self.name ...
- 4、基于JZ2440之编写测试代码处理(处理图片识别人脸)
1.代码如下: void detectAndDisplay(Mat image) { CascadeClassifier ccf; //创建脸部对象 //ccf.load(xmlPath); //导入 ...
- python测试代码
前言: 编写函数或者类时,需要编写测试代码,来保证其的功能运行是否按预期的那样工作.在程序添加新的代码时,用来测试是否会破坏本身的功能. 我们使用python自带的unittest模块来测试代码. 编 ...
随机推荐
- js弹出div层内容(按回退键关闭div层及遮罩)
<!--弹出的div列表对应的详情--> <div id="newhtml" class="white_content"> <di ...
- 初识web.xml文件
做了那么久的web项目都没有花心思了充分解下这个文件有什么用,看项目配制是否都差不多呢 ======================================================== ...
- 尚硅谷springboot学习23-SpringMVC配置
1. Spring MVC auto-configuration 以下是SpringBoot对SpringMVC的默认配置:(WebMvcAutoConfiguration) Inclusion of ...
- es查询时报 Data too large
报错如下: 原因: https://www.cnblogs.com/jiu0821/p/6526930.html 参数 indices.fielddata.cache.size 控制有多少堆内存是分配 ...
- 健康检测文件httpchk.jsp
静态显示: <html><body><center> Now time is: <%=new java.util.Date()%> </cente ...
- foreman自动化工具安装使用
简单的安装指导在官网上 官网地址为:https://theforeman.org/ 点击get started 找到 Installation 选择直接的发行版按照步骤一个一个来 需要注意的是,主机名 ...
- Linux基本命令(新手入门使用)
Linux常用基本命令主要包括目录操作命令.文件操作命令.文件查看命令.磁盘管理命令.用户管理命令.系统管理命令等. 目录操作命令:cd.ls.mkdir.pwd.rmdir 文件操作命令:cp.mv ...
- bug提单规范
一.提单模板 标题:[项目组][模块][子模块][发生原因]问题简要描述描述:[预置条件] 有就写清楚,没有就写无[操作步骤]1.XXXXX2.XXXXXX3.XXXXX[实际结果] XXXXX[预期 ...
- jQuery中事情的动态绑定
在jQuery的开发过程中,我们往往需要处理各种事件,例如click(),hover()等.在jQuery的API中,我们可以使用不同的方法来将这些事件绑定到特定的元素中.今天这篇文章中,我们将要介绍 ...
- 【原】Ubuntu virtual terminal
CTRL+ALT+F1 ~ F6 six virtual terminal ALT-F7 return to graphic desktop