File: README.txt

package info (click to toggle)
python-webunit 1%3A1.3.8-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 156 kB
  • ctags: 211
  • sloc: python: 1,502; makefile: 13
file content (357 lines) | stat: -rw-r--r-- 12,317 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
Web unit testing  (the concise help)
====================================

To run the current demo tests, use::

   ./run_tests demo


Installation
------------

Install the webunit libraries with::

   python setup.py install

Then make a directory like the demo one in your application with tests in
it, and invoke with::

   ./run_tests <test>

<test>
   Run the test named - just like unittest.py.


Configuration
-------------

We've found it useful to be able to test different servers with the same test
suite. To this end, we've got a simple configuration file setup. See the doc
string in webunit.config


Errors
------
If a request fails with an incorrect response code (the default valid
response codes are 200, 301 and 302) then the result body fetched from the
server will be appended to the logfile for that server.


Making requests - the WebTestCase class
---------------------------------------
The WebTestCase is best thought of as a web browser with some added features.
To truly emulate a web browser (single-threaded at present) use the "page"
methods below. For more fine-grained control and testing, use the other
methods.

The WebTestCase objects have a number of attributes (see `Setting up fetch
defaults`_):

1. protocol, server, port -- these default to http://localhost:80 and are used
   when a relative fetch is performed. Most tests set these vars up with
   setServer in the setUp method and then use relative URLs.
2. authinfo -- basic authentication information. Use setBasicAuth to set and
   clearBasicAuth to clear.
3. cookies -- the test's store of cookies
4. images -- cache of images fetched

There are two modes of retrieval using two HTTP methods:

**fetch** mode
 This is the default mode, and just fetches the HTML of a page.
**page** mode
 This is a more browser-like mode. It follows redirections and loads
 images and stylesheets required to render the page.
**GET** method
 This is the default HTTP method used.
**POST** method
 All GET methods have a HTTP POST analog, usually just with ``post``
 prefixed.

Now the actual calls that are possible:

assertCode(self, url, code=None, \*\*kw)
  Perform a HTTP GET and assert that the return code from the
  server one of the indicated codes.

get = assertCode(self, url, code=None, \*\*kw)
  Just an alias

assertContent(self, url, content, code=None, \*\*kw)
  Perform a HTTP GET and assert that the data returned from the
  server contains the indicated content string.
assertNotContent(self, url, content, code=None, \*\*kw)
  Perform a HTTP GET and assert that the data returned from the
  server contains the indicated content string.

getAssertCode = assertCode(self, url, code=None, \*\*kw)
  Just an alias

getAssertContent = assertContent(self, url, content, code=None, \*\*kw)
  Just an alias

getAssertNotContent = assertNotContent(self, url, content, code=None, \*\*kw)
  Just an alias

page(self, url, code=None, \*\*kw)
  Perform a HTTP GET using the specified URL and then retrieve all
  image and linked stylesheet components for the resulting HTML page.

post(self, url, params, code=None, \*\*kw)
  Perform a HTTP POST using the specified URL and form parameters.

postAssertCode(self, url, params, code=None, \*\*kw)
  Perform a HTTP POST and assert that the return code from the
  server is one of the indicated codes.

postAssertContent(self, url, params, content, code=None, \*\*kw)
  Perform a HTTP POST and assert that the data returned from the
  server contains the indicated content string.

postAssertNotContent(self, url, params, content, code=None, \*\*kw)
  Perform a HTTP POST and assert that the data returned from the
  server doesn't contain the indicated content string.

postPage(self, url, params, code=None, \*\*kw)
  Perform a HTTP POST using the specified URL and form parameters
  and then retrieve all image and linked stylesheet components for the
  resulting HTML page.

All of these methods eventually call fetch() - the additional \*\*kw are
passed directly to the fetch method:

fetch(self, url, postdata=None, server=None, port=None, protocol=None, ok_codes=None)
      Run a single test request to the indicated url. Use the POST data
      if supplied. If the URL is a fully-qualified one (ie. has a server
      and protocol) then that overrides the session's default, but may
      be further overridden by the method arguments.
      Raises failureException if the returned data contains any of the
      strings indicated to be Error Content.
      Returns a HTTPReponse object wrapping the response from the server.

HTTP Response objects
---------------------

The HTTPResponse objects hold all the infomation about the server's response
to the request. This information includes:

1. protocol, server, port, url - the request server and URL
2. code, message, headers - the information returned by httplib.HTTP.getreply()
3. body - the response body returned by httplib.HTTP.getfile()

