0

When I run the following test case using unittest.main():

import unittest

class TestInner_Helper(unittest.TestCase):

    class InnerClass(object):
        def __init__(self):
            self.flag_var = False

        def setFlag(self, newVal):
            self.flag_var = newVal

    def test_inner_class(self):
        inner = InnerClass()
        self.assertFalse(inner.flag_var)
        inner.setFlag(True)
        self.assertTrue(inner.flag_var)

if __name__ == "__main__":
    unittest.main()

I get the following error:

Traceback (most recent call last):
  File "test_inner_class.py", line 13, in test_inner_class
    inner = InnerClass()
NameError: global name 'InnerClass' is not defined

Why is the test runner unable to instantiate the inner class? I'm running this on Python 2.7.14 on OSX.

quanticle
  • 4,872
  • 6
  • 32
  • 42
  • 3
    `inner = self.InnerClass()` will work as the inner class is a class attribute of the outer class and should be accessed as such: `inner = TestInner_Helper.InnerClass()` is even clearer. – user2390182 Dec 18 '17 at 19:54
  • @schwobaseggl Could you put that in an answer so I can mark it as such (and give you the points you deserve)? – quanticle Dec 18 '17 at 20:00
  • I can't, the question is closed already (I have voted to close it myself), so no more answers are possible. Nevermind ;) – user2390182 Dec 18 '17 at 20:07

0 Answers0