python用unittest编写测试类

    使用Python模块unittest中的工具来测 试代码。编写测试用例,核实一系列输入都将得到预期的 输出。测试通过了是什么样子,测试未通过又是什么样子, 还将知道测试未通过如何有助于改进代码。这里将学习如何测试函数和类。

name_function.py类代码:

def get_formatted_name(first,last):
#def get_formatted_name(first,midele,last): try:
full_name = first +' ' +last
#full_name = first +' '+midele+' ' +last except Exception as e: print(e) return full_name.title()

test_name_function.py测试类代码:

import unittest

from baike_spider.name_function import get_formatted_name

class NameTestCase(unittest.TestCase):

    def test_first_last_name(self):
        print("############开始执行测试###################")
        try:
            formatted_name = get_formatted_name('cao','qing')
            print(formatted_name)
            self.assertEqual(formatted_name, 'Cao Qing')
        except Exception as e:
            print("发生异常了"+ e)
        
unittest.main

运行的时候我遇到点问题,就是执行没结果,郁闷半天。

正确执行测试类的时候:执行区域鼠标右键 >> run as >>python unit-test 

执行异常:


执行正常:


猜你喜欢

转载自blog.csdn.net/goodlook0123/article/details/80015781
今日推荐