Home>Article>Backend Development> Summary of Python basics
When writing functions and classes, you can also write test functions. Through testing, you can confirm that the code can work normally in the face of various inputs. As you add new code to your program, you can also test it to make sure that they don't break the program's existing routines. Test modules frequently.
Test the code through the tools in the python module unittest. You will understand what it looks like to pass a test, what it looks like to fail, know how to improve the code if you know that the test fails, and know how many tests to write for the project. Know how to test functions and classes.
During the running of the program, various errors will always be encountered. Some errors are caused by problems in program writing. For example, a string should be output instead of an integer. This kind of error is usually called a bug, and bugs must be fixed.
The unittest module in Python provides code testing tools,Unit testis used to verify certain aspects of the function no problem.Test caseis a set of unit tests to ensure that the function has no problems in all aspects.
Full coverage test cases include a complete set of test cases, covering various possible ways of using functions.
To write a test function for a function, first import the module unittest and the function to be tested, then create a class that inherits unittest.TestCase, and write a series of methods for the function Different aspects of behavior are tested. It is best to name it with test.
name_function.py
##def get_formatted_name(first, last):""" Get the full name."""full_name = first + ' ' + lastreturn full_name.title() |
names.py
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 + '.') |
from name_function import get_formatted_nameclass NamesTestCase(unittest.TestCase ):"""Test name_function.py"""def test_first_last_name(self): ? """formatted_name = get_formatted_name('janis', 'joplin') self.assertEqual(formatted_name, 'Janis Joplin')unittest.main()One of the most useful functions of unittest: an assertion method. The assertEqual assertion method is used to verify whether the result obtained is the same as the expected value. |
##Methods
##assertEqual(a, b) | |||
assertNotEqual(a, b) | |||
assertTrue(x) | |||
##assertFalse(x) | Verify that x is False | ||
assertIn(item, list) | Verify that item is in In list | ||
assertNotIn(item, list) | Verify that item is not in list | ||
Method setUp()The unittest.TestCase class contains the method setUp(), which allows us to create objects only once and use them in every test method. survey.py
test_survey.py
height = int(height) #Convert to integer ##Modulo operator
>>> 6 % 30>>> 7 % 3 1 If one number is divisible by another number, the remainder is 0, so the modulo operator will return 0. You can use this to determine whether a number is odd or even. |
The above is the detailed content of Summary of Python basics. For more information, please follow other related articles on the PHP Chinese website!