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 (if None, fields will be keys of the first data item)

  • filename -- name of file or filelike object

  • mknewfile -- factory method for file objects. if not None and filename is not None and no filename is passed to the constructor then this is expected to return an open file handle. It will be called with its first argument the instance of the Fixture class. Note that any meta method needs to be wrapped in

    testtools.fixtures.metamethod ; see example of this below...

  • dialect -- csv dialect to use (default: excel)

Here is an example of declaring a mknewfile factory method:

>>> from testtools.fixtures import CsvFixture, metamethod
>>> from os import path
>>> @metamethod
... def mk_new_bookfile(fxt):
...     print fxt
...     return open('/tmp/testtools_example.csv', 'w')
...
>>> class MyBooks(CsvFixture):
...     class meta:
...         mknewfile = mk_new_bookfile
...         data = (('Yann Martel', {'title': 'Life of Pi'}),)
...
>>> fxt = MyBooks()
<class 'MyBooks' keys=() from module testtools.fixtures.storable>
>>> fxt.close()
>>> assert path.exists('/tmp/testtools_example.csv')

NOTE: at the time the fixture instance is passed into the factory method, it obviously will contain no data. Its use would be primarily for making decisions on what file to return.


Methods

f __init__(self, **kw) ...

f __len__(self) ...

See the source for more information.