Skip to content

Commit 364111b

Browse files
mdmedleyageitgey
authored andcommitted
Refactor Tests (ageitgey#8)
* Refactor Tests
1 parent a307d5a commit 364111b

File tree

1 file changed

+44
-36
lines changed

1 file changed

+44
-36
lines changed

tests/test_face_recognition.py

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,65 +20,73 @@
2020
class Test_face_recognition(unittest.TestCase):
2121

2222
def test_load_image_file(self):
23-
img = api.load_image_file(os.path.join(os.path.dirname(__file__), "test_images", "obama.jpg"))
24-
assert img.shape == (1137, 910, 3)
23+
img = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'obama.jpg'))
24+
self.assertEqual(img.shape, (1137, 910, 3))
2525

2626
def test_load_image_file_32bit(self):
27-
img = api.load_image_file(os.path.join(os.path.dirname(__file__), "test_images", "32bit.png"))
28-
assert img.shape == (1200, 626, 3)
27+
img = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', '32bit.png'))
28+
self.assertEqual(img.shape, (1200, 626, 3))
2929

3030
def test_raw_face_locations(self):
31-
img = api.load_image_file(os.path.join(os.path.dirname(__file__), "test_images", "obama.jpg"))
31+
img = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'obama.jpg'))
3232
detected_faces = api._raw_face_locations(img)
3333

34-
assert len(detected_faces) == 1
35-
assert detected_faces[0].top() == 142
36-
assert detected_faces[0].bottom() == 409
34+
self.assertEqual(len(detected_faces), 1)
35+
self.assertEqual(detected_faces[0].top(), 142)
36+
self.assertEqual(detected_faces[0].bottom(), 409)
3737

3838
def test_raw_face_locations_32bit_image(self):
39-
img = api.load_image_file(os.path.join(os.path.dirname(__file__), "test_images", "32bit.png"))
39+
img = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', '32bit.png'))
4040
detected_faces = api._raw_face_locations(img)
4141

42-
assert len(detected_faces) == 1
43-
assert detected_faces[0].top() == 290
44-
assert detected_faces[0].bottom() == 558
42+
self.assertEqual(len(detected_faces), 1)
43+
self.assertEqual(detected_faces[0].top(), 290)
44+
self.assertEqual(detected_faces[0].bottom(), 558)
4545

4646
def test_face_locations(self):
47-
img = api.load_image_file(os.path.join(os.path.dirname(__file__), "test_images", "obama.jpg"))
47+
img = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'obama.jpg'))
4848
detected_faces = api.face_locations(img)
4949

50-
assert len(detected_faces) == 1
51-
assert detected_faces[0] == (142, 617, 409, 349)
50+
self.assertEqual(len(detected_faces), 1)
51+
self.assertEqual(detected_faces[0], (142, 617, 409, 349))
5252

5353
def test_raw_face_landmarks(self):
54-
img = api.load_image_file(os.path.join(os.path.dirname(__file__), "test_images", "obama.jpg"))
54+
img = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'obama.jpg'))
5555
face_landmarks = api._raw_face_landmarks(img)
5656
example_landmark = face_landmarks[0].parts()[10]
5757

58-
assert len(face_landmarks) == 1
59-
assert face_landmarks[0].num_parts == 68
60-
assert (example_landmark.x, example_landmark.y) == (552, 399)
58+
self.assertEqual(len(face_landmarks), 1)
59+
self.assertEqual(face_landmarks[0].num_parts, 68)
60+
self.assertEqual((example_landmark.x, example_landmark.y), (552, 399))
6161

6262
def test_face_landmarks(self):
63-
img = api.load_image_file(os.path.join(os.path.dirname(__file__), "test_images", "obama.jpg"))
63+
img = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'obama.jpg'))
6464
face_landmarks = api.face_landmarks(img)
6565

66-
assert set(face_landmarks[0].keys()) == set(['chin', 'left_eyebrow', 'right_eyebrow', 'nose_bridge', 'nose_tip', 'left_eye', 'right_eye', 'top_lip', 'bottom_lip'])
67-
assert face_landmarks[0]['chin'] == [(369, 220), (372, 254), (378, 289), (384, 322), (395, 353), (414, 382), (437, 407), (464, 424), (495, 428), (527, 420), (552, 399), (576, 372), (594, 344), (604, 314)]
66+
self.assertEqual(
67+
set(face_landmarks[0].keys()),
68+
set(['chin', 'left_eyebrow', 'right_eyebrow', 'nose_bridge',
69+
'nose_tip', 'left_eye', 'right_eye', 'top_lip',
70+
'bottom_lip']))
71+
self.assertEqual(
72+
face_landmarks[0]['chin'],
73+
[(369, 220), (372, 254), (378, 289), (384, 322), (395, 353),
74+
(414, 382), (437, 407), (464, 424), (495, 428), (527, 420),
75+
(552, 399), (576, 372), (594, 344), (604, 314)])
6876

6977
def test_face_encodings(self):
70-
img = api.load_image_file(os.path.join(os.path.dirname(__file__), "test_images", "obama.jpg"))
78+
img = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'obama.jpg'))
7179
encodings = api.face_encodings(img)
7280

73-
assert len(encodings) == 1
74-
assert len(encodings[0]) == 128
81+
self.assertEqual(len(encodings), 1)
82+
self.assertEqual(len(encodings[0]), 128)
7583

7684
def test_compare_faces(self):
77-
img_a1 = api.load_image_file(os.path.join(os.path.dirname(__file__), "test_images", "obama.jpg"))
78-
img_a2 = api.load_image_file(os.path.join(os.path.dirname(__file__), "test_images", "obama2.jpg"))
79-
img_a3 = api.load_image_file(os.path.join(os.path.dirname(__file__), "test_images", "obama3.jpg"))
85+
img_a1 = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'obama.jpg'))
86+
img_a2 = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'obama2.jpg'))
87+
img_a3 = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'obama3.jpg'))
8088

81-
img_b1 = api.load_image_file(os.path.join(os.path.dirname(__file__), "test_images", "biden.jpg"))
89+
img_b1 = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'biden.jpg'))
8290

8391
face_encoding_a1 = api.face_encodings(img_a1)[0]
8492
face_encoding_a2 = api.face_encodings(img_a2)[0]
@@ -88,16 +96,16 @@ def test_compare_faces(self):
8896
faces_to_compare = [
8997
face_encoding_a2,
9098
face_encoding_a3,
91-
face_encoding_b1
92-
]
99+
face_encoding_b1]
93100

94101
match_results = api.compare_faces(faces_to_compare, face_encoding_a1)
95-
assert match_results[0] == True
96-
assert match_results[1] == True
97-
assert match_results[2] == False
102+
self.assertIs(match_results[0], True)
103+
self.assertIs(match_results[1], True)
104+
self.assertIs(match_results[2], False)
98105

99106
def test_command_line_interface(self):
107+
target_string = '--help Show this message and exit.'
100108
runner = CliRunner()
101109
help_result = runner.invoke(cli.main, ['--help'])
102-
assert help_result.exit_code == 0
103-
assert '--help Show this message and exit.' in help_result.output
110+
self.assertEqual(help_result.exit_code, 0)
111+
self.assertTrue(target_string in help_result.output)

0 commit comments

Comments
 (0)