test_setup_env.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import datetime
  2. import sys
  3. from unittest import TestCase
  4. from mmengine import DefaultScope
  5. from mmdet.utils import register_all_modules
  6. class TestSetupEnv(TestCase):
  7. def test_register_all_modules(self):
  8. from mmdet.registry import DATASETS
  9. # not init default scope
  10. sys.modules.pop('mmdet.datasets', None)
  11. sys.modules.pop('mmdet.datasets.coco', None)
  12. DATASETS._module_dict.pop('CocoDataset', None)
  13. self.assertFalse('CocoDataset' in DATASETS.module_dict)
  14. register_all_modules(init_default_scope=False)
  15. self.assertTrue('CocoDataset' in DATASETS.module_dict)
  16. # init default scope
  17. sys.modules.pop('mmdet.datasets')
  18. sys.modules.pop('mmdet.datasets.coco')
  19. DATASETS._module_dict.pop('CocoDataset', None)
  20. self.assertFalse('CocoDataset' in DATASETS.module_dict)
  21. register_all_modules(init_default_scope=True)
  22. self.assertTrue('CocoDataset' in DATASETS.module_dict)
  23. self.assertEqual(DefaultScope.get_current_instance().scope_name,
  24. 'mmdet')
  25. # init default scope when another scope is init
  26. name = f'test-{datetime.datetime.now()}'
  27. DefaultScope.get_instance(name, scope_name='test')
  28. with self.assertWarnsRegex(
  29. Warning, 'The current default scope "test" is not "mmdet"'):
  30. register_all_modules(init_default_scope=True)