Testing Applications with WebTest¶
|author:||Ian Bicking <firstname.lastname@example.org>|
|maintainer:||Gael Pasgrimaud <email@example.com>|
Status & License¶
WebTest is an extraction of
portions to use WebOb. It is under
active development as part of the Pylons cloud of packages.
This library is licensed under an MIT-style license.
You can use pip or easy_install to get the latest stable release:
$ pip install WebTest $ easy_install WebTest
Or if you want the development version:
$ pip install https://nodeload.github.com/Pylons/webtest/tar.gz/master
What This Does¶
WebTest helps you test your WSGI-based web applications. This can be any application that has a WSGI interface, including an application written in a framework that supports WSGI (which includes most actively developed Python web frameworks – almost anything that even nominally supports WSGI should be testable).
With this you can test your web applications without starting an HTTP server, and without poking into the web framework shortcutting pieces of your application that need to be tested. The tests WebTest runs are entirely equivalent to how a WSGI HTTP server would call an application. By testing the full stack of your application, the WebTest testing model is sometimes called a functional test, integration test, or acceptance test (though the latter two are not particularly good descriptions). This is in contrast to a unit test which tests a particular piece of functionality in your application. While complex programming tasks are often suited to unit tests, template logic and simple web programming is often best done with functional tests; and regardless of the presence of unit tests, no testing strategy is complete without high-level tests to ensure the entire programming system works together.
WebTest helps you create tests by providing a convenient interface to run WSGI applications and verify the output.
The most important object in WebTest is
TestApp, the wrapper
for WSGI applications. It also allows you to perform HTTP requests on it.
To use it, you simply instantiate it with your WSGI application.
If your WSGI application requires any configuration, you must set that up manually in your tests.
Here is a basic application:
>>> def application(environ, start_response): ... headers = [('Content-Type', 'text/html; charset=utf8'), ... ('Content-Length', str(len(body)))] ... start_response('200 OK', headers) ... return [body]
Wrap it into a
>>> from webtest import TestApp >>> app = TestApp(application)
Then you can get the response of a HTTP GET:
>>> resp = app.get('/')
And check the results, like response’s status:
>>> assert resp.status == '200 OK' >>> assert resp.status_int == 200
>>> assert resp.content_type == 'text/html' >>> assert resp.content_length > 0
Or response’s body:
>>> resp.mustcontain('<html>') >>> assert 'form' in resp
WebTest can do much more. In particular, it can handle Form handling and
- Functional Testing of Web Applications
- HTTP client/server utilities
- WSGI Debug application
- Framework Hooks
- Contribute to webtest project
- 2.0.19 (unreleased)
- 2.0.18 (2015-02-05)
- 2.0.17 (2014-12-20)
- 2.0.16 (2014-09-19)
- 2.0.15 (2014-04-17)
- 2.0.14 (2014-01-23)
- 2.0.13 (2014-01-23)
- 2.0.12 (2014-01-17)
- 2.0.11 (2013-12-29)
- 2.0.10 (2013-11-14)
- 2.0.9 (2013-09-18)
- 2.0.8 (2013-09-17)
- 2.0.7 (2013-08-07)
- 2.0.6 (2013-05-23)
- 2.0.5 (2013-04-12)
- 2.0.4 (2013-03-28)
- 2.0.3 (2013-03-19)
- 2.0.2 (2013-03-15)
- 2.0.1 (2013-03-05)
- 2.0 (2013-02-25)
Copyright (c) 2010 Ian Bicking and Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.