1

I got stuck while writing unit test cases by mock function. I have a self variable created inside of the function, not by the init method. When I run the script, it throws an error.

myFooFile.py:

class Foo(object):

    def my_foo(self):
        self.MY_LIST = []
        self.generate_value()
        self.other_function()
        
    def generate_value(self):
        some_ops_value = 10
        return self.MY_LIST.append(some_ops_value)

I have MY_LIST variable created in my_foo method, not by the __init__ method. I am collecting values by the generate_value method. When I try to create a test generate_value method, throws an error

AttributeError: 'Foo' object has no attribute 'MY_LIST'.

Not sure how to mock MY_LIST and get it worked

Below is my test script:

import unittest
from unittest import TestCase, mock
from myFooFile import Foo

class FooTest(TestCase):

    def setUp(self):
        self.foo = Foo()
        
    def test_generate_value(self):
        self.assertEqual(self.foo.generate_value(), [10])
        
unittest.main()
Lin Du
  • 88,126
  • 95
  • 281
  • 483
kumar
  • 41
  • 7

0 Answers0