Some aspect of cell function test used to verify there is no problem, the test case is a set of unit tests, unit tests to verify the behavior of these functions together under all circumstances meet the requirements
Unittest module provides code testing tools
Test function
Class for the test class must inherit unittest.TestCase
One of the most useful features unittest class is: a assertion method. Assertion methods for verifying whether the results obtained are consistent with the desired result
name_function.py
def get_formatted_name(first, last):
full_name = first + ' ' + last
return full_name.title()
test.py
import unittest
from name_function import get_formatted_name
class NamesTestCase(unittest.TestCase):
def test_first_last_name(self):
formatted_name = get_formatted_name('janis', 'joplin')
self.assertEqual(formatted_name, 'Janis Joplin')
# Unittest.main () using pycharm not need to add the phrase
Test category
unittest.TestCase class contains methods setUp (), you can let us just need to create an object used in the test once and use them in each test method. If included in the setUp TestCase (), Python will
To run it, and then run each method starts with test_. So that each test method can be used in the object method to setUp (created) in the.
The following is a routine test class book:
survey.py
class AnonymousSurvey():
def __init__(self, question):
self.question = question
self.responses = []
def store_response(self, new_response):
self.responses.append(new_response)
def show_results(self):
print("Survey results: ")
for response in self.responses:
print('- ' + response)
test_survey.py
import unittest
from survey import AnonymousSurvey
class TestAnonymousSurvey(unittest.TestCase):
def setUp(self):
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):
self.my_survey.store_response(self.responses[0])
self.assertIn('English',self.my_survey.responses)
def test_store_three_responses(self):
for response in self.responses:
self.my_survey.store_response(response)
for response in self.responses:
self.assertIn(response,self.my_survey.responses)
Some aspect of cell function test used to verify there is no problem, the test case is a set of unit tests, unit tests to verify the behavior of these functions together under all circumstances meet the requirements
Unittest module provides code testing tools
Test function
Class for the test class must inherit unittest.TestCase
One of the most useful features unittest class is: a assertion method. Assertion methods for verifying whether the results obtained are consistent with the desired result
name_function.py
def get_formatted_name(first, last):
full_name = first + ' ' + last
return full_name.title()
test.py
import unittest
from name_function import get_formatted_name
class NamesTestCase(unittest.TestCase):
def test_first_last_name(self):
formatted_name = get_formatted_name('janis', 'joplin')
self.assertEqual(formatted_name, 'Janis Joplin')
# Unittest.main () using pycharm not need to add the phrase
Test category
unittest.TestCase class contains methods setUp (), you can let us just need to create an object used in the test once and use them in each test method. If included in the setUp TestCase (), Python will
To run it, and then run each method starts with test_. So that each test method can be used in the object method to setUp (created) in the.
The following is a routine test class book:
survey.py
class AnonymousSurvey():
def __init__(self, question):
self.question = question
self.responses = []
def store_response(self, new_response):
self.responses.append(new_response)
def show_results(self):
print("Survey results: ")
for response in self.responses:
print('- ' + response)
test_survey.py
import unittest
from survey import AnonymousSurvey
class TestAnonymousSurvey(unittest.TestCase):
def setUp(self):
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):
self.my_survey.store_response(self.responses[0])
self.assertIn('English',self.my_survey.responses)
def test_store_three_responses(self):
for response in self.responses:
self.my_survey.store_response(response)
for response in self.responses:
self.assertIn(response,self.my_survey.responses)