testtools.content
module documentationtesttools
Class | Content | A MIME-like Content object. |
Class | StackLinesContent | Content object for stack lines. |
Class | TracebackContent | Content object for tracebacks. |
Function | StacktraceContent | Content object for stack traces. |
Function | json_content | Create a JSON Content object from JSON-encodeable data. |
Function | text_content | Create a Content object from some text. |
Function | maybe_wrap | Merge metadata for func into wrapper if functools is present. |
Function | content_from_file | Create a Content object from a file on disk. |
Function | content_from_stream | Create a Content object from a file-like stream. |
Function | content_from_reader | Create a Content object that will obtain the content from reader. |
Function | attach_file | Attach a file to this test as a detail. |
Function | _iter_chunks | Read 'stream' in chunks of 'chunk_size'. |
Parameters | stream | A file-like object to read from. |
chunk_size | The size of each read from 'stream'. | |
seek_offset | If non-None, seek before iterating. | |
seek_whence | Pass through to the seek call, if seeking. |
Content object for stack traces.
This function will create and return a 'Content' object that contains a stack trace.
The mime type is set to 'text/x-traceback;language=python', so other languages can format their stack traces differently.
Parameters | prefix_content | A unicode string to add before the stack lines. |
postfix_content | A unicode string to add after the stack lines. |
Create a Content object from some text.
This is useful for adding details which are short strings.
Create a Content object from a file on disk.
Note that unless buffer_now is explicitly passed in as True, the file will only be read from when iter_bytes is called.
Parameters | path | The path to the file to be used as content. |
content_type | The type of content. If not specified, defaults to UTF8-encoded text/plain. | |
chunk_size | The size of chunks to read from the file. Defaults to DEFAULT_CHUNK_SIZE. | |
buffer_now | If True, read the file from disk now and keep it in memory. Otherwise, only read when the content is serialized. | |
seek_offset | If non-None, seek within the stream before reading it. | |
seek_whence | If supplied, pass to stream.seek() when seeking. |
Create a Content object from a file-like stream.
Note that unless buffer_now is explicitly passed in as True, the stream will only be read from when iter_bytes is called.
Parameters | stream | A file-like object to read the content from. The stream is not closed by this function or the 'Content' object it returns. |
content_type | The type of content. If not specified, defaults to UTF8-encoded text/plain. | |
chunk_size | The size of chunks to read from the file. Defaults to DEFAULT_CHUNK_SIZE. | |
buffer_now | If True, reads from the stream right now. Otherwise, only reads when the content is serialized. Defaults to False. | |
seek_offset | If non-None, seek within the stream before reading it. | |
seek_whence | If supplied, pass to stream.seek() when seeking. |
Parameters | reader | A callback to read the content. Should return an iterable of bytestrings. |
content_type | The content type to create. | |
buffer_now | If True the reader is evaluated immediately and buffered. |
Attach a file to this test as a detail.
This is a convenience method wrapping around addDetail.
Note that by default the contents of the file will be read immediately. If buffer_now is False, then the file must exist when the test result is called with the results of this test, after the test has been torn down.
Parameters | detailed | An object with details |
path | The path to the file to attach. | |
name | The name to give to the detail for the attached file. | |
content_type | The content type of the file. If not provided, defaults to UTF8-encoded text/plain. | |
chunk_size | The size of chunks to read from the file. Defaults to something sensible. | |
buffer_now | If False the file content is read when the content object is evaluated rather than when attach_file is called. Note that this may be after any cleanups that obj_with_details has, so if the file is a temporary file disabling buffer_now may cause the file to be read after it is deleted. To handle those cases, using attach_file as a cleanup is recommended because it guarantees a sequence for when the attach_file call is made: detailed.addCleanup(attach_file, 'foo.txt', detailed) |