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.

3
  • 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. Commented Dec 18, 2017 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)? Commented Dec 18, 2017 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 ;) Commented Dec 18, 2017 at 20:07

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.