11-1 城市和国家:编写一个函数,它接受两个形参:一个城市名和一个国家名。这个函数返回一个格式为City, Country 的字符串,如Santiago, Chile。将这个函数存储在一个名为city _functions.py 的模块中。

创建一个名为test_cities.py 的程序,对刚编写的函数进行测试(别忘了,你需要导入模块unittest 以及要测试的函数)。编写一个名为test_city_country()的方法,核实使用类似于'santiago'和'chile'这样的值来调用前述函数时,得到的字符串是正确的。运行test_cities.py,确认测试test_city_country()通过了。

city _functions.py

 def city_country(city, country):
city_name = city + ', ' + country
return city_name.title()

test_city_country()

 import unittest
from city_function import city_country class CityTestCase(unittest.TestCase):
"""测试city_function.py""" def test_city_country(self):
city_name = city_country('santiago', 'chile')
self.assertEqual(city_name, 'Santiago, Chile') unittest.main()

11-2 人口数量:修改前面的函数,使其包含第三个必不可少的形参population,并返回一个格式为City, Country – population xxx 的字符串,如Santiago, Chile – population 5000000。运行test_cities.py,确认测试test_city_country()未通过。
修改上述函数,将形参population 设置为可选的。再次运行test_cities.py,确认测试test_city_country()又通过了。
再编写一个名为test_city_country_population()的测试,核实可以使用类似于'santiago'、'chile'和'population=5000000'这样的值来调用这个函数。再次运行test_cities.py,确认测试test_city_country_population()通过了。

city _functions.py

def city_country(city, country, population=''):
if population:
city_name = city + ', ' + country + ' - population ' + str(population)
else:
city_name = city + ', ' + country
return city_name.title()

test_cities.py

 import unittest
from city_function import city_country class CityTestCase(unittest.TestCase):
"""测试city_function.py""" def test_city_country(self):
city_name = city_country('santiago', 'chile')
self.assertEqual(city_name, 'Santiago, Chile') def test_city_country_population(self):
city_name = city_country('santiago', 'chile', population=5000000)
self.assertEqual(city_name, "Santiago, Chile - Population 5000000") unittest.main()
assertEqual()方法的作用是将要测试函数的执行结果返回的值与预期的结果对比,如果相等则Ok通过
而assertNotEqual()与assertEqual()是相反的,若是不相等则Ok通过
想了解更多unittest类的功能请查阅Python标准库

Python学习笔记之测试函数的更多相关文章

  1. python学习笔记整理——字典

    python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...

  2. VS2013中Python学习笔记[Django Web的第一个网页]

    前言 前面我简单介绍了Python的Hello World.看到有人问我搞搞Python的Web,一时兴起,就来试试看. 第一篇 VS2013中Python学习笔记[环境搭建] 简单介绍Python环 ...

  3. python学习笔记之module && package

    个人总结: import module,module就是文件名,导入那个python文件 import package,package就是一个文件夹,导入的文件夹下有一个__init__.py的文件, ...

  4. python学习笔记(六)文件夹遍历,异常处理

    python学习笔记(六) 文件夹遍历 1.递归遍历 import os allfile = [] def dirList(path): filelist = os.listdir(path) for ...

  5. python学习笔记--Django入门四 管理站点--二

    接上一节  python学习笔记--Django入门四 管理站点 设置字段可选 编辑Book模块在email字段上加上blank=True,指定email字段为可选,代码如下: class Autho ...

  6. python学习笔记--Django入门0 安装dangjo

    经过这几天的折腾,经历了Django的各种报错,翻译的内容虽然不错,但是与实际的版本有差别,会出现各种奇葩的错误.现在终于找到了解决方法:查看英文原版内容:http://djangobook.com/ ...

  7. python学习笔记(一)元组,序列,字典

    python学习笔记(一)元组,序列,字典

  8. Pythoner | 你像从前一样的Python学习笔记

    Pythoner | 你像从前一样的Python学习笔记 Pythoner

  9. OpenCV之Python学习笔记

    OpenCV之Python学习笔记 直都在用Python+OpenCV做一些算法的原型.本来想留下发布一些文章的,可是整理一下就有点无奈了,都是写零散不成系统的小片段.现在看 到一本国外的新书< ...

随机推荐

  1. Rust中的哈希Map

    严谨! fn main() { use std::collections::HashMap; let mut scores = HashMap::new(); scores.insert(String ...

  2. linux (07) redis详解

    一.redis持久化RDB 1.在配置文件中添加参数,开启rdb功能 redis.conf 写入 port 6379 daemonize yes logfile /data/6379/redis.lo ...

  3. windows 7系统下查看被占用的端口并解除占用

    运行输入cmd,在命令行输入 netstat -ano 查找所占用端口所在的行,如图本例子被占用端口为9999,记住对应的pid 然后输入 tasklist|findstr pid(此处为9528) ...

  4. 201871010108-高文利《面向对象程序设计(java)》第十一周学习总结

    项目 内容 这个作业属于哪个课程 <任课教师博客主页链接> https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 <作业链接地址> ht ...

  5. JavaMap常用操作

    判断key值是否存在 map.containsKey("youkey") 根据key修改value值 map.put("youkey","you ne ...

  6. [LeetCode] 658. Find K Closest Elements 寻找K个最近元素

    Given a sorted array, two integers k and x, find the k closest elements to x in the array. The resul ...

  7. [LeetCode] 15. 3Sum 三数之和

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  8. js 立即调用函数 IIFE(Immediately Invoked Function Expression) 【转】

    原文链接:https://www.cnblogs.com/ming-os9/p/8891300.html JS中 (function(){...})()立即执行函数   1 (function(){. ...

  9. Redis数据结构及常用命令(草稿)

    通用命令 数据类型 string 字符 list 列表 set 集合 zset 有序集合 hash 散列(字典中的字典) bitmap 位图 hyperloglog

  10. docker-composer 简单教程

    原文地址:https://blog.51cto.com/9291927/2310444 Docker快速入门——Docker-Compose 一.Docker-Compose简介 1.Docker-C ...