Testing Applications with WebTest¶
author: | Ian Bicking <ianb@colorstudy.com> |
---|---|
maintainer: | Gael Pasgrimaud <gael@gawel.org> |
Status & License¶
WebTest is an extraction ofpaste.fixture.TestApp
, rewritingportions to useWebOb. It is underactive development as part of the Pylons cloud of packages.
Feedback and discussion should take place on thePylons discuss list, and bugsshould go into theGithub tracker.
This library is licensed under anMIT-style license.
Installation¶
You can use pip or easy_install to get the latest stable release:
$pipinstallWebTest$easy_installWebTest
Or if you want the development version:
$pipinstallhttps://nodeload.github.com/Pylons/webtest/tar.gz/main
What This Does¶
WebTest helps you test your WSGI-based web applications. This can beany application that has a WSGI interface, including an applicationwritten in a framework that supports WSGI (which includes mostactively developed Python web frameworks -- almost anything that evennominally supports WSGI should be testable).
With this you can test your web applications without starting an HTTPserver, and without poking into the web framework shortcuttingpieces of your application that need to be tested. The tests WebTestruns are entirely equivalent to how a WSGI HTTP server would call anapplication. By testing the full stack of your application, theWebTest testing model is sometimes called afunctional test,integration test, oracceptance test (though the latter two arenot particularly good descriptions). This is in contrast to aunittest which tests a particular piece of functionality in yourapplication. While complex programming tasks are often suited tounit tests, template logic and simple web programming is often bestdone with functional tests; and regardless of the presence of unittests, no testing strategy is complete without high-level tests toensure the entire programming system works together.
WebTest helps you create tests by providing a convenient interface torun WSGI applications and verify the output.
Quick start¶
The most important object in WebTest isTestApp
, the wrapperfor WSGI applications. It also allows you to perform HTTP requests on it.To use it, you simply instantiate it with your WSGI application.
Note
If your WSGI application requires any configuration,you must set that up manually in your tests.
Here is a basic application:
>>>defapplication(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 aTestApp
:
>>>fromwebtestimportTestApp>>>app=TestApp(application)
Then you can get the response of a HTTP GET:
>>>resp=app.get('/')
And check the results, like response's status:
>>>assertresp.status=='200 OK'>>>assertresp.status_int==200
Response's headers:
>>>assertresp.content_type=='text/html'>>>assertresp.content_length>0
Or response's body:
>>>resp.mustcontain('<html>')>>>assert'form'inresp
WebTest can do much more. In particular, it can handleForm handling.
Contents¶
- Functional Testing of Web Applications
webtest
API- Contribute to webtest project
- News
- 3.0.1 (unreleased)
- 3.0.0 (2021-08-19)
- 2.0.35 (2020-04-27)
- 2.0.34 (2020-01-29)
- 2.0.33 (2019-02-09)
- 2.0.32 (2018-10-05)
- 2.0.31 (2018-10-05)
- 2.0.30 (2018-06-23)
- 2.0.29 (2017-10-21)
- 2.0.28 (2017-08-01)
- 2.0.27 (2017-03-15)
- 2.0.26 (2017-03-05)
- 2.0.25 (2017-02-05)
- 2.0.24 (2016-12-16)
- 2.0.23 (2016-07-21)
- 2.0.22 (2016-07-21)
- 2.0.21 (2016-04-12)
- 2.0.20 (2015-11-03)
- 2.0.19 (2015-11-01)
- 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)
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.6
- 1.3.5
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2
- 1.1
- 1.0.2
- 1.0
- 0.9
License¶
Copyright (c) 2010 Ian Bicking and Contributors
Permission is hereby granted, free of charge, to any person obtaininga copy of this software and associated documentation files (the"Software"), to deal in the Software without restriction, includingwithout limitation the rights to use, copy, modify, merge, publish,distribute, sublicense, and/or sell copies of the Software, and topermit persons to whom the Software is furnished to do so, subject tothe following conditions:
The above copyright notice and this permission notice shall beincluded 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 OFMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE ANDNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BELIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTIONOF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTIONWITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.