Skip to content

Commit a047f62

Browse files
committed
Increased test coverage / mocking of external modules
1 parent 4c2d17e commit a047f62

File tree

2 files changed

+102
-29
lines changed

2 files changed

+102
-29
lines changed

tools/detect_targets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def get_interface_version(mount_point):
113113
if get_module_avail('mbed_lstools'):
114114
try :
115115
mbeds = mbed_lstools.create()
116-
details_txt = mbeds.get_details_txt(mount_point)
116+
details_txt = mbeds.get_details_txt(mount_point)
117117

118118
if 'Interface Version' in details_txt:
119119
return details_txt['Interface Version']

tools/test/detect_targets_test.py

Lines changed: 101 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,59 @@
1616
"""
1717

1818
import unittest
19+
from mock import patch
1920
from tools.detect_targets import get_interface_version
20-
from tools.test_api import get_autodetected_MUTS_list
21+
22+
23+
class MbedLsToolsMock():
24+
25+
def __init__(self, type):
26+
self.interface_test_type = type
27+
28+
def get_details_txt(self, mount_point):
29+
return self.details_txt_types[self.interface_test_type];
30+
31+
# Static details.txt types.
32+
details_txt_types = {
33+
'details_valid_interface_version' : {
34+
'Unique ID': '0226000029164e45002f0012706e0006f301000097969900',
35+
'HIF ID': '97969900',
36+
'Auto Reset': '0',
37+
'Automation allowed': '0',
38+
'Daplink Mode': 'Interface',
39+
'Interface Version': '0240',
40+
'Git SHA': 'c765cbb590f57598756683254ca38b211693ae5e',
41+
'Local Mods': '0',
42+
'USB Interfaces': 'MSD, CDC, HID',
43+
'Interface CRC': '0x26764ebf'
44+
},
45+
'details_valid_version' : {
46+
'Version': '0226',
47+
'Build': 'Aug 24 2015 17:06:30',
48+
'Git Commit SHA': '27a236b9fe39c674a703c5c89655fbd26b8e27e1',
49+
'Git Local mods': 'Yes'
50+
},
51+
'details_missing_interface_version' : {
52+
'Unique ID': '0226000033514e450044500585d4001de981000097969900',
53+
'HIC ID': '97969900',
54+
'Auto Reset': '0',
55+
'Automation allowed': '0',
56+
'Overflow detection': '0',
57+
'Daplink Mode': 'Interface',
58+
'Git SHA': 'b403a07e3696cee1e116d44cbdd64446e056ce38',
59+
'Local Mods': '0',
60+
'USB Interfaces': 'MSD, CDC, HID',
61+
'Interface CRC': '0x4d98bf7e',
62+
'Remount count': '0'
63+
},
64+
'details_invalid_none' : None
65+
}
2166

2267
"""
2368
Tests for detect_targets.py
2469
"""
2570

26-
class DetectTargetsTests(unittest.TestCase):
71+
class DetectTargetsTest(unittest.TestCase):
2772
"""
2873
Test cases for Detect Target functionality
2974
"""
@@ -33,26 +78,9 @@ def setUp(self):
3378
Called before each test case
3479
3580
:return:
36-
"""
37-
# Gather a valid mount point from the host machine
38-
muts = get_autodetected_MUTS_list()
39-
40-
count = 0
41-
42-
for mut in muts.values():
43-
44-
count += 1
45-
self.valid_mount_point = mut['disk']
46-
break
47-
48-
# If no targets are found, there is no way to determine Host OS mount point.
49-
# Function should handle failure gracefully regardless of a mount point being present.
50-
# Therefore it is safe to assume a valid mount point.
51-
if count is 0:
52-
self.valid_mount_point = "D:"
53-
54-
self.invalid_mount_point = "23z/e\n"
81+
"""
5582
self.missing_mount_point = None
83+
self.mount_point = "D:"
5684

5785
def tearDown(self):
5886
"""
@@ -62,22 +90,67 @@ def tearDown(self):
6290
:return:
6391
"""
6492
pass
93+
94+
@patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_valid_interface_version'))
95+
def test_interface_version_valid(self, mbed_lstools_mock):
96+
"""
97+
Test that checks function returns correctly when given a valid Interface Version
98+
99+
:param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock
100+
:return
101+
"""
65102

66-
def test_interface_version_valid_mount_point(self):
103+
interface_version = get_interface_version(self.mount_point)
104+
assert interface_version == '0240'
67105

68-
interface_version = get_interface_version(self.valid_mount_point)
69-
assert len(interface_version) > 0
106+
@patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_valid_version'))
107+
def test_version_valid(self, mbed_lstools_mock):
108+
"""
109+
Test that checks function returns correctly when given a valid Version
110+
111+
:param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock
112+
:return
113+
"""
70114

71-
def test_interface_version_invalid_mount_point(self):
115+
interface_version = get_interface_version(self.mount_point)
116+
assert interface_version == '0226'
72117

73-
interface_version = get_interface_version(self.invalid_mount_point)
118+
@patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_missing_interface_version'))
119+
def test_interface_version_missing_interface_version(self, mbed_lstools_mock):
120+
"""
121+
Test that checks function returns correctly when DETAILS.txt is present
122+
but an interface version is not listed.
123+
124+
:param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock
125+
:return
126+
"""
127+
128+
interface_version = get_interface_version(self.mount_point)
74129
assert interface_version == 'unknown'
75130

76-
def test_interface_version_missing_mount_point(self):
131+
@patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_invalid_none'))
132+
def test_version_none(self, mbed_lstools_mock):
133+
"""
134+
Test that checks function returns correctly when a valid mount point is supplied
135+
but DETAILS.txt is not present.
136+
137+
:param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock
138+
:return
139+
"""
140+
141+
interface_version = get_interface_version(self.mount_point)
142+
assert interface_version == 'unknown'
143+
144+
@patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_invalid_none'))
145+
def test_interface_version_missing_mount_point(self, mbed_lstools_mock):
146+
"""
147+
Test that checks function returns correctly when no moint point is supplied.
77148
149+
:param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock
150+
:return
151+
"""
78152
interface_version = get_interface_version(self.missing_mount_point)
79153
assert interface_version == 'unknown'
80154

81-
82155
if __name__ == '__main__':
83156
unittest.main()

0 commit comments

Comments
 (0)