【日常Python-18】测试代码

测试函数

任务1.城市和国家

  1. 问题描述
    编写一个函数,它接受两个形参:一个城市名和一个国家名。这个函数返回一个格式为City, Country 的字符串,Santiago, Chile 。将 这个函数存储在一个名为city_functions.py的模块中。创建一个名为test_cities.py的程序,对刚编写的函数进行测试(别忘了,你需要导入模块unittest 以及要测试的函数)。编写一个名test_city_country() 的方法,核实使用类似于’santiago’ 和’chile’ 这样的值来调用前述函数时,得到的字符串是正确的。运行test_cities.py ,确认测试test_city_country() 通过了
  2. 源代码

city_functions.py:

"""A collection of functions for working with cities."""

def city_country(city, country):
	""""Return a string like 'Santiago, Chile'."""
	full_inf = city + ", " + country
	return full_inf.title()

test_citys.py:

import unittest
from city_functions import city_country

class NameTestCase(unittest.TestCase):
	"""Test for 'city_functions.py'."""
	
	def test_city_country(self):
		""""Can it process information like city, country correctly"""
		santiago_chile = city_country('santiago', 'chile')
		self.assertEqual(santiago_chile, 'Santiago, Chile')
		
unittest.main()
  1. 结果截图

在这里插入图片描述

任务2.人口数量

  1. 问题描述
    修改前面的函数,使其包含第三个必不可少的形参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() 通过了。
  2. 源代码及结果截图

a.修改任务1的函数,使其包含第三个必不可少的形参population。

"""A collection of functions for working with cities."""

def city_country(city, country, population):
	""""Return a string like 'Santiago, Chile'."""
	full_inf = city + ", " + country + " - population " + str(int(population))
	return full_inf.title()

b.编写及运行test_cities.py。

import unittest
from city_functions import city_country
class NameTestCase(unittest.TestCase):
    def test_city_country(self):
	""""Can it process information like 'city, country' correctly?"""
        santiago_chile = city_country('santiago', 'chile')
	    self.assertEqual(santiago_chile, 'Santiago, Chile')

unittest.main()

在这里插入图片描述
c.继续修改函数,将形参population 设置为可选的。

"""A collection of functions for working with cities."""

def city_country(city, country, population = ''):
	""""Return a string like 'Santiago, Chile'."""
	if population:
		full_inf = city + ", " + country + " - population " + str(int(population))
	else:
		full_inf = city + ", " + country
	return full_inf.title()

d.再次运行test_cities.py,则通过了测试。
在这里插入图片描述
e.修改函数及编写test_city_country_population() 的测试。
city_functions.py

"""A collection of functions for working with cities."""

def city_country(city, country, population = ''):
	""""Return a string like 'Santiago, Chile'."""
	if population:
		full_inf = city + ", " + country + " - population " + str(int(population))
	else:
		full_inf = city + ", " + country
	return full_inf

test_cities.py

import unittest
from city_functions import city_country

class NameTestCase(unittest.TestCase):
	"""Test for 'city_functions.py'."""
	
	def test_city_country(self):
		""""Can it process information like city, country correctly."""
		santiago_chile = city_country('Santiago', 'Chile')
		self.assertEqual(santiago_chile, 'Santiago, Chile')
	
	def test_city_country_population(self):
		"""Can it process information like 'city, country - population xxx' correctly?"""
		santiago_chile_5000000 = city_country('Santiago', 'Chile', '5000000')
		self.assertEqual(santiago_chile_5000000, 'Santiago, Chile - population 5000000')

unittest.main()

d.最后再次运行test_cities.py,确认测试test_city_country_population() 通过了。
在这里插入图片描述

测试类

任务.雇员

  1. 问题描述
    编写一个名为Employee 的类,其方法__init__() 接受名、姓和年薪,并将它们都存储在属性中。编写一个名为give_raise() 的方法,它默认将年薪增加5000美元,但也能够接受其他的年薪增加量。 为Employee 编写一个测试用例,其中包含两个测试方法:test_give_default_raise() 和test_give_custom _raise() 。使用方法setUp() ,以免在每个测试方法中都创建新的雇员实例。运行这个测试用例,确认两个测试都通过了。
  2. 源代码
    employ_functions.py
