Python基础汇总


本文摘自php中文网,作者巴扎黑,侵删。

测试代码

  编写函数和类时,还可以编写测试函数,通过测试可以确定代码面对各种输入都能正常工作。在程序中添加新代码时,也可以对其进行测试,确定他们不会破坏程序的既有程序。要经常测试模块。

  通过python的模块unittest中的工具来测试代码。将明白测试通过是怎么样的,未通过是什么样的,知道未通过测试怎么来改善代码,知道要为项目编写多少测试。知道怎么测试函数和类。

  在程序运行过程中,总会遇到各种各样的错误。有的错误是程序编写有问题造成的,比如本来应该输出整数结果输出了字符串,这种错误我们通常称之为bug,bug是必须修复的。

测试函数

单元测试和测试用例

  Python中的unittest模块提供代码测试工具,单元测试用来核实函数某个方面没有问题。测试用例是一组单元测试,确保函数在各个方面都没有问题。

  全覆盖式测试用例包含一整套的测试用例,涵盖了各种可能的函数使用方式。

可通过测试

  要为函数编写测试函数,先导入模块unittest和要测试的涵数,在创建一个继承unittest.TestCase的类,并编写一系列方法对函数行为的不同方面进行测试。命名最好带有test。

  name_function.py

def get_formatted_name(first, last):

"""获得全名."""

full_name = first + ' ' + last

return full_name.title()

  names.py

from name_function import get_formatted_name

print("Enter 'q' at any time to quit.")

while True:

first = input("\nPlease give me a first name: ")

if first == 'q':

break

last = input("Please give me a last name: ")

if last == 'q':

break

formatted_name = get_formatted_name(first, last)

print("\tNeatly formatted name: " + formatted_name + '.')

  test_name_function.py

import unittest

from name_function import get_formatted_name

class NamesTestCase(unittest.TestCase):

"""测试name_function.py"""

def test_first_last_name(self):

"""能够正确地处理像Janis Joplin这样的姓名吗?"""

formatted_name = get_formatted_name('janis', 'joplin') self.assertEqual(formatted_name, 'Janis Joplin')

unittest.main()

  unittest最有用的功能之一:一个断言方法,assertEqual断言方法用来核实得到的结果是否和期望的值一样。

不能通过的测试

  测试没有通过,不要修改测试,而应修复导致测试不能通过的代码:检查刚对函数所做的修改,找出导致函数行为不符合预期的修改。

模块中的各种断言方法

  6个常见断言

方法

用途

assertEqual(a, b)

核实a == b

assertNotEqual(a, b)

核实 a != b

assertTrue(x)

核实 x 为True

assertFalse(x)

核实 x 为False

assertIn(item, list)

核实 item在list中

assertNotIn(item, list)

核实 item不在list中

方法setUp()

  unittest.TestCase 类包含方法setUp() ,让我们只需创建一次对象,并可以在每个测试方法中使用它们。

  survey.py

class AnonymousSurvey():

"""收集匿名调查问卷的答案"""

def __init__(self, question):

"""存储一个问题,并为存储答案做准备"""

self.question = question

self.responses = []

def show_question(self):

"""显示调查问卷"""

print(question)

def store_response(self, new_response):

"""存储单份调查答卷"""

self.responses.append(new_response)

def show_results(self):

"""显示收集到的所有答卷"""

print("Survey results:")

for response in responses:

print('- ' + response)

  language_survey.py

from survey import AnonymousSurvey

#定义一个问题,并创建一个表示调查的AnonymousSurvey对象

question = "What language did you first learn to speak?"

my_survey = AnonymousSurvey(question)

#显示问题并存储答案 my_survey.show_question()

print("Enter 'q' at any time to quit.\n")

while True:

response = input("Language: ")

if response == 'q':

break

my_survey.store_response(response)

# 显示调查结果

print("\nThank you to everyone who participated in the survey!")

my_survey.show_results()

  test_survey.py

import unittest

from survey import AnonymousSurvey

class TestAnonymousSurvey(unittest.TestCase):

"""针对AnonymousSurvey类的测试"""

def setUp(self):

"""创建一个调查对象和一组答案,供使用的测试方法使用"""

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):

"""测试单个答案会被妥善地存储""" self.my_survey.store_response(self.responses[0])

self.assertIn(self.responses[0], self.my_survey.responses)

def test_store_three_responses(self):

"""测试三个答案会被妥善地存储"""

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()

  方法setUp() 做了两件事情:创建一个调查对象;创建一个答案列表。存储这两样东西的变量名包含前缀self (即存储在属性中),因此可在这个类的任何 地方使用。这让两个测试方法都更简单,因为它们都不用创建调查对象和答案。

使用int() 来输入数字

  因为input()返回的数据类型是str(字符串),python会把直接输入的数字转换成字符串。str不能直接和整数比较,必须先把str转换成整数。Python提供了int()函数来完成这件事情


height = input("How tall are you, in inches? ")

height = int(height) #转化为整数


求模运算符

  处理数值信息时,求模运算符 (%)是一个很有用的工具,它将两个数相除并返回余数:

>>> 4 % 3

1

>>> 5 % 3

2

>>> 6 % 3

0

>>> 7 % 3

1

如果一个数可被另一个数整除,余数就为0,因此求模运算符将返回0。你可利用这一点来判断一个数是奇数还是偶数。

以上就是Python基础汇总的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

安卓app可以用Python写吗

介绍Python的函数装饰器

Python3.7.2如何安装pil库

Python元组怎么排序

Python print怎么换行

Python数据可视化的四种方法介绍(附示例)

Python压缩包怎么安装

Python 语法串联

Python中的异常处理(代码示例)

Python格式化输出是什么意思

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...