2019-03-05 17:09:12 +00:00
|
|
|
#
|
|
|
|
|
# Copyright 2019 Ettus Research, a National Instruments Brand
|
|
|
|
|
#
|
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
#
|
|
|
|
|
"""
|
|
|
|
|
USRP MPM Python Unit testing framework
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
|
import sys
|
2020-01-22 10:54:22 +00:00
|
|
|
import argparse
|
2019-03-05 17:09:12 +00:00
|
|
|
from sys_utils_tests import TestNet
|
2020-03-03 19:00:20 +00:00
|
|
|
from mpm_utils_tests import TestMpmUtils
|
2019-12-05 14:43:06 +00:00
|
|
|
from eeprom_tests import TestEeprom
|
2021-06-04 06:27:50 +00:00
|
|
|
from usrp_mpm import __simulated__
|
2019-03-05 17:09:12 +00:00
|
|
|
|
2020-01-22 10:54:22 +00:00
|
|
|
import importlib.util
|
|
|
|
|
if importlib.util.find_spec("xmlrunner"):
|
|
|
|
|
from xmlrunner import XMLTestRunner
|
|
|
|
|
|
2019-03-05 17:09:12 +00:00
|
|
|
TESTS = {
|
2020-03-03 19:00:20 +00:00
|
|
|
'__all__': {
|
|
|
|
|
TestNet,
|
|
|
|
|
TestMpmUtils,
|
2019-12-05 14:43:06 +00:00
|
|
|
TestEeprom,
|
2020-03-03 19:00:20 +00:00
|
|
|
},
|
2019-03-05 17:09:12 +00:00
|
|
|
'n3xx': set(),
|
2021-06-04 06:27:50 +00:00
|
|
|
'x4xx': set()
|
2019-03-05 17:09:12 +00:00
|
|
|
}
|
|
|
|
|
|
2021-06-04 06:27:50 +00:00
|
|
|
if not __simulated__:
|
|
|
|
|
from components_tests import TestZynqComponents
|
|
|
|
|
TESTS['x4xx'].update({
|
|
|
|
|
TestZynqComponents
|
|
|
|
|
})
|
|
|
|
|
|
2020-01-22 10:54:22 +00:00
|
|
|
def parse_args():
|
|
|
|
|
"""Parse arguments when running this as a script"""
|
|
|
|
|
parser_help = 'Run MPM Python unittests'
|
|
|
|
|
parser = argparse.ArgumentParser(description=parser_help)
|
|
|
|
|
parser.add_argument('-x', '--xml', dest='xml', action='store_true', default=False,
|
|
|
|
|
help='Generate XML report (only if module xmlrunner is available)')
|
|
|
|
|
parser.add_argument('device_name', help="the device name for device specific tests",
|
|
|
|
|
default='', nargs='?')
|
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
2019-03-05 17:09:12 +00:00
|
|
|
def get_test_suite(device_name=''):
|
|
|
|
|
"""
|
|
|
|
|
Gets a test suite (collection of test cases) which is relevant for
|
|
|
|
|
the specified device.
|
|
|
|
|
"""
|
|
|
|
|
# A collection of test suites, generated by test loaders, which will
|
|
|
|
|
# be later combined
|
|
|
|
|
test_suite_list = []
|
|
|
|
|
test_loader = unittest.TestLoader()
|
|
|
|
|
|
|
|
|
|
# Combine generic and device specific tests
|
|
|
|
|
test_cases = TESTS.get('__all__') | TESTS.get(device_name, set())
|
|
|
|
|
for case in test_cases:
|
|
|
|
|
new_suite = test_loader.loadTestsFromTestCase(case)
|
|
|
|
|
for test in new_suite:
|
|
|
|
|
# Set up test case class for a specific device.
|
|
|
|
|
# Each test uses a different test case instance.
|
|
|
|
|
if (hasattr(test, 'set_device_name')) and (device_name != ''):
|
|
|
|
|
test.set_device_name(device_name)
|
|
|
|
|
test_suite_list.append(new_suite)
|
|
|
|
|
|
|
|
|
|
# Individual test suites are combined into a master test suite
|
|
|
|
|
test_suite = unittest.TestSuite(test_suite_list)
|
|
|
|
|
return test_suite
|
|
|
|
|
|
2020-01-22 10:54:22 +00:00
|
|
|
def run_tests(device_name='', use_xmlrunner=False):
|
2019-03-05 17:09:12 +00:00
|
|
|
"""
|
|
|
|
|
Executes the unit tests specified by the test suite.
|
|
|
|
|
This should be called from CMake.
|
|
|
|
|
"""
|
|
|
|
|
test_result = unittest.TestResult()
|
2020-01-22 10:54:22 +00:00
|
|
|
if use_xmlrunner and 'XMLTestRunner' in globals():
|
|
|
|
|
test_runner = XMLTestRunner(verbosity=2)
|
|
|
|
|
else:
|
|
|
|
|
test_runner = unittest.TextTestRunner(verbosity=2)
|
2019-03-05 17:09:12 +00:00
|
|
|
test_result = test_runner.run(get_test_suite(device_name))
|
|
|
|
|
return test_result
|
|
|
|
|
|
|
|
|
|
def main():
|
2020-01-22 10:54:22 +00:00
|
|
|
args = parse_args()
|
2019-03-05 17:09:12 +00:00
|
|
|
|
2020-01-22 10:54:22 +00:00
|
|
|
if not run_tests(args.device_name, use_xmlrunner=args.xml).wasSuccessful():
|
2019-03-05 17:09:12 +00:00
|
|
|
sys.exit(-1)
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|