Python Note: Code 12 Test

Code testing: test function is through code to the implementation of the results, and user preset code content, compare!

format:

  import unittest

# Class definitions, to inherit unittest.TestCase class

  class Test_a(unittest.TestCase):

    ......

# Method name must start with test, following unittest.main (), can be automatically executed to test the code will not execute

    def test_first_last_name(self):

# This will return value of the function, and user presets the results were compared, if the same, the test is passed, otherwise an error and prompts test fails 

     self.assertEqual result (code execution or calling function (return value), preset by the user (eg: 'Jimm Green')

  unittest.main( )

 

example:

 # Determine the format of user input, whether the format requirements

the unittest Import 

# define a function that returns all Name:
DEF get_formatted_name (First, Last, middle = ''):
"". "Full the Generate A neatly formatted name" ""
# if there middle value, the middle output, otherwise no output Middle
IF Middle:
FULL_NAME = First + '' + Middle + '' + Last
the else:
FULL_NAME = First + '' + Last
return full_name.title ()

# used to allow the user to enter and first_name last_name, and calling the above function output
Print ( "the Enter 'Q' AT the any Time to quit.")
the while True:
First = INPUT ( "\ nPlease give Me A First name:")
IF First == 'Q':
BREAK
Last = INPUT ( "Please give A Last name Me: ")
IF Last == 'Q':
break

= get_formatted_name formatted_name (First, Last)
Print ( "\ tNeatly formatted name:" + formatted_name + '.')


class NamesTestCase (of unittest.TestCase):
"" "splash test name_function.py" ""

The method defined in #, must the beginning of the test, otherwise unittest.main () function does not automatically perform testing methods in the class!
test_first_last_name DEF (Self):
"" "? Janis Joplin can handle this correctly name it" ""
formatted_name = get_formatted_name ( "Janis", 'joplin')

# This function will first parameter value and the second parameter values are compared, if they are equal then pass the test, or do not pass!
self.assertEqual (formatted_name, 'Janis Joplin')

DEF test_first_middle_last_name (Self):
fromatted_name = get_formatted_name ( "WSS", 'win', 'King'


 

Guess you like

Origin www.cnblogs.com/wssking/p/11545873.html