前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python--从入门到实践--chapter 11 代码测试unittest

python--从入门到实践--chapter 11 代码测试unittest

作者头像
Michael阿明
发布2021-02-20 10:37:23
2050
发布2021-02-20 10:37:23
举报
文章被收录于专栏:Michael阿明学习之路

编写的代码需要测试是否有Bug

1.函数测试

city_functions.py

代码语言:javascript
复制
def city_country(city, country):
    return str(city) + ',' + str(country)

city_country_unittest.py

代码语言:javascript
复制
import unittest	#导入测试模块
from city_functions import city_country as cc
class citytest(unittest.TestCase):
    def test_city_country(self):
        returnValue = cc("Santiago", "Chile")
        self.assertEqual(returnValue, "Santiago,Chile") #运行结果是否和期待值相等

unittest.main() #运行测试

city_functions.py中函数添加一个参数,测试程序不变

代码语言:javascript
复制
def city_country(city, country, population):
    return str(city) + ',' + str(country) + " - " + "population " + str(population)

再对上面2个文件进行修改

代码语言:javascript
复制
def city_country(city, country, population = ''):
    if not population:
        return str(city) + ',' + str(country)
    else:
        return str(city) + ',' + str(country) + " - " + "population " + str(population)
代码语言:javascript
复制
import unittest
from city_functions import city_country as cc
class citytest(unittest.TestCase):
    def test_city_country(self):
        returnValue = cc("Santiago", "Chile")
        self.assertEqual(returnValue, "Santiago,Chile") #运行结果是否和期待值相等
    def test_city_country_population(self):
        returnValue = cc("Santiago", "Chile", 5000000)
        self.assertEqual(returnValue, "Santiago,Chile - population 5000000")
unittest.main() #运行测试

2.类测试

survey.py

代码语言:javascript
复制
class AnonymousSurvey():
    def __init__(self, question):
        self.question = question
        self.responses = []

    def show_question(self):
        print(self.question)

    def store_response(self, new_response):
        self.responses.append(new_response)

    def show_results(self):
        print("Survey results:")
        for response in self.responses:
            print('- ' + response)

test_language_survey.py

代码语言:javascript
复制
import unittest
from survey import AnonymousSurvey

class testLanguageSurvey(unittest.TestCase):
    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_response(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()

3.课后作业

employee.py

代码语言:javascript
复制
class Employee():
    def __init__(self, firstname, lastname, salary):
        self.firstname = firstname
        self.lastname = lastname
        self.salary = salary
    def give_raise(self, raisedmoney = 5000):
        self.salary += raisedmoney

test_employee.py

代码语言:javascript
复制
from employee import Employee
import unittest

class test_employee(unittest.TestCase):
    def setUp(self) -> None:
        self.employee_default = Employee('michael', 'ming', 5000)
        self.employee_custom = Employee('kobe', 'bryant', 5000)
    def test_dafault(self):
        self.employee_default.give_raise()
        self.assertEqual(10000, self.employee_default.salary)
    def test_custom(self):
        self.employee_custom.give_raise(1000)
        self.assertEqual(6000, self.employee_custom.salary)

unittest.main()
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/04/19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.函数测试
  • 2.类测试
  • 3.课后作业
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档