mercredi 27 mai 2020

What is best-practice for importing classes or functions to into a `unittest` test module

What is best-practice for importing classes or functions to into a unittest test module?

Suppose, I've the following common package in my project root, and I'd like to test common/utils.py in common/tests/test_utils.py:

common
├── __init__.py
├── tests
│   ├── __init__.py
│   └── test_utils.py
└── utils.py

Should I import classes or functions at the top-level of the test module, or inside the the individual test cases (see below)?

import unittest


class TestUtils(unittest.TestCase):

    def test_object_factory(self):
        from ..utils import object_factory
        object_factory = _(foo=1, bar='bar', baz={'a': 1, 'b': (2, 3)})
        self.assertTrue(hasattr(o, 'foo'))
        self.assertEqual(o.foo, 1)
        self.assertTrue(hasattr(o, 'bar'))
        self.assertEqual(o.bar, 'bar')
        self.assertTrue(hasattr(o, 'baz'))
        self.assertEqual(o.baz, {'a': 1, 'b': (2, 3)})

Aucun commentaire:

Enregistrer un commentaire