Unit test module python

1, function test
import unittest
from name_function import get_formatted_name
class NamesTestCase(unittest.TestCase):
"""测试name_function.py"""
def test_first_last_name(self):
"" "Able to correctly handle such names as Janis Joplin do?" ""
formatted_name = get_formatted_name('janis', 'joplin')
self.assertEqual(formatted_name, 'Janis Joplin')
 
unittest.main()
 
2, affirm
assertEqual(a, b) Verify a == b
assertNotEqual(a, b) Verify a! = B
assertTrue(x) Verification of x is True
assertFalse(x) Verification of x is False
assertIn(item , list ) Verification item in the list
assertNotIn(item , list ) Verify the item is not in the list
 
 
3, class test
import unittest
from survey import AnonymousSurvey
class TestAnonymousSurvey(unittest.TestCase):
"" "AnonymousSurvey class test for the" ""
def test_store_single_response(self):
"" "Testing a single answer will be properly stored." ""
--snip--
def test_store_three_responses(self):
"" "Test three answers are properly stored." ""
question = "What language did you first learn to speak?"
my_survey = AnonymousSurvey(question)
responses = ['English', 'Spanish', 'Mandarin']
for response in responses:
my_survey.store_response(response)
for response in responses:
self.assertIn(response, my_survey.responses)
 
unittest.main()
 
4, setup creates an instance
import unittest
from survey import AnonymousSurvey
class TestAnonymousSurvey(unittest.TestCase):
"" "AnonymousSurvey class test for the" ""
def setUp(self):
"""
Create a survey and a set of answers, using the test methods for use
"""
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):
"" "Testing a single answer will be properly stored." ""
self.my_survey.store_response(self.responses[0])
self.assertIn(self.responses[0], self.my_survey.responses)
def test_store_three_responses(self):
"" "Test three answers are properly stored." ""
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()

Guess you like

Origin www.cnblogs.com/chqworkhome/p/10954636.html