Fixture

Encapsulates one or more datasets to be loaded into a storage medium.

Most likely you will only use one of the wrapper classes designed for dealing with a specific storage medium.

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

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', fields=['name','on_sale'] ) # csv field headers
>>> fxt.product_on_sale
Fixture.Values(**{'on_sale': True, 'name': 'toy truck'})

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
Fixture.Values(**{'on_sale': True, 'name': 'slacks, reversible'})

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
Fixture.Values(**{'name': 'bob', 'hair_color': 'black'})

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
Fixture.Values(**{'name': 'jane', 'hair_color': 'brown'})

... 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
Fixture.Values(**{'name': 'jane', 'hair_color': 'brown'})

Methods

f __init__(self, data=None, build_meta=True, clean=False) ...

f __len__(self) ...

See the source for more information.