yesod-test
Pragmatic integration tests for haskell web applications using WAI and optionally a database (Persistent).
Its main goal is to encourage integration and system testing of web applications by making everything easy to test.
Your tests are like browser sessions that keep track of cookies and the last
visited page. You can perform assertions on the content of HTML responses
using CSS selectors.
You can also easily build requests using forms present in the current page.
This is very useful for testing web applications built in yesod for example,
where your forms may have field names generated by the framework or a randomly
generated CSRF "_token" field.
Your database is also directly available so you can use runDB to set up
backend pre-conditions, or to assert that your session is having the desired effect.
The testing facilities behind the scenes are HSpec (on top of HUnit).
The code sample below covers the core concepts of yesod-test. Check out the
yesod-scaffolding for usage in a complete application.
spec :: Spec
spec = withApp $ do
describe "Basic navigation and assertions" $ do
it "Gets a page that has a form, with auto generated fields and token" $ do
get ("url/to/page/with/form" :: Text)
statusIs 200
bodyContains "Hello Person"
htmlCount "form .main" 1
htmlAllContain "h1#mainTitle" "Sign Up Now!"
request $ do
setMethod "POST"
setUrl SignupR
addToken
byLabel "Email:" "gustavo@cerati.com"
byLabel "Password:" "secret"
byLabel "Confirm:" "secret"
it "Sends another form, this one has a file" $ do
request $ do
setMethod "POST"
setUrl ("url/to/post/file/to" :: Text)
addFile "file_field_name" "path/to/local/file" "image/jpeg"
addPostParam "answer" "42"
statusIs 302
describe "Database access" $ do
it "selects the list" $ do
msgs <- runDB $ selectList ([] :: [Filter Message]) []
assertEqual "One Message in the DB" 1 (length msgs)