-
-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: switch test frameworks #266
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,65 +1,51 @@ | ||||||||||||||||||||||
# pylint: skip-file | ||||||||||||||||||||||
import importlib | ||||||||||||||||||||||
import pkgutil | ||||||||||||||||||||||
import sys | ||||||||||||||||||||||
import unittest | ||||||||||||||||||||||
|
||||||||||||||||||||||
import pytest | ||||||||||||||||||||||
|
||||||||||||||||||||||
_MODULES = sys.modules.keys() | ||||||||||||||||||||||
|
||||||||||||||||||||||
_SYSTEM_MODULES = [ | ||||||||||||||||||||||
"system.alarm", | ||||||||||||||||||||||
"system.bacnet", | ||||||||||||||||||||||
"system.bacnet.enumerated", | ||||||||||||||||||||||
"system.bacnet.enums", | ||||||||||||||||||||||
"system.dataset", | ||||||||||||||||||||||
"system.date", | ||||||||||||||||||||||
"system.db", | ||||||||||||||||||||||
"system.dnp3", | ||||||||||||||||||||||
"system.eam", | ||||||||||||||||||||||
"system.file", | ||||||||||||||||||||||
"system.groups", | ||||||||||||||||||||||
"system.gui", | ||||||||||||||||||||||
"system.iec61850", | ||||||||||||||||||||||
"system.math", | ||||||||||||||||||||||
"system.mongodb", | ||||||||||||||||||||||
"system.mongodb.types", | ||||||||||||||||||||||
"system.nav", | ||||||||||||||||||||||
"system.net", | ||||||||||||||||||||||
"system.opc", | ||||||||||||||||||||||
"system.opchda", | ||||||||||||||||||||||
"system.opcua", | ||||||||||||||||||||||
"system.perspective", | ||||||||||||||||||||||
"system.perspective.workstation", | ||||||||||||||||||||||
"system.project", | ||||||||||||||||||||||
"system.report", | ||||||||||||||||||||||
"system.roster", | ||||||||||||||||||||||
"system.secsgem", | ||||||||||||||||||||||
"system.security", | ||||||||||||||||||||||
"system.serial", | ||||||||||||||||||||||
"system.sfc", | ||||||||||||||||||||||
"system.tag", | ||||||||||||||||||||||
"system.twilio", | ||||||||||||||||||||||
"system.user", | ||||||||||||||||||||||
"system.util", | ||||||||||||||||||||||
"system.vision", | ||||||||||||||||||||||
] | ||||||||||||||||||||||
_SYS_MODULES = sys.modules.keys() | ||||||||||||||||||||||
_MODULES = [] | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
def _cleanup(): | ||||||||||||||||||||||
for module in sys.modules.keys(): | ||||||||||||||||||||||
if module not in _MODULES: | ||||||||||||||||||||||
if module not in _SYS_MODULES: | ||||||||||||||||||||||
sys.modules.pop(module) | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
@pytest.mark.parametrize("test_module", _SYSTEM_MODULES) | ||||||||||||||||||||||
def test_system(test_module): | ||||||||||||||||||||||
try: | ||||||||||||||||||||||
imported_module = importlib.import_module(test_module) | ||||||||||||||||||||||
except ImportError: | ||||||||||||||||||||||
assert False, "Failed to import module: {0}".format(test_module) | ||||||||||||||||||||||
def _create_import_test(module_name): | ||||||||||||||||||||||
def test(self): | ||||||||||||||||||||||
self.assertModuleAvailable(module_name) | ||||||||||||||||||||||
|
||||||||||||||||||||||
return test | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
def _discover_modules(package): | ||||||||||||||||||||||
for _, name, is_pkg in pkgutil.walk_packages(["src/{}".format(package)]): | ||||||||||||||||||||||
if is_pkg: | ||||||||||||||||||||||
_module = "{}.{}".format(package, name) | ||||||||||||||||||||||
_MODULES.append(_module) | ||||||||||||||||||||||
_discover_modules("{}/{}".format(package, name)) | ||||||||||||||||||||||
else: | ||||||||||||||||||||||
_module = "{}.{}".format(package, name) | ||||||||||||||||||||||
_MODULES.append(_module.replace("/", ".")) | ||||||||||||||||||||||
return _MODULES | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
class TestPackageImports(unittest.TestCase): | ||||||||||||||||||||||
modules_to_test = _discover_modules("system") | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): Ensure cleanup is called after each test to prevent state leakage. The |
||||||||||||||||||||||
|
||||||||||||||||||||||
for module_name in modules_to_test: | ||||||||||||||||||||||
test_name = "test_import_{}".format(module_name) | ||||||||||||||||||||||
locals()[test_name] = _create_import_test(module_name) | ||||||||||||||||||||||
_cleanup() | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): Consider a more explicit approach than using Using
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
def assertModuleAvailable(self, module_name): | ||||||||||||||||||||||
try: | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): Consider adding a test for dynamic module discovery failure. While the current tests ensure that expected modules can be imported, it would be beneficial to also test the behavior when a module cannot be discovered dynamically. This could help in ensuring the robustness of the dynamic discovery mechanism.
Suggested change
|
||||||||||||||||||||||
importlib.import_module(module_name) | ||||||||||||||||||||||
except ImportError: | ||||||||||||||||||||||
self.fail("Failed to import module: {}".format(module_name)) | ||||||||||||||||||||||
|
||||||||||||||||||||||
assert imported_module is not None, "Module {0} is None after import.".format( | ||||||||||||||||||||||
test_module | ||||||||||||||||||||||
) | ||||||||||||||||||||||
|
||||||||||||||||||||||
_cleanup() | ||||||||||||||||||||||
if __name__ == "__main__": | ||||||||||||||||||||||
unittest.main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (code-quality): Hoist repeated code outside conditional statement (
hoist-statement-from-if
)