/
contact_test.py
executable file
·151 lines (119 loc) · 4.69 KB
/
contact_test.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
#!/usr/bin/env python
# Importing the unittest module
import unittest
# Importing Contact class
from contacts import Contact
# Importing pyperclip
import pyperclip
class TestContact(unittest.TestCase):
"""
We define the TestContact sub-class that defines our test for the
Contact class.
Args:
unittest.TestCase
We inherit the TestCase class that has its own predefined properties and method that will help in creating test cases.
"""
# Setup - Object creation/instantiation
def setUp(self):
"""
Set up method to run before each test case.
We create our contact object instance that we will use to conduct to test different aspects of our Contact class.
"""
# Create an object instance new_contact of class Contact
self.new_contact = Contact("James", "Bond", "007007007", "bond@mi6.com")
# Testing proper instantiation of object.
def test_proper_object_creation(self):
"""
test_init test case is to test if our object instance has been created properly.
"""
self.assertEqual(self.new_contact.first_name, "James")
self.assertEqual(self.new_contact.last_name, "Bond")
self.assertEqual(self.new_contact.phone_number, "007007007")
self.assertEqual(self.new_contact.email, "bond@mi6.com")
# Testing saving of our object
def test_save_contact(self):
"""
test_save_contact case is to test if the contact object is
being save into the contact list that we will create.
"""
# save_contact method which we will define in our Contact class.
# It should basically add a Contact instance to the Contact list
self.new_contact.save_contact()
# We check if your contact_list length is now 1
self.assertEqual(len(Contact.contact_list), 1);
# Function that clears our array following each test.
def tearDown(self):
Contact.contact_list = []
# Test saving multiple contact.
def test_save_multiple_contacts(self):
# Saving our setUp contact
self.new_contact.save_contact()
# Adding another contact
self.added_contact = Contact("Ethan", "Hunt", "0786474", "ethan@imf.com")
# Saving our new contact
self.added_contact.save_contact()
# Checking to see if the length of our array has increased.
self.assertEqual(len(Contact.contact_list), 2)
# Test deleting of a saved contact.
def test_delete_contact(self):
#Saving our setUp contact
self.new_contact.save_contact()
# Adding another contact
self.added_contact = Contact("Ethan", "Hunt", "0786474", "ethan@imf.com")
# Saving our new contact
self.added_contact.save_contact()
# Calling our delete_contact function
self.new_contact.delete_contact()
# Checking to see if the length of our array has reduced
self.assertEqual(len(Contact.contact_list), 1)
def test_find_contact_by_number(self):
"""
Test find_contact by number method that is meant to return a
contact when given a number.
"""
self.new_contact.save_contact()
self.added_contact = Contact("Ethan", "Hunt", "0786474", "ethan@imf.com")
self.added_contact.save_contact()
found_contact = Contact.find_by_number("0786474")
"""
Since we know that add_contact number is 0786474 with email
ethan@imf.com, found contact method should return the same email.
"""
self.assertEqual(found_contact.email, self.added_contact.email)
def test_contact_exists(self):
"""
Tests if the contact_exists will return a boolean depending on
whether or not a contact is found when a number is passed.
"""
self.new_contact.save_contact()
self.added_contact = Contact("Ethan", "Hunt", "0786474", "ethan@imf.com")
self.added_contact.save_contact()
existing_contact = Contact.contact_exists("007007007")
"""
Here we use the assertTrue method for a change. that should return a boolean
"""
self.assertTrue(existing_contact)
def test_display_contacts(self):
"""
Test if our display_contact method will return all our contacts
"""
self.assertTrue(Contact.display_contacts() == Contact.contact_list)
def test_copy_email(self):
"""
Testing to see if our copying function is able to copy email address from a found contact.
"""
self.new_contact.save_contact()
Contact.copy_email("007007007")
"""
pyperclip.paste returns whatever is copied on the machine's clipboard at that time.
"""
self.assertEqual(self.new_contact.email, pyperclip.paste())
if __name__ == '__main__':
"""
We are confirming that unittest.main should be run only if this
(contact_test.py) is being run.
"""
unittest.main()
"""
Above is the method that executes all the tests.
"""