paste.fixture – Test applications

Contents

Routines for testing WSGI applications.

Most interesting is the TestApp for testing WSGI applications, and the TestFileEnvironment class for testing the effects of command-line scripts.

Module Contents

class paste.fixture.TestApp(app, namespace=None, relative_to=None, extra_environ=None, pre_request_hook=None, post_request_hook=None)
delete(url, params=b'', headers=None, extra_environ=None, status=None, expect_errors=False)

Do a DELETE request. Very like the .get() method. params are put in the body of the request.

Returns a response object

do_request(req, status)

Executes the given request (req), with the expected status. Generally .get() and .post() are used instead.

encode_multipart(params, files)

Encodes a set of parameters (typically a name/value list) and a set of files (a list of (name, filename, file_body)) into a typical POST body, returning the (content_type, body).

get(url, params=None, headers=None, extra_environ=None, status=None, expect_errors=False)

Get the given url (well, actually a path like '/page.html').

params:

A query string, or a dictionary that will be encoded into a query string. You may also include a query string on the url.

headers:

A dictionary of extra headers to send.

extra_environ:

A dictionary of environmental variables that should be added to the request.

status:

The integer status code you expect (if not 200 or 3xx). If you expect a 404 response, for instance, you must give status=404 or it will be an error. You can also give a wildcard, like '3*' or '*'.

expect_errors:

If this is not true, then if anything is written to wsgi.errors it will be an error. If it is true, then non-200/3xx responses are also okay.

Returns a response object

head(url, headers=None, extra_environ=None, status=None, expect_errors=False)

Do a HEAD request. Very like the .get() method.

Returns a response object

post(url, params=b'', headers=None, extra_environ=None, status=None, upload_files=None, expect_errors=False)

Do a POST request. Very like the .get() method. params are put in the body of the request.

upload_files is for file uploads. It should be a list of [(fieldname, filename, file_content)]. You can also use just [(fieldname, filename)] and the file content will be read from disk.

Returns a response object

put(url, params=b'', headers=None, extra_environ=None, status=None, upload_files=None, expect_errors=False)

Do a PUT request. Very like the .get() method. params are put in the body of the request.

upload_files is for file uploads. It should be a list of [(fieldname, filename, file_content)]. You can also use just [(fieldname, filename)] and the file content will be read from disk.

Returns a response object

reset()

Resets the state of the application; currently just clears saved cookies.

class paste.fixture.TestRequest(url, environ, expect_errors=False)

Forms

class paste.fixture.Form(response, text)

This object represents a form that has been found in a page. This has a couple useful attributes:

text:

the full HTML of the form.

action:

the relative URI of the action.

method:

the method (e.g., 'GET').

id:

the id, or None if not given.

fields:

a dictionary of fields, each value is a list of fields by that name. <input type="radio"> and <select> are both represented as single fields with multiple options.

get(name, index=None, default=<class 'paste.fixture.NoDefault'>)

Get the named/indexed field object, or default if no field is found.

select(name, value, index=None)

Like .set(), except also confirms the target is a <select>.

set(name, value, index=None)

Set the given name, using index to disambiguate.

submit(name=None, index=None, **args)

Submits the form. If name is given, then also select that button (using index to disambiguate)``.

Any extra keyword arguments are passed to the .get() or .post() method.

Returns a response object.

submit_fields(name=None, index=None)

Return a list of [(name, value), ...] for the current state of the form.

class paste.fixture.Field(form, tag, name, pos, value=None, id=None, **attrs)

Field object.

force_value(value)

Like setting a value, except forces it even for, say, hidden fields.

class paste.fixture.Select(*args, **attrs)

Field representing <select>

class paste.fixture.Radio(*args, **attrs)

Field representing <input type="radio">

class paste.fixture.Checkbox(*args, **attrs)

Field representing <input type="checkbox">

class paste.fixture.Text(form, tag, name, pos, value='', id=None, **attrs)

Field representing <input type="text">

class paste.fixture.Textarea(form, tag, name, pos, value='', id=None, **attrs)

Field representing <textarea>

class paste.fixture.Hidden(form, tag, name, pos, value='', id=None, **attrs)

Field representing <input type="hidden">

class paste.fixture.Submit(form, tag, name, pos, value=None, id=None, **attrs)

Field representing <input type="submit"> and <button>

Script Testing

class paste.fixture.TestFileEnvironment(base_path, template_path=None, script_path=None, environ=None, cwd=None, start_clear=True, ignore_paths=None, ignore_hidden=True)

This represents an environment in which files will be written, and scripts will be run.

clear()

Delete all the files in the base directory.

run(script, *args, **kw)

Run the command, with the given arguments. The script argument can have space-separated arguments, or you can use the positional arguments.

Keywords allowed are:

expect_error: (default False)

Don’t raise an exception in case of errors

expect_stderr: (default expect_error)

Don’t raise an exception if anything is printed to stderr

stdin: (default "")

Input to the script

printresult: (default True)

Print the result after running

cwd: (default self.cwd)

The working directory to run in

Returns a ProcResponse object.

writefile(path, content=None, frompath=None)

Write a file to the given path. If content is given then that text is written, otherwise the file in frompath is used. frompath is relative to self.template_path

class paste.fixture.ProcResult(test_env, args, stdin, stdout, stderr, returncode, files_before, files_after)

Represents the results of running a command in TestFileEnvironment.

Attributes to pay particular attention to:

stdout, stderr:

What is produced

files_created, files_deleted, files_updated:

Dictionaries mapping filenames (relative to the base_dir) to FoundFile or FoundDir objects.

class paste.fixture.FoundFile(base_path, path)

Represents a single file found as the result of a command.

Has attributes:

path:

The path of the file, relative to the base_path

full:

The full path

stat:

The results of os.stat. Also mtime and size contain the .st_mtime and st_size of the stat.

bytes:

The contents of the file.

You may use the in operator with these objects (tested against the contents of the file), and the .mustcontain() method.

class paste.fixture.FoundDir(base_path, path)

Represents a directory created by a command.