Fixture
Encapsulates one or more datasets to be loaded into a storage medium.
The Fixture class is accessible via the testtools.fixtures module.
Most likely you will only use one of the wrapper classes designed for dealing with a specific storage medium (i.e. SOFixture for SQLObjects or CsvFixture for csv files).
before looking at how you load data and work with it, here is how fixtures are designed to work inside a test case:
>>> from testtools.testself.myfixtures import EmployeeData >>> class TestTPSCoverSheet: ... def setUp(self): ... self.fxt = EmployeeData() ... def tearDown(self): ... self.fxt.clean() >>>
Keyword Arguments
-
data -- if this is not None then your data will be loaded from here. see Defining Data. Setting this keyword will also make your object unregistrable.
-
stor_obj -- object to store data. if you don't provide this here or in meta, you will get a ValueError.
-
clean -- if this is True, your object is cleanable, meaning that its data can be deleted from the storage object (i.e. when db load fails, all will be deleted for you in try/except). Defaults to False.
-
- build_meta -- if this is True then the internal meta object will be built from the outermost
-
parent class (defaults to True). This is only provided so that wrapper classes can do their own building, which was necessary for some reason.
-
is_registrable -- if True (the default) the fixture object will have its data loaded only once per class instance unless data is unloaded via Fixture.clean`(). This is set internally for file-based fixtures (i.e. `CsvFixture ) and the like.
Now for the more low level examples ...
Example with CsvFixture
>>> from testtools.fixtures import CsvFixture >>> class Products(CsvFixture): ... class meta: ... data = ( ('product_on_sale', {'name':'toy truck', 'on_sale':True}), ) >>> fxt = Products(filename='/tmp/product.csv' ) >>> fxt.product_on_sale <class 'Values' dict(on_sale='True', name='toy truck') from module testtools.fixtures.fixtures>
Defining data
Data is an iterable of tuples where each row unpacks to a context key and dictionary of values. The context key becomes an attribute and/or dictionary key in the fixture object.
For example:
>>> # NOTE: the trailing comma here forces the type into a tuple : >>> fxt.load( (('reversible_slacks_on_sale', ... {'name': 'slacks, reversible', 'on_sale': True}),) ) >>> fxt.reversible_slacks_on_sale <class 'Values' dict(on_sale='True', name='slacks, re...') from module testtools.fixtures.fixtures>
A fixture's data can also be set in the following ways, respectively :
From the data keyword :
>>> from testtools.testself.model import Employee >>> from testtools.fixtures import SOFixture >>> fxt = SOFixture(data=(('bob', {'name':'bob','hair_color':'black'}),), so_class=Employee) >>> fxt.bob <class 'Values' dict(name='bob', hair_color='black') from module testtools.fixtures.fixtures>
from meta.data :
>>> # here the SQLObject "Employee" is pulled out from meta.env >>> # because it matches the class name employee >>> from testtools.testself import model >>> class Employee(SOFixture): ... class meta: ... env = model ... data = (('jane', {'name':'jane','hair_color':'brown'}),) >>> fxt = Employee() >>> fxt.jane <class 'Values' dict(name='jane', hair_color='brown') from module testtools.fixtures.fixtures>
... or by defining a data() method :
>>> # or you can set meta.so_class to the specific SQLObject >>> class JaneEmployee(SOFixture): ... class meta: ... so_class = model.Employee ... def data(self): ... return (('jane', {'name':'jane','hair_color':'brown'}),) >>> fxt = JaneEmployee() >>> fxt.jane <class 'Values' dict(name='jane', hair_color='brown') from module testtools.fixtures.fixtures>
A Word About Meta
the "meta" attribute of a Fixture defines properties of a Fixture class because instances are singletons with respect to their first parent class. I.E. all instances of EmployeeData will share the same data and will only be loaded once ... but DepartmentData instances have their own data. Most Fixture wrappers, like SOFixture , use meta to set up some common attributes for you. With the exception of "build_meta" all default meta attributes are defined like the Keyword Arguments, documented above.
Methods
f __init__(self, data=None, build_meta=True, clean=False, is_registrable=True) ...
see Fixture for signature.
See the source for more information.