fixtures

Provides tools to load and inspect test data.

See class/ function docs for examples of usage. The Fixture class has most of the examples.


Functions

f unload_fixtures() ...

unload any loaded fixtures.

f find_so_from_data(meta, fixture_class) ...

finder method for SOFixture that transforms SomeFixtureData into the name SomeFixture.

f find_so_from_fixture(meta, fixture_class) ...

finder method for SOFixture that passes through your fixture name (no transformation).

This is the default.

f find_so_from_table(meta, fixture_class) ...

finder method for SOFixture that transforms your fixture class as if it were a table name.

It assumes your fixture class is named exactly like your database table name and its SQLObject should be translated like it would in SQLObject land.

i.e. fixture class products_to_offers becomes ProductsToOffers

Classes

C CsvFixture(...) ...

Fixture storable as a CSV file.

The following are attributes you can set as keywords or meta attributes (see Fixture for examples on configuring a class using meta)

  • addfields -- if True first row in the CSV file will contain a field list
  • data -- dataset(s) to load (see Fixture for format)
  • encoding -- string encoding to use when writing to file
  • fields -- field list
  • filename -- name of file or filelike object

This class contains 2 members.

C 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'})

This class contains 2 members.

C SOFixture(...) ...

Fixture storable by an SQLObject.

The following are attributes you can set as keywords or meta attributes (see Fixture for examples on configuring a class using meta)

  • data -- datasets to load (see Fixture for format)
  • so_class -- default SQLObject class to use (if missing, SOFixture.find_so_class will be consulted)
  • env -- dict or something with attributes
  • finder -- callable to get so_class from self.__class__ (default is find_so_from_fixture )

This class contains 2 members.

C affix(...) ...

a collection of Fixture instances so that all attributes can be shared.

accepts fixture instances as arguments, returns an instance with all attributes combined.

This class contains 2 members.

C FixtureCollection(...) ...

a collection of Fixture instances so that all attributes can be shared.

accepts fixture instances as arguments, returns an instance with all attributes combined.

This class contains 2 members.

See the source for more information.