Jim T
Jim T

Reputation: 65

Properly creating object instances for unit test

I'm trying to run a unit test in a separate unit test file located in the same directory, but running the unit test file just runs the functionality within the main file, while not calling upon the instance I've tried to create. Here's the code for the main file:

class ListOperations:
    def list_copy(l):
        return [x for x in l]

    print(list_copy(["What","A", "List"]))

Here's the unit test code located in the same directory:

import unittest
from basicfunction import ListOperations

class TestListOperations(unittest.TestCase):

    def test_1(self):
        ListOperations = ListOperations()
        self.assertEqual(ListOperations.list_copy(["This", "Is", "Just", "A", "List"]), ["This", "Is", "Just", "A", "List"])

    def test_2(self):
        self.assertTrue(True)

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

When I try to run the unit test, I get the output:

E.
======================================================================
ERROR: test_1 (__main__.TestListOperations)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:/path/basicfunction.ut.py", line 8, in test_1
    ListOperations = ListOperations()
UnboundLocalError: local variable 'ListOperations' referenced before assignment

----------------------------------------------------------------------
Ran 2 tests in 0.001s

FAILED (errors=1)
['What', 'A', 'List']

How could I set this test up properly?

Edit: @staticmethod helped to run the test above, but I'm having issues with methods with two parameters. If I try to create an instance outside of the unittest class:

lop = ListOperations()
intersect1 = ["Pikachu", "Evolves", "Into", "Raichu"]
intersect2 = ["Raichu", "Does", "Not", "Evolve", "Into", "Anything"]
print(lop.list_intersect(intersect1, intersect2))

Output is ['Into', 'Raichu'] in the main file, but I get an error of TypeError: list_intersect() takes 2 positional arguments but 3 were given when I try to import into the test file. Any thoughts on how I could resolve this error?

Upvotes: 0

Views: 1479

Answers (2)

Canti
Canti

Reputation: 11

Firstly, in OOP for Python, when you define a method you will need to put the parameter self in the first position. This is to give the method access to the underlying object that it is tied to.

class ListOperations:
    def list_copy(self, l):
        return [x for x in l]

The other issue causing the error message is your variable name ListOperations. That word is taken by your class ListOpertions so you can change it to listOperations so that they don't collide.

import unittest
from basicfunction import ListOperations

class TestListOperations(unittest.TestCase):

    def test_1(self):
        listOperations = ListOperations()
        self.assertEqual(listOperations.list_copy(["This", "Is", "Just", "A", "List"]), ["This", "Is", "Just", "A", "List"])

    def test_2(self):
        self.assertTrue(True)

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

Upvotes: 1

Gaurang patel
Gaurang patel

Reputation: 21

I think there is small bug in your code you are using same name for variable as the name of your class ListOperations.

Try changing variable name to something else and run the code

For example

lop = ListOperations() 
self.assertEqual(lop.list_copy(["This", "Is", "Just", "A", "List"]), ["This", "Is", "Just", "A", "List"])

Upvotes: 0

Related Questions