gaetk2.resttestlib - Simple Acceptance Tests¶
This module allows you to run simple non-interactive tasks against an installed Version of your application. We found that it helps to catch most simple programming errors and regressions prior to production deployment.
Simple tests look like this:
from gaetk2.resttestlib import create_testclient_from_cli
client = create_testclient_from_cli('myserver.appspot.com')
client.GET('/_ah/warmup').responds_http_status(200)
client.run_checks(max_workers=4)
print len(client.responses), "URLs tested"
sys.exit(client.errors)
This uses the low-level Response
interface. But usually you will work with the TestClient.check()
family of functions. Check can handle more than one URL at once:
client.check(
'/mk/pay/start/a6LP3L',
'/mk/pay/paypal/init/a6LP3L'
)
Based on file extension we check not only the content type, but also that the response is well formed - at least to a certain degree:
client.check(
'/k/SC10001/artikel',
'/api/marketsuche.json'
'/k/SC10001/artikel.csv',
'/k/SC10001/artikel.html',
'/k/SC10001/artikel.xml'
)
TestClient.check_redirect()
takes a list of sources and destinations
and ensures that the server redirects to the desired destination:
client.check_redirect(
dict(url='/artnr/73005/', to='/artnr/73000/'),
dict(url='/artnr/73000/', to='/artnr/73000/01/'),
)
The framework is meant to check for fine grained access controls via
HTTP-Basic-Auth. You can provide a list of handle=username:password
pairs during instantiation or via the command line. You can then refer to
them in your checks the the auth
parameter:
users = [
'usera=CK101:FNYBMAMPVC6EU',
'userb=u1001:TEABPVPGPVGBFE',
'admin=u2001:LQASNAJC6GUUP4VY',
'inactiveuser=u22730o:MATLEU4BJA756']
client = create_testclient_from_cli('myserver.appspot.com', users)
client.check(
'/pay/start/testingClassic',
'/mk/pay/paypal/init/testingMarket',
auth='usera')
client.check_redirect(dict(url='/', to='/inactive.html'), auth='inactiveuser')
One of the main uses of resttestlib is to check that certain resources are allowed for some users and denied for others:
client.check_allowdeny(
'/k/SC10001/auftraege',
allow=['usera', 'admin'],
deny=['userb', None]
)
The special user None
means unauthenticated.
Describe how this is part of the general test and deployment strategy.
Module contents¶
-
gaetk2.resttestlib.
create_testclient_from_cli
(default_hostname, users)[source]¶ Creates a Testclient with it’s arguments from the Commandline.
the CLI understands the options, –hostname, –credentials-user, their default values are taken from this functions args
default_hostname: hostname, on wich to run tests, if none is provided via CLI
returns a TestClient
-
class
gaetk2.resttestlib.
TestClient
(host, users, debug=False)[source]¶ Hilfsklasse zum Ausfuehren von HTTP-Requests im Rahmen von Tests.
-
add_credentials
(auth, creds)[source]¶ Stellt dem Client credentials zur Verfügung, die in GET genutzt werden können.
auth: key der Credentials creds: HTTP-Credentials in der Form ‘username:password’
-
GET
(path, auth=None, accept=None, headers={}, **kwargs)[source]¶ Führt einen HTTP-GET auf den gegebenen [path] aus. Nutzt dabei ggf. die credentials zu [auth] und [accept].
-
errors
¶ Anzahl der fehlgeschlagenen Zusicherungen, die für Anfragen dieses Clients gefroffen wurden.
-
-
class
gaetk2.resttestlib.
Response
(client, method, url, status, headers, content, duration, response)[source]¶ Repräsentiert das Ergebnis einer REST-Anfrage. Mittels responds_* koennen zusicherungen geprueft werden:
r.responds_http_status(200) r._responds_html()
-
responds_with_content_location
(expected_location)[source]¶ Sichert zu, dass die Antwort einen location-header hat.
-
responds_http_status
(expected_status)[source]¶ sichert zu, dass mit dem gegebenen HTTP-status geantwortet wurde.
-