Side affects are important to test for, as in almost any non-functional language, they are used extensivly. dectest provides generic tests to test for different kinds of side affects.
This class provides a blank side affect test as an example as what a side affect test should provide.
A base class for other side affect tests.
If this attribute is True then the instance attribute of the side affect test will be set to a captured value of the the self argument from when the function was called and tested.
The default value of this argument is False
The actuall test that will be run. Should return True for success, and False for fail.
Is the decorator that is accessed at the attribute with the same name as the side affect test. As this is a decorator, it must return a function. If only the arguments are going to be used, and the function does not need to be modified, return blank_decorator().
Some situations are incredibly common, and thus dectest provides built in classes to deal with them.
A side affect test for changes in global state.
>>> ts = TestSuite("global state suite",
... DictConfig({'testing':
... {'sideaffects': ['dectest.sideaffects.GlobalStateChange']}}))
>>> globalvar = 3
>>> @ts.register("tc")
... @ts.tc.input(1)
... @ts.tc.globalstatechange({'globalvar': (lambda a, b: a + 1 == b)})
... def add(delta):
... globalvar += delta
...
>>> @ts.register("tc2")
... @ts.tc2.input(1)
... @ts.tc2.globalstatechange({'globalvar': 1})
... def set(i):
... global globalvar
... globalvar = i
...
Called before the tested function is called, so we use this to capture the global variables’ state before the function is called.
Returns True if the self.func.__globals__ is equal to self.initglobal.update(self.expectedglobal).
Takes a dictionary mapping variable names to tester functions. Each variable given as a key will be retreived before and after the function is run, and both are then passed to the tester function. If the tester function returns True, then the test has passed.
The dictionary may also have keys that are non callable values. If a value is found that isn’t callable, it will just be compared for equality with the value of the associated variable after the tested function has been called.
A side affect test for changes in a class’ state.
>>> ts = TestSuite("class state suite", DictConfig({'testing':
{'sideaffects': ['dectest.sideaffects.ClassStateChange']}}))
>>> class TestedClass():
... a = 3
...
... @ts.register("tc", method=True)
... @ts.tc.in(4)
... @ts.tc.classstatechange({'a': 4})
... def change_state(self, new_a):
... self.a = new_a
...
Takes a dict of items that should be in the class’ namespace, and stores them for use by the ClassStateChange later.