C_Z_ C_Z_ - 2 years ago 77
Python Question

Testing classwide attributes

Let's say I have a module named

with a class
has a classwide counter attribute that allows me to track the order which instances were created.
looks like this:

from itertools import count

class Bar:
class_count = count(0)
def __init__(self):
self.id = self.class_count.next()

Now I have a test file where I am testing the various functionalities of
. I am unsure of how to test this
attribute, because the other unittests are creating instances of
and so I don't know what the given id of a
instance should be. Furthermore, this behavior of my class means that my unittests are independent of each other, which is undesirable. How should I structure my unittests so the tests are independent of each other?

Answer Source

You could use setUp to safe the current count and then temporarily reset the count. Then with tearDown you restore the original state again:

from itertools import count
import unittest

class Bar:
    class_count = count(0)
    def __init__(self):
        self.id = next(self.class_count)

class TestBar(unittest.TestCase):
    def setUp(self):
        self.nxtcount = next(Bar.class_count)  # safe current state
        Bar.class_count = count(0)             # reset to 0

    def tearDown(self):
        Bar.class_count = count(self.nxtcount) # reset to old state

    def teststh1(self):
        x = Bar()
        self.assertEqual(x.id, 0)

    def teststh2(self):
        x1 = Bar()
        x2 = Bar()
        x3 = Bar()
        self.assertEqual(x1.id, 0)
        self.assertEqual(x2.id, 1)
        self.assertEqual(x3.id, 2)

This makes sure every test method will start with a Bar.class_count of 0.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download