Coverage for tests/test_doImport.py: 12%
54 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-09 10:51 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-09 10:51 +0000
1# This file is part of utils.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# Use of this source code is governed by a 3-clause BSD-style
10# license that can be found in the LICENSE file.
12import inspect
13import unittest
15import lsst.utils.tests
16from lsst.utils import doImport, doImportType
19class ImportTestCase(lsst.utils.tests.TestCase):
20 """Basic tests of doImport."""
22 def testDoImport(self):
23 c = doImport("lsst.utils.tests.TestCase")
24 self.assertEqual(c, lsst.utils.tests.TestCase)
26 c = doImport("lsst.utils.tests.TestCase.assertFloatsAlmostEqual")
27 self.assertEqual(c, lsst.utils.tests.TestCase.assertFloatsAlmostEqual)
29 c = doImport("lsst.utils.doImport")
30 self.assertEqual(type(c), type(doImport))
31 self.assertTrue(inspect.isfunction(c))
33 c = doImport("lsst.utils")
34 self.assertTrue(inspect.ismodule(c))
36 with self.assertRaises(ImportError):
37 doImport("lsst.utils.tests.TestCase.xyprint")
39 with self.assertRaises(ImportError):
40 doImport("lsst.utils.nothere")
42 with self.assertRaises(ModuleNotFoundError):
43 doImport("missing module")
45 with self.assertRaises(ModuleNotFoundError):
46 doImport("lsstdummy.import.fail")
48 with self.assertRaises(ImportError):
49 doImport("lsst.import.fail")
51 with self.assertRaises(ImportError):
52 doImport("lsst.utils.x")
54 with self.assertRaises(TypeError):
55 doImport([])
57 # Use a special test module
58 with self.assertRaises(RuntimeError):
59 doImport("import_test.two.three.runtime")
61 with self.assertRaises(ImportError):
62 doImport("import_test.two.three.success.not_okay")
64 with self.assertRaises(ImportError):
65 doImport("import_test.two.three.fail")
67 # Check that the error message reports the notthere failure
68 try:
69 doImport("import_test.two.three.fail.myfunc")
70 except ImportError as e:
71 self.assertIn("notthere", str(e))
73 c = doImport("import_test.two.three.success")
74 self.assertTrue(c.okay())
75 c = doImport("import_test.two.three.success.okay")
76 self.assertTrue(c())
77 c = doImport("import_test.two.three.success.Container")
78 self.assertEqual(c.inside(), "1")
79 c = doImport("import_test.two.three.success.Container.inside")
80 self.assertEqual(c(), "1")
82 def testDoImportType(self):
83 with self.assertRaises(TypeError):
84 doImportType("lsst.utils")
86 c = doImportType("lsst.utils.tests.TestCase")
87 self.assertEqual(c, lsst.utils.tests.TestCase)
90if __name__ == "__main__": 90 ↛ 91line 90 didn't jump to line 91, because the condition on line 90 was never true
91 unittest.main()