yesod-test: integration testing for WAI/Yesod Applications

[ library, mit, testing, web, yesod ] [ Propose Tags ] [ Report a vulnerability ]

API docs and the README are available at http://www.stackage.org/package/yesod-test


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1, 0.2.0, 0.2.0.1, 0.2.0.2, 0.2.0.3, 0.2.0.4, 0.2.0.5, 0.2.0.6, 0.2.1, 0.3.0, 0.3.0.1, 0.3.1, 0.3.1.1, 0.3.2, 0.3.3, 0.3.3.1, 0.3.3.2, 0.3.4, 0.3.5, 1.2.0, 1.2.1, 1.2.1.1, 1.2.1.2, 1.2.1.3, 1.2.1.4, 1.2.1.5, 1.2.2, 1.2.3, 1.2.3.1, 1.2.3.2, 1.2.4, 1.2.5, 1.2.6, 1.4.0, 1.4.0.1, 1.4.0.2, 1.4.0.3, 1.4.1, 1.4.1.1, 1.4.2, 1.4.2.1, 1.4.2.2, 1.4.3, 1.4.3.1, 1.4.4, 1.5, 1.5.0.1, 1.5.1.0, 1.5.1.1, 1.5.2, 1.5.3, 1.5.4, 1.5.4.1, 1.5.5, 1.5.6, 1.5.7, 1.5.8, 1.5.9, 1.5.9.1, 1.6.0, 1.6.1, 1.6.2, 1.6.3, 1.6.4, 1.6.5, 1.6.5.1, 1.6.6, 1.6.6.1, 1.6.6.2, 1.6.7, 1.6.8, 1.6.9, 1.6.9.1, 1.6.10, 1.6.11, 1.6.12, 1.6.13, 1.6.14, 1.6.15, 1.6.16, 1.6.19
Change log ChangeLog.md
Dependencies aeson, attoparsec (>=0.10), base (>=4.10 && <5), blaze-builder, blaze-html (>=0.5), blaze-markup, bytestring (>=0.9), case-insensitive (>=0.2), conduit, containers, cookie, hspec-core (>=2 && <3), html-conduit (>=0.1), http-types (>=0.7), HUnit (>=1.2), memory, mtl (>=2.0.0), network (>=2.2), pretty-show (>=1.6), text, time, transformers (>=0.2.2), wai (>=3.0), wai-extra, xml-conduit (>=1.0), xml-types (>=0.3), yesod-core (>=1.6.17) [details]
License MIT
Author Nubis <nubis@woobiz.com.ar>
Maintainer Michael Snoyman, Greg Weber, Nubis <nubis@woobiz.com.ar>
Category Web, Yesod, Testing
Home page http://www.yesodweb.com
Source repo head: git clone git://github.com/yesodweb/yesod.git
Uploaded by psibi at 2024-10-20T05:54:16Z
Distributions Arch:1.6.16, Debian:1.6.10, Fedora:1.6.15, FreeBSD:1.4.3.1, LTSHaskell:1.6.16, NixOS:1.6.16, Stackage:1.6.19
Reverse Dependencies 6 direct, 3 indirect [details]
Downloads 73455 total (161 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2024-10-20 [all 1 reports]

Readme for yesod-test-1.6.19

[back to package description]

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) -- 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.
          byLabelExact "Email:" "gustavo@cerati.com"
          byLabelExact "Password:" "secret"
          byLabelExact "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)