-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch_11.py
186 lines (125 loc) · 5.31 KB
/
ch_11.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# CHAPTER 11
"""Testing your code. """
def get_formatted_name(first_name, last_name):
"""Generate a neatly formatted full name. """
full_name = f"{first_name} {last_name}"
return full_name.title()
# 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("Please give me a last name: ")
if last == "q":
break
formatted_name = get_formatted_name(first, last)
print(f"\n Neatly formatted name: {formatted_name}.")
# A Passing Test
from ctypes import FormatError
import unittest
from urllib import response
# from name_function immport get_formatted_name
class NamesTestCase(unittest.TestCase):
"""Tests for get_formatted_name. """
def test_first_last_name(self):
"""Do names like 'Janis Joplin' work? """
formatted_name = get_formatted_name('janis', 'joplin')
self.assertEqual(formatted_name, 'Janis Joplin')
if __name__ == '__main__': # for some reason, this program causes problems.
unittest.main()
def get_formatted_name(first, middle, last):
"""Generate a neatly formatted full name. """
full_name = f"{first} {middle} {last}"
return full_name
def get_formatted_name(first, last, middle=''):
"""Generate a neatly formatted full name. """
if middle:
full_name = f"{first} {middle} {last}"
else:
full_name = f"{first} {middle} {last}"
return full_name.title()
# Adding a new test
import unittest
class NamesTestCase(unittest.TestCase):
"""Tests for get_formatted_name """
def test_first_last_name(self):
"""Do names like 'Janis Joplin' work? """
formatted_name = get_formatted_name('janis', 'joplin')
self.assertEqual(formatted_name, 'Janis Joplin')
def test_first_last_middle_name(self):
"""Do names like 'Wolfgang Amadeus Mozart' work? """
formatted_name = get_formatted_name(
'wolfgang', 'mozart', 'amadeus')
self.assertEqual(formatted_name, 'Wolfgang Mozart Amadeus')
if __name__ == '__main__':
unittest.main()
# 11-1, 11-2 were not done because of the errors in the terminal when running tests in the file so far.
# Testing a class
# A class to Test
class AnonymousSurvey:
"""Collect anonymous answers to a survey question. """
def __init__(self, question):
"""Store a question, and prepare to store responses. """
self.question = question
self.responses = []
def shou_question(self):
"""Show the survey question. """
print(self.question)
def store_response(self, new_response):
"""Store a single response to the survey. """
self.responses.append(new_response)
def show_results(self):
"""Show all the responses that have been given. """
print("Survey Results:")
for response in self.responses:
print(f"- {response}")
# from survey import AnonymousSurvey
# Define a question, and make a survey
question = 'What language did you first learn to speak? '
my_survey = AnonymousSurvey(question)
# Show a question, and store the responses to the question
my_survey.shou_question()
print("Enter 'q' at any time to quit.\n")
while True:
response = input("Language: ")
if response == 'q':
break
my_survey.store_response(response)
# Show the survey results
print("\nThank you to everyone who took the surver! ")
my_survey.show_results()
# Testing the AnonymousSurvey Class
import unittest
# from survey import AnonymousSurvey
class TestAnonymousSurvey(unittest.TestCase):
"""Test for the class AnonymousSurvery. """
def test_store_single_response(self):
"""Test that a single response is stored properly. """
question = "What language did you first learn to speak? "
my_survey = AnonymousSurvey(question)
my_survey.store_response('English')
self.assertIn('English', my_survey.responses)
# if __name__ == '__main__': # This program still generates errors
# unittest.main()
import unittest
# from survey import AnonymousSurvey
class TestAnonymousSurvey(unittest.TestCase):
"""Tests for the class AnonymousSurvey"""
def test_store_single_response(self):
"""Test that a single response is stored properly. """
question = "What language did you first learn to speak? "
my_survey = AnonymousSurvey(question)
my_survey.store_response('English')
self.assertIn('English', my_survey.responses)
def test_store_three_responses(self):
"""Test that three individual responses are stored properly."""
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)
# if __name__ == '__main__':
# unittest.main()