Here is an example test case that tests a login page for a simple web application:
|( "Users can log in via the login page"|
|; The test database starts off empty: we need a new user:|
|(let ([user (make-user #:username "dave" #:password "password")]|
|; Save the user to the database:|
|; Open the login page in the browser:|
|; Fill in the login form in the browser:|
|( ( 'username-field) "dave")|
|( ( 'password-field) "password")|
|; Click 'Submit' and wait for the page to refresh:|
|; Check the page title is 'Welcome, dave!'|
|( () "Welcome, dave!")|
|; Check the login was recorded:|
|( > (user-last-login user) now)))|
The code in this example comes from three sources:
Delirium tests are defined on the server, so they have access to all the library code used in the web application being tested. The make-user, save! and user-last-login in the example are all imported from the application itself.
Delirium is built on top of Schemeunit, which provides a rich testing framework. The , and forms in the example are standard procedures and macros from SchemeUnit.
Note that, like Schemeunit tests, Delirium tests are all fully fledged Scheme code: you can use all the abstractions that you would expect including comprehensions, higher order procedures and macros.
Delirium runs on top of the PLT Web Server. This allows it to maintain a REPL-style communication with the web browser using continuations:
test code is run until a browser API call is encountered.
the result of the command is sent back to the server as a block of JSON.
the JSON is converted into a Scheme value, which is returned by the API call.
more tests are run until the next browser API call is encountered...
Delirium is typically used to test PLT web applications. These applications are set up so they can be run in two different modes:
Production mode bypasses all of the Delirium setup and simply runs the application.
Test mode runs the application with whatever external resources (databases, configuration files, user credentials) are needed for testing, and sets up a special test servlet that runs Delirium.
Here is an example web application written using web-server/servlet-env and Delirium:
|; -> response|
|(define (start request)|
|; Application code goes here...)|
|( "Delirium tests"|
|; Test code goes here...))|
|; -> thunk|
|; -> thunk|
|( start tests))|
In this example, run-production runs the regular application on port 8000 using the PLT web server’s built-in serve/servlet produre. run-tests runs the same application using the wrapper procedure. configures the web server to run the application as normal except for the following caveats:
the URL /test is mapped to a Delirium test page;