integration testing for WAI/Yesod Applications

Version on this page:1.6.9
LTS Haskell 22.29:1.6.16
Stackage Nightly 2024-07-13:1.6.16
Latest on Hackage:1.6.16

See all snapshots yesod-test appears in

MIT licensed by Nubis
Maintained by Michael Snoyman, Greg Weber, Nubis
This version can be pinned in stack with:yesod-test-1.6.9@sha256:bf7a3d003d83085befc5eaf72aa1d61f3f6a2951439c072d16db9d7b1ac5c418,2789

Module documentation for 1.6.9


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) -- Load a page.
        statusIs 200 -- Assert the status was success.

        bodyContains "Hello Person" -- Assert any part of the document contains some text.
        -- Perform CSS queries and assertions.
        htmlCount "form .main" 1 -- It matches 1 element.
        htmlAllContain "h1#mainTitle" "Sign Up Now!" -- All matches have some text.

        -- Performs the POST using the current page to extract field values:
        request $ do
          setMethod "POST"
          setUrl SignupR
          addToken -- Add the CSRF _token field with the currently shown value.

          -- Lookup field by the text on the labels pointing to them.
          byLabel "Email:" "[email protected]"
          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)
          -- You can easily add files, though you still need to provide the MIME type for them.
          addFile "file_field_name" "path/to/local/file" "image/jpeg"
          -- And of course you can add any field if you know its name.
          addPostParam "answer" "42"

        statusIs 302

    describe "Database access" $ do
      it "selects the list" $ do
        -- See the Yesod scaffolding for the runDB implementation
        msgs <- runDB $ selectList ([] :: [Filter Message]) []
        assertEqual "One Message in the DB" 1 (length msgs)


ChangeLog for yesod-test


Add requireJSONResponse function #1646


Add testModifySite function #1642


Add addBasicAuthHeader function #1632

addPostParam will now URL-encode keys and values to prevent corruption when special characters such as & are used #1617

  • Documentation fixes
  • Support for network 3


  • Add utility functions to modify cookies $1570

  • Make test suite build with GHC 8.6 #1561


bodyEquals prints out actual body in addition to expected body in failure msg #1525


Add yesodSpecWithSiteGeneratorAndArgument #1485


Add performMethod #1502


  • Add byLabel-related functions like byLabelContain #1482


  • Fix the build with base-4.11 (GHC 8.4).


  • Upgrade to yesod-core 1.6.0

  • Fixes a Haddock syntax error in 1.5.9 #1473


  • Add byLabelExact and related functions #1459


  • Added implicit parameter HasCallStack to assertions. #1421




  • Fix warnings

  • Compilation fix for GHC 7.8


  • yesod-test: add getLocation test helper. #1314


  • Added bodyNotContains #1271


  • Added assertEq, deprecated assertEqual #1259

  • Fix addToken_ needing a trailing space and allows multiples spaces in css selector.

  • Better error provenance for stuff invoking withResponse’ #1191

  • Fixed the application/x-www-form-urlencoded header being added to all requests, even those sending a binary POST body #1064
    • The application/x-www-form-urlencoded Content-Type header is now only added if key-value POST parameters are added
    • If no key-values pairs are added, or the request body is set with setRequestBody, no default Content-Type header is set


  • remove deprecated addNonce functions
  • You can now configure testing middleware

Configuring middleware makes it easy to add logging among other things. middleware is applied to the wai app before each test.

If you follow the yesod scaffold, you probably have a withApp function in TestImport.hs. This function should now return (foundation, middleware). id is an acceptable value for middleware.


test helpers for CRSF middleware such as addTokenFromCookie

  • Add addTokenFromCookie and addTokenFromCookieNamedToHeaderNamed, which support the new CSRF token middleware #1058
  • Add getRequestCookies, which returns the cookies from the most recent request #1058

  • Improved README


Provide Example instance for YesodExample.

Upgrade to hspec 2