单元测试

每完成一个单元测试,Python都会打印一个字符:

测试通过打印一个句点;测试引发错误打印E;测试导致断言失败打印F

模块unittest

import unittest
from name_function import get_formatted_name#导入要测试的函数 class NamesTestCase(unittest.TestCase):#注意类的参数哦
"""方法名字开头test,测试时会自动执行""" def test_first_last_name(self):
formatted_name = get_formatted_name('janis', 'joplin')
self.assertEqual(formatted_name, 'Janis Joplin') def test_first_last_middle_name(self):
formatted_name = get_formatted_name(
'wolfgang', 'mozart', 'amadeus')
self.assertEqual(formatted_name, 'Wolfgang Amadeus Mozart')#方法:断言
#将括号内左方同右方比较,相等则无事,不等则警报 unittest.main()#让python运行这个文件测试

方法setUP

import unittest
from survey import AnonymousSurvey class TestAnonymousSurvey(unittest.TestCase):
"""Tests for the class AnonymousSurvey.""" def setUp(self):#TestCase类中包含了方法setUP Python将先运行它再运行以test_为前缀的方法
"""用来创建测试实例对象 """
question = "What language did you first learn to speak?"
self.my_survey = AnonymousSurvey(question)
self.responses = ['English', 'Spanish', 'Mandarin'] def test_store_single_response(self):
"""Test that a single response is stored properly."""
self.my_survey.store_response(self.responses[0])
self.assertIn(self.responses[0], self.my_survey.responses) def test_store_three_responses(self):
"""Test that three individual responses are stored properly."""
for response in self.responses:
self.my_survey.store_response(response)
for response in self.responses:
self.assertIn(response, self.my_survey.responses) unittest.main()

最新文章

  1. 利用Maple推导向量方程的微分公式
  2. JUnit笔记
  3. Spark核心概念
  4. Number Sequence(KMP,判断子串 模板)
  5. UIImageView加抖动效果(转)
  6. (转载)shell日志分析常用命令
  7. Render Texture的使用(截取rendertexture的一帧到Texture2D)
  8. 【机房重构】SQL之视图
  9. hdu1230火星A+B (大数题)
  10. sql2008R2数据库备份--双机备份
  11. T-SQL使用案例——结果数据前面自动补0
  12. ASP.NET Core实现强类型Configuration读取配置数据
  13. Android02-控件
  14. vue 和 react 学习 异同点
  15. 同事搭一个测试RAC说节点2发现idle了,报ORA-00304
  16. php如何和linux进行通讯
  17. Oracle 12c的自增列Identity Columns
  18. npm install —— 从一个简单例子,看本地安装与全局安装的区别
  19. 包管理 ----- Linux操作系统rpm包安装方式步骤
  20. 0117 面向对象OOP有关方法、类、构造方法及权限修饰符的练习

热门文章

  1. 第一次安装android studio遇到的问题
  2. 深入分析Java反射(三)-泛型
  3. HDU 1004 Let the Balloon Rise(STL初体验之map)
  4. bootstrap 按钮组件
  5. Spark Streaming运行流程及源码解析(一)
  6. Go语言实现:【剑指offer】用两个栈实现队列
  7. Docker的基本使用与简介
  8. Hapi+MySql项目实战环境初始化(一)
  9. mongodb搭建带auth的主从
  10. HDU 1042 大数阶乘