Skip to content
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

Merged
merged 2 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 39 additions & 53 deletions tests/test_system.py
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("/", "."))
Copy link
Contributor

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)

return _MODULES


class TestPackageImports(unittest.TestCase):
modules_to_test = _discover_modules("system")
Copy link
Contributor

Choose a reason for hiding this comment

The 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 _cleanup function is called at the end of the module discovery process, which might not guarantee its execution after each test case. Consider using tearDown or tearDownClass methods to ensure that _cleanup is executed reliably after each test or after all tests in the class, respectively.


for module_name in modules_to_test:
test_name = "test_import_{}".format(module_name)
locals()[test_name] = _create_import_test(module_name)
_cleanup()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Consider a more explicit approach than using locals() for test case generation.

Using locals() to dynamically generate test cases can make the code harder to understand and debug. A more explicit approach, such as adding test methods directly to the TestPackageImports class or using a test case generation library, might improve readability and maintainability.

Suggested change
locals()[test_name] = _create_import_test(module_name)
def _generate_test_cases():
for module_name in modules_to_test:
test_name = "test_import_{}".format(module_name)
def test(self, module_name=module_name):
self.assertModuleAvailable(module_name)
setattr(TestPackageImports, test_name, test)
_generate_test_cases()


def assertModuleAvailable(self, module_name):
try:
Copy link
Contributor

Choose a reason for hiding this comment

The 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
def assertModuleAvailable(self, module_name):
def assertModuleAvailable(self, module_name):
try:
importlib.import_module(module_name)
except ImportError:
self.fail("Failed to import module: {}".format(module_name))
def test_module_not_found_behavior(self):
with self.assertRaises(ImportError):
importlib.import_module("non_existent_module_name")

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()
8 changes: 3 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ requires =
tox>=4.2
virtualenv<20.22.0
env_list =
pytest
unittest
typecheck

[testenv:pytest]
[testenv:unittest]
description = run tests
base_python = python2.7
deps =
pytest
commands =
pytest
python -m unittest discover -s tests

[testenv:typecheck]
description = run type check on code base
Expand Down
Loading