0

I have the following code:

import unittest

class TestFMP(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        FMP_object = MyClass1(path, {})
        Locator_object = MyClass2(path)

    @classmethod
    def tearDownClass(cls):
        print('YYY')

    def test_method(self):

        self.assertEqual(FMP_object.method1(), Locator_object.method2())

My understanding is, that the setUpClass() should be executed once on instantiation of the TestFMP class, and provide constant access to FMP_object and Locator_object. However, when I run the test_method, I get the following error:

testcase = TestFMP()
testcase.test_method()

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-277-b11e25b4803c> in <module>
      1 testcase = TestFMP()
----> 2 testcase.test_method()

<ipython-input-276-dba1c1e55b1a> in test_method(self)
     12     def test_method(self):
     13 
---> 14         self.assertEqual(FMP_object.method1(), Locator_object.method2())

NameError: name 'FMP_object' is not defined

I get the same result when I attempt to access FMP_object/Locator_object with self. prefix in the test_method().

Any ideas on what I am doing wrong?

I am getting this on Python 3.6.

1 Answer 1

1

setupClass(cls) is called, but the results you computed aren't stored. You need to assign the results to an attribute on the cls (TestFMP class), and not just as variables, then you can retrieve these results through the self as self can access cls attributes (but not the inverse).

Something like the following will achieve what you're after:

import unittest

class TestFMP(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        # set `fmp` and `locator` onto the class through `cls` 
        cls.fmp = MyClass1(path, {})
        cls.locator = MyClass2(path)

    @classmethod
    def tearDownClass(cls):
        # Dispose of those resources through `cls.fmp` and `cls.locator`
        print('YYY')

    def test_method(self):
        # access through self: 
        self.assertEqual(self.fmp.method1(), self.locator.method2())
Sign up to request clarification or add additional context in comments.

1 Comment

I have done that, but now I am getting the following error: AttributeError: 'TestFMP' object has no attribute 'fmp'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.