Convert memap tests to pytest style

pull/4984/head
Jimmy Brisson 2017-08-28 13:52:28 -05:00
parent 177fb7933d
commit de9f9dd3a2
2 changed files with 161 additions and 182 deletions

View File

@ -19,11 +19,10 @@ script:
- |
find -name "*.s" | tee BUILD/badasm | sed -e "s/^/Bad Assembler file name found: /" && [ ! -s BUILD/badasm ]
- make -C events/equeue test clean
- PYTHONPATH=. coverage run -m pytest tools/test/config_test/config_test.py tools/test/toolchains/api.py
- PYTHONPATH=. coverage run -m pytest tools/test/config_test/config_test.py tools/test/toolchains/api.py tools/test/memap/memap_test.py
- PYTHONPATH=. coverage run tools/test/build_api/build_api_test.py
- PYTHONPATH=. coverage run tools/test/targets/target_test.py
- coverage run tools/test/pylint.py
- coverage run tools/test/memap/memap_test.py
- coverage run tools/project.py -S
- coverage run tools/build_travis.py
- coverage html

View File

@ -15,13 +15,11 @@ See the License for the specific language governing permissions and
limitations under the License.
"""
import sys
import os
from os.path import isfile, join
import json
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", ".."))
sys.path.insert(0, ROOT)
import pytest
import unittest
from tools.memap import MemapParser
from copy import deepcopy
@ -29,190 +27,172 @@ from copy import deepcopy
Tests for test_api.py
"""
class MemapParserTests(unittest.TestCase):
@pytest.fixture
def memap_parser():
"""
Test cases for Test Api
Called before each test case
:return:
"""
memap_parser = MemapParser()
memap_parser.modules = {
"mbed-os/targets/TARGET/TARGET_MCUS/api/pinmap.o": {
".text": 1,
".data": 2,
".bss": 3,
".heap": 0,
".stack": 0,
".interrupts_ram":0,
".init":0,
".ARM.extab":0,
".ARM.exidx":0,
".ARM.attributes":0,
".eh_frame":0,
".init_array":0,
".fini_array":0,
".jcr":0,
".stab":0,
".stabstr":0,
".ARM.exidx":0,
".ARM":0,
".interrupts":0,
".flash_config":0,
"unknown":0,
"OUTPUT":0,
},
"[lib]/libc.a/lib_a-printf.o": {
".text": 4,
".data": 5,
".bss": 6,
".heap": 0,
".stack": 0,
".interrupts_ram":0,
".init":0,
".ARM.extab":0,
".ARM.exidx":0,
".ARM.attributes":0,
".eh_frame":0,
".init_array":0,
".fini_array":0,
".jcr":0,
".stab":0,
".stabstr":0,
".ARM.exidx":0,
".ARM":0,
".interrupts":0,
".flash_config":0,
"unknown":0,
"OUTPUT":0,
},
"main.o": {
".text": 7,
".data": 8,
".bss": 0,
".heap": 0,
".stack": 0,
".interrupts_ram":0,
".init":0,
".ARM.extab":0,
".ARM.exidx":0,
".ARM.attributes":0,
".eh_frame":0,
".init_array":0,
".fini_array":0,
".jcr":0,
".stab":0,
".stabstr":0,
".ARM.exidx":0,
".ARM":0,
".interrupts":0,
".flash_config":0,
"unknown":0,
"OUTPUT":0,
},
"test.o": {
".text": 0,
".data": 0,
".bss": 0,
".heap": 0,
".stack": 0,
".interrupts_ram":0,
".init":0,
".ARM.extab":0,
".ARM.exidx":0,
".ARM.attributes":0,
".eh_frame":0,
".init_array":0,
".fini_array":0,
".jcr":0,
".stab":0,
".stabstr":0,
".ARM.exidx":0,
".ARM":0,
".interrupts":0,
".flash_config":0,
"unknown":0,
"OUTPUT":0,
},
}
return memap_parser
def generate_test_helper(memap_parser, format, depth, file_output=None):
"""
Helper that tests that the member variables "modules" is
unchanged after calling "generate_output"
:param output_type: type string that is passed to "generate_output"
:param file_output: path to output file that is passed to "generate_output"
"""
def setUp(self):
"""
Called before each test case
old_modules = deepcopy(memap_parser.modules)
:return:
"""
self.memap_parser = MemapParser()
memap_parser.generate_output(format, depth, file_output=file_output)
self.memap_parser.modules = {
"mbed-os/targets/TARGET/TARGET_MCUS/api/pinmap.o": {
".text": 1,
".data": 2,
".bss": 3,
".heap": 0,
".stack": 0,
".interrupts_ram":0,
".init":0,
".ARM.extab":0,
".ARM.exidx":0,
".ARM.attributes":0,
".eh_frame":0,
".init_array":0,
".fini_array":0,
".jcr":0,
".stab":0,
".stabstr":0,
".ARM.exidx":0,
".ARM":0,
".interrupts":0,
".flash_config":0,
"unknown":0,
"OUTPUT":0,
},
"[lib]/libc.a/lib_a-printf.o": {
".text": 4,
".data": 5,
".bss": 6,
".heap": 0,
".stack": 0,
".interrupts_ram":0,
".init":0,
".ARM.extab":0,
".ARM.exidx":0,
".ARM.attributes":0,
".eh_frame":0,
".init_array":0,
".fini_array":0,
".jcr":0,
".stab":0,
".stabstr":0,
".ARM.exidx":0,
".ARM":0,
".interrupts":0,
".flash_config":0,
"unknown":0,
"OUTPUT":0,
},
"main.o": {
".text": 7,
".data": 8,
".bss": 0,
".heap": 0,
".stack": 0,
".interrupts_ram":0,
".init":0,
".ARM.extab":0,
".ARM.exidx":0,
".ARM.attributes":0,
".eh_frame":0,
".init_array":0,
".fini_array":0,
".jcr":0,
".stab":0,
".stabstr":0,
".ARM.exidx":0,
".ARM":0,
".interrupts":0,
".flash_config":0,
"unknown":0,
"OUTPUT":0,
},
"test.o": {
".text": 0,
".data": 0,
".bss": 0,
".heap": 0,
".stack": 0,
".interrupts_ram":0,
".init":0,
".ARM.extab":0,
".ARM.exidx":0,
".ARM.attributes":0,
".eh_frame":0,
".init_array":0,
".fini_array":0,
".jcr":0,
".stab":0,
".stabstr":0,
".ARM.exidx":0,
".ARM":0,
".interrupts":0,
".flash_config":0,
"unknown":0,
"OUTPUT":0,
},
}
def tearDown(self):
"""
Called after each test case
:return:
"""
pass
def generate_test_helper(self, output_type, depth, file_output=None):
"""
Helper that ensures that the member variables "modules" is
unchanged after calling "generate_output"
:param output_type: type string that is passed to "generate_output"
:param file_output: path to output file that is passed to "generate_output"
:return:
"""
old_modules = deepcopy(self.memap_parser.modules)
self.memap_parser.generate_output(output_type, depth, file_output)
self.assertEqual(self.memap_parser.modules, old_modules,
"generate_output modified the 'modules' property")
assert memap_parser.modules == old_modules,\
"generate_output modified the 'modules' property"
def test_report_computed(self):
"""
Test ensures the report and summary are computed
@pytest.mark.parametrize("depth", [1, 2, 20])
def test_report_computed(memap_parser, depth):
"""
Test that a report and summary are computed
"""
:return:
"""
memap_parser.generate_output('table', depth)
self.memap_parser.generate_output('table', 2)
# Report is created after generating output
self.assertTrue(self.memap_parser.mem_summary)
self.assertTrue(self.memap_parser.mem_report)
def test_generate_output_table(self):
"""
Test ensures that an output of type "table" can be generated correctly
:return:
"""
depth = 2
self.generate_test_helper('table', depth)
def test_generate_output_json(self):
"""
Test ensures that an output of type "json" can be generated correctly
:return:
"""
file_name = '.json_test_output.json'
depth = 2
self.generate_test_helper('json', depth, file_output=file_name)
self.assertTrue(os.path.exists(file_name), "Failed to create json file")
os.remove(file_name)
def test_generate_output_csv_ci(self):
"""
Test ensures that an output of type "csv-ci" can be generated correctly
:return:
"""
file_name = '.csv_ci_test_output.csv'
depth = 2
self.generate_test_helper('csv-ci', depth, file_output=file_name)
self.assertTrue(os.path.exists(file_name), "Failed to create csv-ci file")
os.remove(file_name)
# Report is created after generating output
assert memap_parser.mem_summary
assert memap_parser.mem_report
if __name__ == '__main__':
unittest.main()
@pytest.mark.parametrize("depth", [1, 2, 20])
def test_generate_output_table(memap_parser, depth):
"""
Test that an output of type "table" can be generated correctly
"""
generate_test_helper(memap_parser, 'table', depth)
@pytest.mark.parametrize("depth", [1, 2, 20])
def test_generate_output_json(memap_parser, tmpdir, depth):
"""
Test that an output of type "json" can be generated correctly
"""
file_name = str(tmpdir.join('json_test_output.json').realpath())
generate_test_helper(memap_parser, 'json', depth, file_name)
assert isfile(file_name), "Failed to create json file"
json.load(open(file_name))
@pytest.mark.parametrize("depth", [1, 2, 20])
def test_generate_output_csv_ci(memap_parser, tmpdir, depth):
"""
Test ensures that an output of type "csv-ci" can be generated correctly
:return:
"""
file_name = str(tmpdir.join('.csv_ci_test_output.csv').realpath())
generate_test_helper(memap_parser, 'csv-ci', depth, file_name)
assert isfile(file_name), "Failed to create csv-ci file"