1. 程式人生 > >測試代碼(測試函數)

測試代碼(測試函數)

bre arm import ase 測試用例 war lin 用例 input

測試函數:

name_function.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/2/21 21:54
# @Author  : solo
# @Site    : 
# @File    : name_function.py
# @Software: PyCharm

def get_formatted_name(first,middle,last,lll):
    full_name = first +   + middle +  + last + lllc
    return full_name.title()

names.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/2/21 21:56
# @Author  : solo
# @Site    : 
# @File    : names.py
# @Software: PyCharm

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("\nplease give me a last name:") if last == q: break get_formatted_name = get_formatted_name(first,last) print("\tNeatly formatteb name: " + get_formatted_name + ".")

執行結果:

Enter q at any time to quit.

please give me a first name:jamea

please give me a last name:kason
    Neatly formatteb name: Jamea Kason.

please give me a first name:q

Process finished with exit code 0

單元測試和測試用例

代碼:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/2/21 22:06
# @Author  : solo
# @Site    : 
# @File    : test_name_function.py
# @Software: PyCharm

import unittest
from name_function import get_formatted_name

class NamesTestCase(unittest.TestCase):
    """測試name_function.py"""
    def test_first_last_name(self):
        """能否正確的處理名字?"""
        formatted_name = get_formatted_name(janis,joplin)
        self.assertEqual(formatted_name,Janis Joplin)

unittest.main()

執行結果:

----------------------------------------------------------------------

Ran 0 tests in 0.000s

OK

測試代碼(測試函數)