test_setup_env.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import datetime
  3. import sys
  4. from unittest import TestCase
  5. from mmengine import DefaultScope
  6. from mmpose.utils import register_all_modules
  7. class TestSetupEnv(TestCase):
  8. def test_register_all_modules(self):
  9. from mmpose.registry import DATASETS
  10. dataset_name = 'CocoDataset'
  11. dataset_module = 'mmpose.datasets.datasets.body.coco_dataset'
  12. # not init default scope
  13. module = dataset_module
  14. while '.' in module:
  15. sys.modules.pop(module, None)
  16. module = module.rsplit('.', 1)[0]
  17. DATASETS._module_dict.pop(dataset_name, None)
  18. self.assertFalse(dataset_name in DATASETS.module_dict)
  19. register_all_modules(init_default_scope=False)
  20. self.assertTrue(dataset_name in DATASETS.module_dict)
  21. # init default scope
  22. module = dataset_module
  23. while '.' in module:
  24. sys.modules.pop(module, None)
  25. module = module.rsplit('.', 1)[0]
  26. DATASETS._module_dict.pop(dataset_name, None)
  27. self.assertFalse(dataset_name in DATASETS.module_dict)
  28. register_all_modules(init_default_scope=True)
  29. self.assertTrue(dataset_name in DATASETS.module_dict)
  30. self.assertEqual(DefaultScope.get_current_instance().scope_name,
  31. 'mmpose')
  32. # init default scope when another scope is init
  33. name = f'test-{datetime.datetime.now()}'
  34. DefaultScope.get_instance(name, scope_name='test')
  35. with self.assertWarnsRegex(
  36. Warning, 'The current default scope "test" is not "mmpose"'):
  37. register_all_modules(init_default_scope=True)