WestCoastProjects
WestCoastProjects

Reputation: 63022

Can the unittest framework discover nested tests?

Is the following nested structure discoverable by unittest ?

class HerclTests(unittest.TestCase):

    def testJobs(self):
        def testJobSubmit():
            jid = "foobar"
            assert jid,'hercl job submit failed no job_id'
            return jid

        def testJobShow(jid):
            jid = "foobar"
            out,errout=bash(f"hercl job show --jid {jid} --form json")
            assert 'Job run has been accepted by airflow successfully' in out,'hercl job show failed'

Here is the error when trying to run unittest :

============================= test session starts ==============================
platform darwin -- Python 3.6.7, pytest-5.4.3, py-1.10.0, pluggy-0.13.1 -- /Users/steve/git/hercl/.venv/bin/python
cachedir: .pytest_cache
rootdir: /Users/steve/git/hercl/tests
collecting ... collected 0 items
ERROR: not found: /Users/steve/git/hercl/tests/hercl_flow_test.py::HerclTests::testJobs::testJobSubmit
(no name '/Users/steve/git/hercl/tests/hercl_flow_test.py::HerclTests::testJobs::testJobSubmit' in any of [<TestCaseFunction testJobs>])


============================ no tests ran in 0.01s =============================

Can this structure be tweaked to work with unittest or must each test method be elevated to the level of the HerclTests class?

Upvotes: 0

Views: 221

Answers (1)

Christoph Burschka
Christoph Burschka

Reputation: 4689

This can't work - functions defined inside another function ("inner functions") only "exist" as variables inside the outer function's local scope. They're not accessible to any other code. ​The unittest discovery won't find them, and couldn't call them even if it knew about them.

Upvotes: 1

Related Questions