MyNixOS website logo
Description

integration testing for WAI/Yesod Applications.

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

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:" "[email protected]"
          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)
Metadata

Version

1.6.16

License

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows