dectest.sideaffects

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.

Base side affect test

This class provides a blank side affect test as an example as what a side affect test should provide.

class dectest.sideaffects.SideAffectTest(logger)[source]

A base class for other side affect tests.

needs_instance

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

pre_test()[source]

Called before the function that is being tested is run. An optional callback.

test()[source]

The actuall test that will be run. Should return True for success, and False for fail.

decorator(*args, **kwargs)[source]

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().

static blank_decorator(func)[source]

A decorator that does nothing.

Side affect tests

Some situations are incredibly common, and thus dectest provides built in classes to deal with them.

class dectest.sideaffects.GlobalStateChange(logger)[source]

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
...
pre_test()[source]

Called before the tested function is called, so we use this to capture the global variables’ state before the function is called.

test()[source]

Returns True if the self.func.__globals__ is equal to self.initglobal.update(self.expectedglobal).

decorator(tests)[source]

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.

class dectest.sideaffects.ClassStateChange(logger)[source]

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
...
decorator(tests)[source]

Takes a dict of items that should be in the class’ namespace, and stores them for use by the ClassStateChange later.

pre_test()

Use the instance attribute that was assigned by the TestCase to store the specified pieces of the state before the tested function is run.

test()[source]

Checks that the state has changed as determined by the argument passed to the decorator function.

Table Of Contents

Previous topic

dectest.suite

Next topic

dectest.config

This Page