class Employee():
	
	def __init__(self, first_name, last_name, annual_salary):
		self.first_name = first_name
		self.last_name = last_name
		self.annual_salary = annual_salary
		
	def give_raise(self, amount=5000):
		self.annual_salary += amount

test_employee.py

import unittest
from employee_functions import Employee

class TestEmployee(unittest.TestCase):
	"""A test for the employee class."""
	
	def setUp(self):
		"""Make an employee to use in the tests."""
		self.aurora = Employee('aurora', 'marry', 35489)
	e
	def test_give_default_raise(self):
		"""Test that a default raise works corrcetly."""
		self.aurora.give_raise()
		self.assertEqual(self.aurora.annual_salary, 40489)
		
	def test_give_custom_raise(self):
		"""Test that a custom raise works correctly."""
		self.aurora.give_raise(24000)
		self.assertEqual(self.aurora.annual_salary, 59489)
		
unittest.main()
  • 结果截图
    在这里插入图片描述

总结

  • 方法setUp() 做了两件事情:创建一个调查对象;创建一个答案列表。
  • 作为初学者,并非必须为你尝试的所有项目编写测试;但参与工作量较大的项目时,你应对自己编写的函数和类的重要行为进行测试。这样 你就能够更加确定自己所做的工作不会破坏项目的其他部分,你就能够随心所欲地改进既有代码了。如果不小心破坏了原来的功能,你马上就会知道,从而能够轻松地修复问 题。相比于等到不满意的用户报告bug后再采取措施,在测试未通过时采取措施要容易得多。
  • 运行测试用例时,每完成一个单元测试,Python都打印一个字符:测试通过时打印一个句点;测试引发错误时打印一个E ;测试导致断言失败时打印一个F 。 这就是运行测试用例时,在输出的第一行中看到的句点和字符数量各不相同的原因。如果测试用例包含很多单元测试,需要运行很长时间,就可通过观察这些结果 来获悉有多少个测试通过了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#模块导入 import pygame,sys from pygame.locals import* #初始化pygame pygame.init() #设置窗口大小,单位是像素 screen = pygame.display.set_mode((500,400)) #设置背景颜色 screen.fill((0,0,0)) #设置窗口标题 pygame.display.set_caption("你好,我的朋友") # 绘制一条线 pygame.draw.rect(screen, (0,0,0), [0,100,70,40]) #加载图片 img = pygame.image.load("panda.jpg") #初始化图片位置 imgx = 0 imgy = 10 #加载和播放音频 sound = pygame.mixer.Sound('Sound_Of_The_Sea.ogg') sound.play() #加载背景音乐 pygame.mixer.music.load('TEST1.mp3') #播放背景音乐,第一个参数为播放的次数(-1表示无限循环),第二个参数是设置播放的起点(单位为秒) pygame.mixer.music.play(-1, 30.0) #导入文字格式 fontt=pygame.font.Font(None,50) #配置文字 tex=fontt.render("It is boring!!!",True,(0,0,128),(0,255,0)) #显示文字及坐标 texr=tex.get_rect() texr.center=(10,250) #初始化方向 dire = "right" #设置循环 while 1: #绘制文字 screen.blit(tex,texr) screen.fill((0,0,0)) screen.blit(img,(imgx,imgy)) if dire == "right": imgx+=5 if imgx == 380: dire = 'down' elif dire == 'down': imgy += 5 if imgy == 300: dire = 'left' elif dire == 'left': imgx -= 5 if imgx == 10: dire = 'up' elif dire == 'up': imgy -= 5 if imgy == 10: dire = 'right' #获取事件 for ss in pygame.event.get(): #判断事件 if ss.type == QUIT: #退出Pygame pygame.quit() #退出系统 sys.exit() #绘制屏幕内容 pygame.display.update() #设置帧率 pygame.time.delay(10)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值