class TestTarget:
def __init__(self,x=0):
self._x=x
self._isSet = False
def receiveInput():
self._x = int(input("set an integer"))
self.isSet = True
@property
def x():
return self._x
@property
def isSet(self):
return self._isSet
@isSet.setter
def isSet(self, x):
self._isSet = x
Testing
Testing and automating tests
unittest
Allows for the creation of test and subsequent automation.
@patch can be used as:
with patch('builtins.input', return_value="42"):
inside testing function
Using the example class below:
Which can be organized as a single test case with two test functions
class testCase(unittest.TestCase):
def test_init(self):
= TestTarget()
target self.assertFalse(target.isSet)
self.assertEqual(target.x,0)
@patch('builtins.input', return_value="42")
def test_setting(self, mocked_input):
= TestTarget(1)
target self.assertEqual(target.x,1)
target.receiveInput()self.assertTrue(target.isSet)
self.assertEqual(target.x,42)