Additionally, the object has several methods:

getDOM(self)
  Get a DOM for this page. See the SimpleDOM_ instructions for details

extractForm(self, path=[], include_submit=0, include_button=0)
  Extract a form (as a dictionary) from this page.
  
  The "path" is a list of 2-tuples ``('element name', index)`` to follow
  to find the form. So::

   <html><head>..</head><body>
    <p><form>...</form></p>
    <p><form>...</form></p>
   </body></html>
  
  To extract the second form, any of these could be used::

   [('html',0), ('body',0), ('p',1), ('form',0)]
   [('form',1)]
   [('p',1)]


HTTPResponse objects are also able to fetch using WebTestCase methods. They
define additional methods:

getForm(self, formnum, getmethod, postargs, \*args)
  Given this page, extract the "formnum"th form from it, fill the
  form with the "postargs" and post back to the server using the
  "getmethod" with additional "args".
    
  NOTE: the form submission will include any "default" values from
  the form extracted from this page. To "remove" a value from the
  form, just pass a value None for the elementn and it will be
  removed from the form submission.
    
  example WebTestCase::

      page = self.get('/foo')
      page.postForm(0, self.post, {'name': 'blahblah',
              'password': 'foo'})
    
  or the slightly more complex::

      page = self.get('/foo')
      page.postForm(0, self.postAssertContent, {'name': 'blahblah',
              'password': None}, 'password incorrect')


postForm(self, formnum, postmethod, postargs, \*args)
  As with getForm, only use a POST request.


Setting up fetch defaults
-------------------------

setServer(self, server, port)
  Set the server and port number to perform the HTTP requests to.

setBasicAuth(self, username, password)
  Set the Basic authentication information to the given username
  and password.

clearBasicAuth(self)
  Clear the current Basic authentication information

setAcceptCookies(self, accept=1)
  Indicate whether to accept cookies or not

clearCookies(self)
  Clear all currently received cookies


Auto-fail error content
-----------------------
You may have the fetcher automatically fail when receiving certain content
using:

registerErrorContent(self, content)
  Register the given string as content that should be considered a
  test failure (even though the response code is 200).

removeErrorContent(self, content)
  Remove the given string from the error content list.

clearErrorContent(self)
  Clear the current list of error content strings.



Cookies
-------
To test for cookies being sent _to_ a server, use:

registerExpectedCookie(self, cookie)
  Register a cookie name that we expect to send to the server.

removeExpectedCookie(self, cookie)
  Remove the given cookie from the list of cookies we expect to
  send to the server.

To test for cookies sent _from_ the server, access the cookies attribute of
the test harness. It is a dict of::

  cookies[host name][path][cookie name] = string


SimpleDOM
---------
Simple usage::

 >>> import SimpleDOM
 >>> parser = SimpleDOM.SimpleDOMParser()
 >>> parser.parseString("""<html><head><title>My Document</title></head>
 ... <body>
 ...  <p>This is a paragraph!!!</p>
 ...  <p>This is another para!!</p>
 ... </body>
 ... </html>""")
 >>> dom = parser.getDOM()
 >>> dom.getByName('p')
 [<SimpleDOMNode "p" {} (1 elements)>, <SimpleDOMNode "p" {} (1 elements)>]
 >>> dom.getByName('p')[0][0]
 'This is a paragraph!!!'
 >>> dom.getByName('title')[0][0]
 'My Document'


Form extraction example (see also the tests in the test/ directory of the
source)::

        # fetch the start page
        page = self.get('/ekit/home')
        page = page.postForm(1, self.postAssertCode, {'__ac_name': 'joebloggs',
            '__ac_password': 'foo'}, [302])
        # same as last fetch, but automatically follow the redirect
        page = page.postForm(1, self.page, {'__ac_name': 'joebloggs',
            '__ac_password': 'foo'})


Thanks
======

Thanks to everyone who's helped with this package, including supplying
hints, bug reports and (more importantly :) patches:

Gary Capell

Note that this list is nowhere near complete, as I've only just started
maintaining it. If you're miffed that you're not on it, just let me know!


License
=======
Copyright (c) 2003 Richard Jones (http://mechanicalcat.net/richard)

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.


This product includes software developed by Ekit.com Inc.

Copyright (c) 2002 ekit.com Inc (http://www.ekit-inc.com/)

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.


This product includes software developed by Bizar Software.

Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)

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.


This product includes software developed by Digital Creations for use in
the Z Object Publishing Environment (http://www.zope.org/).