Skip to content

Commit 2e6df44

Browse files
theotherjimmyadbridge
authored andcommitted
Convert memap tests to pytest style
1 parent de3cc63 commit 2e6df44

File tree

2 files changed

+169
-190
lines changed

2 files changed

+169
-190
lines changed

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ script:
1919
- |
2020
find -name "*.s" | tee BUILD/badasm | sed -e "s/^/Bad Assembler file name found: /" && [ ! -s BUILD/badasm ]
2121
- make -C events/equeue test clean
22-
- PYTHONPATH=. coverage run -m pytest tools/test/config_test/config_test.py tools/test/toolchains/api.py
22+
- PYTHONPATH=. coverage run -m pytest tools/test/config_test/config_test.py tools/test/toolchains/api.py tools/test/memap/memap_test.py
2323
- PYTHONPATH=. coverage run tools/test/build_api/build_api_test.py
2424
- PYTHONPATH=. coverage run tools/test/targets/target_test.py
2525
- coverage run tools/test/pylint.py
26-
- coverage run tools/test/memap/memap_test.py
2726
- coverage run tools/project.py -S
2827
- coverage run tools/build_travis.py
2928
- coverage html

tools/test/memap/memap_test.py

Lines changed: 168 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -15,204 +15,184 @@
1515
limitations under the License.
1616
"""
1717
import sys
18-
import os
18+
from os.path import isfile, join
1919
import json
2020

21-
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", ".."))
22-
sys.path.insert(0, ROOT)
21+
import pytest
2322

24-
import unittest
2523
from tools.memap import MemapParser
2624
from copy import deepcopy
2725

2826
"""
2927
Tests for test_api.py
3028
"""
3129

32-
class MemapParserTests(unittest.TestCase):
30+
@pytest.fixture
31+
def memap_parser():
3332
"""
34-
Test cases for Test Api
33+
Called before each test case
34+
35+
:return:
36+
"""
37+
memap_parser = MemapParser()
38+
39+
memap_parser.modules = {
40+
"mbed-os/targets/TARGET/TARGET_MCUS/api/pinmap.o": {
41+
".text": 1,
42+
".data": 2,
43+
".bss": 3,
44+
".heap": 0,
45+
".stack": 0,
46+
".interrupts_ram":0,
47+
".init":0,
48+
".ARM.extab":0,
49+
".ARM.exidx":0,
50+
".ARM.attributes":0,
51+
".eh_frame":0,
52+
".init_array":0,
53+
".fini_array":0,
54+
".jcr":0,
55+
".stab":0,
56+
".stabstr":0,
57+
".ARM.exidx":0,
58+
".ARM":0,
59+
".interrupts":0,
60+
".flash_config":0,
61+
"unknown":0,
62+
"OUTPUT":0,
63+
},
64+
"[lib]/libc.a/lib_a-printf.o": {
65+
".text": 4,
66+
".data": 5,
67+
".bss": 6,
68+
".heap": 0,
69+
".stack": 0,
70+
".interrupts_ram":0,
71+
".init":0,
72+
".ARM.extab":0,
73+
".ARM.exidx":0,
74+
".ARM.attributes":0,
75+
".eh_frame":0,
76+
".init_array":0,
77+
".fini_array":0,
78+
".jcr":0,
79+
".stab":0,
80+
".stabstr":0,
81+
".ARM.exidx":0,
82+
".ARM":0,
83+
".interrupts":0,
84+
".flash_config":0,
85+
"unknown":0,
86+
"OUTPUT":0,
87+
},
88+
"main.o": {
89+
".text": 7,
90+
".data": 8,
91+
".bss": 0,
92+
".heap": 0,
93+
".stack": 0,
94+
".interrupts_ram":0,
95+
".init":0,
96+
".ARM.extab":0,
97+
".ARM.exidx":0,
98+
".ARM.attributes":0,
99+
".eh_frame":0,
100+
".init_array":0,
101+
".fini_array":0,
102+
".jcr":0,
103+
".stab":0,
104+
".stabstr":0,
105+
".ARM.exidx":0,
106+
".ARM":0,
107+
".interrupts":0,
108+
".flash_config":0,
109+
"unknown":0,
110+
"OUTPUT":0,
111+
},
112+
"test.o": {
113+
".text": 0,
114+
".data": 0,
115+
".bss": 0,
116+
".heap": 0,
117+
".stack": 0,
118+
".interrupts_ram":0,
119+
".init":0,
120+
".ARM.extab":0,
121+
".ARM.exidx":0,
122+
".ARM.attributes":0,
123+
".eh_frame":0,
124+
".init_array":0,
125+
".fini_array":0,
126+
".jcr":0,
127+
".stab":0,
128+
".stabstr":0,
129+
".ARM.exidx":0,
130+
".ARM":0,
131+
".interrupts":0,
132+
".flash_config":0,
133+
"unknown":0,
134+
"OUTPUT":0,
135+
},
136+
}
137+
return memap_parser
138+
139+
140+
def generate_test_helper(memap_parser, format, depth, file_output=None):
141+
"""
142+
Helper that tests that the member variables "modules" is
143+
unchanged after calling "generate_output"
144+
145+
:param output_type: type string that is passed to "generate_output"
146+
:param file_output: path to output file that is passed to "generate_output"
35147
"""
36148

37-
def setUp(self):
38-
"""
39-
Called before each test case
40-
41-
:return:
42-
"""
43-
self.memap_parser = MemapParser()
44-
45-
self.memap_parser.modules = {
46-
"mbed-os/targets/TARGET/TARGET_MCUS/api/pinmap.o": {
47-
".text": 1,
48-
".data": 2,
49-
".bss": 3,
50-
".heap": 0,
51-
".stack": 0,
52-
".interrupts_ram":0,
53-
".init":0,
54-
".ARM.extab":0,
55-
".ARM.exidx":0,
56-
".ARM.attributes":0,
57-
".eh_frame":0,
58-
".init_array":0,
59-
".fini_array":0,
60-
".jcr":0,
61-
".stab":0,
62-
".stabstr":0,
63-
".ARM.exidx":0,
64-
".ARM":0,
65-
".interrupts":0,
66-
".flash_config":0,
67-
"unknown":0,
68-
"OUTPUT":0,
69-
},
70-
"[lib]/libc.a/lib_a-printf.o": {
71-
".text": 4,
72-
".data": 5,
73-
".bss": 6,
74-
".heap": 0,
75-
".stack": 0,
76-
".interrupts_ram":0,
77-
".init":0,
78-
".ARM.extab":0,
79-
".ARM.exidx":0,
80-
".ARM.attributes":0,
81-
".eh_frame":0,
82-
".init_array":0,
83-
".fini_array":0,
84-
".jcr":0,
85-
".stab":0,
86-
".stabstr":0,
87-
".ARM.exidx":0,
88-
".ARM":0,
89-
".interrupts":0,
90-
".flash_config":0,
91-
"unknown":0,
92-
"OUTPUT":0,
93-
},
94-
"main.o": {
95-
".text": 7,
96-
".data": 8,
97-
".bss": 0,
98-
".heap": 0,
99-
".stack": 0,
100-
".interrupts_ram":0,
101-
".init":0,
102-
".ARM.extab":0,
103-
".ARM.exidx":0,
104-
".ARM.attributes":0,
105-
".eh_frame":0,
106-
".init_array":0,
107-
".fini_array":0,
108-
".jcr":0,
109-
".stab":0,
110-
".stabstr":0,
111-
".ARM.exidx":0,
112-
".ARM":0,
113-
".interrupts":0,
114-
".flash_config":0,
115-
"unknown":0,
116-
"OUTPUT":0,
117-
},
118-
"test.o": {
119-
".text": 0,
120-
".data": 0,
121-
".bss": 0,
122-
".heap": 0,
123-
".stack": 0,
124-
".interrupts_ram":0,
125-
".init":0,
126-
".ARM.extab":0,
127-
".ARM.exidx":0,
128-
".ARM.attributes":0,
129-
".eh_frame":0,
130-
".init_array":0,
131-
".fini_array":0,
132-
".jcr":0,
133-
".stab":0,
134-
".stabstr":0,
135-
".ARM.exidx":0,
136-
".ARM":0,
137-
".interrupts":0,
138-
".flash_config":0,
139-
"unknown":0,
140-
"OUTPUT":0,
141-
},
142-
}
143-
144-
def tearDown(self):
145-
"""
146-
Called after each test case
147-
148-
:return:
149-
"""
150-
pass
151-
152-
def generate_test_helper(self, output_type, depth, file_output=None):
153-
"""
154-
Helper that ensures that the member variables "modules" is
155-
unchanged after calling "generate_output"
156-
157-
:param output_type: type string that is passed to "generate_output"
158-
:param file_output: path to output file that is passed to "generate_output"
159-
:return:
160-
"""
161-
162-
old_modules = deepcopy(self.memap_parser.modules)
163-
164-
self.memap_parser.generate_output(output_type, depth, file_output)
165-
166-
self.assertEqual(self.memap_parser.modules, old_modules,
167-
"generate_output modified the 'modules' property")
168-
169-
170-
def test_report_computed(self):
171-
"""
172-
Test ensures the report and summary are computed
173-
174-
:return:
175-
"""
176-
177-
self.memap_parser.generate_output('table', 2)
178-
179-
# Report is created after generating output
180-
self.assertTrue(self.memap_parser.mem_summary)
181-
self.assertTrue(self.memap_parser.mem_report)
182-
183-
def test_generate_output_table(self):
184-
"""
185-
Test ensures that an output of type "table" can be generated correctly
186-
187-
:return:
188-
"""
189-
depth = 2
190-
self.generate_test_helper('table', depth)
191-
192-
def test_generate_output_json(self):
193-
"""
194-
Test ensures that an output of type "json" can be generated correctly
195-
196-
:return:
197-
"""
198-
file_name = '.json_test_output.json'
199-
depth = 2
200-
self.generate_test_helper('json', depth, file_output=file_name)
201-
self.assertTrue(os.path.exists(file_name), "Failed to create json file")
202-
os.remove(file_name)
203-
204-
def test_generate_output_csv_ci(self):
205-
"""
206-
Test ensures that an output of type "csv-ci" can be generated correctly
207-
208-
:return:
209-
"""
210-
file_name = '.csv_ci_test_output.csv'
211-
depth = 2
212-
self.generate_test_helper('csv-ci', depth, file_output=file_name)
213-
self.assertTrue(os.path.exists(file_name), "Failed to create csv-ci file")
214-
os.remove(file_name)
215-
216-
217-
if __name__ == '__main__':
218-
unittest.main()
149+
old_modules = deepcopy(memap_parser.modules)
150+
151+
memap_parser.generate_output(format, depth, file_output=file_output)
152+
153+
assert memap_parser.modules == old_modules,\
154+
"generate_output modified the 'modules' property"
155+
156+
157+
@pytest.mark.parametrize("depth", [1, 2, 20])
158+
def test_report_computed(memap_parser, depth):
159+
"""
160+
Test that a report and summary are computed
161+
"""
162+
163+
memap_parser.generate_output('table', depth)
164+
165+
# Report is created after generating output
166+
assert memap_parser.mem_summary
167+
assert memap_parser.mem_report
168+
169+
170+
@pytest.mark.parametrize("depth", [1, 2, 20])
171+
def test_generate_output_table(memap_parser, depth):
172+
"""
173+
Test that an output of type "table" can be generated correctly
174+
"""
175+
generate_test_helper(memap_parser, 'table', depth)
176+
177+
178+
@pytest.mark.parametrize("depth", [1, 2, 20])
179+
def test_generate_output_json(memap_parser, tmpdir, depth):
180+
"""
181+
Test that an output of type "json" can be generated correctly
182+
"""
183+
file_name = str(tmpdir.join('json_test_output.json').realpath())
184+
generate_test_helper(memap_parser, 'json', depth, file_name)
185+
assert isfile(file_name), "Failed to create json file"
186+
json.load(open(file_name))
187+
188+
189+
@pytest.mark.parametrize("depth", [1, 2, 20])
190+
def test_generate_output_csv_ci(memap_parser, tmpdir, depth):
191+
"""
192+
Test ensures that an output of type "csv-ci" can be generated correctly
193+
194+
:return:
195+
"""
196+
file_name = str(tmpdir.join('.csv_ci_test_output.csv').realpath())
197+
generate_test_helper(memap_parser, 'csv-ci', depth, file_name)
198+
assert isfile(file_name), "Failed to create csv-ci file"

0 commit comments

Comments
 (0)