Assuming that you're not wanting to check the value all the time during normal operation, the thing that pops to mind first would be the built-in unit test module.
If you are needing to do more complex things that you cannot cleanly isolate the test as shown below, check out a some of my issues I had to work around. It's worth noting that I ended up rolling my own test runner that behaves very similar to unit-test, but with greater control. I'll be pushing it up to py-pi in the next month or so.
Working Unit Test Examples
import unittest
class RealAccumlator(object):
def __init__(self):
self.acc = 0
def add(self, val):
self.acc += val
def set(self, val):
self.acc = val
def get(self):
return self.acc
# Using a global variable as I dont know how "sendCommandToMyProgram"
# is used
real_acc = RealAccumlator()
# Test Script
class MyAccumlator(object):
def __init__(self):
self.acc = 0
def add(self, val):
self.acc += val
real_acc.add(val)
def set(self, val):
self.acc = val
real_acc.set(val)
def check(self):
return self.acc == real_acc.get()
class MockedAccumlatorTests(unittest.TestCase):
def setUp(self):
self.acc = MyAccumlator()
def test_SetFunction(self):
test_values = range(-10,10)
# Test the set commands
for val in test_values:
self.acc.set(val)
self.assertTrue(self.acc.check())
def test_AddFunction(self):
test_values = range(-10,10)
# Set the acc to a known value and to a quick test
self.acc.set(0)
self.assertTrue(self.acc.check())
# Test the add commands
for val in test_values:
self.acc.add(val)
self.assertTrue(self.acc.check())
class RealAccumlatorTests(unittest.TestCase):
def test_SetFunction(self):
test_values = range(-10,10)
# Test the set commands
for val in test_values:
real_acc.set(val)
self.assertEqual(val, real_acc.get())
def test_AddFunction(self):
test_values = range(-10,10)
# Set the acc to a known value and to a quick test
real_acc.set(0)
self.assertEqual(0, real_acc.get())
# Test the add commands
expected_value = 0
for val in test_values:
expected_value += val
real_acc.add(val)
self.assertEqual(expected_value, real_acc.get())
if __name__ == '__main__':
unittest.main()
Update based on accepted answer
If this is only going to be a test script and your mock accumulator isn't used out side this tests consider the following mod. I still believe writing the unittests will serve you better in the long run
class MyAccumlator(object):
def __init__(self):
self.acc = 0
def add(self, val):
self.acc += val
real_acc.add(val)
assert(self.check())
return self.check()
def set(self, val):
self.acc = val
real_acc.set(val)
assert(self.check())
return self.check()
def check(self):
return self.acc == real_acc.get()
This would allow you to iterate over whatever lists you want without having to think about calling the check function. There are two methods provided that you could check.
Leaving the assert
call would throw an exception if they ever didn't match (only suggesting under the assumption this will live as a test script).
Removing the assert
and checking the return status from your calling script (rather than an explicit call to check()
might clean things up as well.