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.
-
class paste.fixture.TestApp(app, namespace=None, relative_to=None, extra_environ=None, pre_request_hook=None, post_request_hook=None)
-
delete(url, params='', 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
-
post(url, params='', 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='', 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)
-
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.