Simple website testing with Snakked Subscribe Pub Share

Kept needing simple unit testing for my little website project and I ended up writing a simple helper class: UrlTester, part of the Snakked project.

It has 4 primitives to test responses to URLs, which, together with the snakking capability, covers most of the tests I needed.

The basic constructs are:

  • URL sok TEXT - URL is accessible and the response contains the TEXT
  • URL snok TEXT - URL is accessible but the response does NOT contain the TEXT
  • URL.s400 - URL is not accessible, whatever the reason (the response code is not 200)

These consitute should test cases in scalatest, allowing you to easily put together flatspecs:

    class SampleTestWiki extends FlatSpec with ShouldMatchers with razie.UrlTester {
      // needs a host/port to target in this test
      implicit val hostport = "http://localhost:9000"
      val (u,p) = ("joe", "password")
      // best way is to call the test from a web page and inject the username/pass used to login to the current html page, that way you avoid hard-coding passwords for test.

      // home page visible - also contains the text "home"
      "/" sok "home"

      // administration pages not reachable without login
      "/administration".s400

      //  "special admin topics" should "not be listed" in {
      "/wiki/list/Special" snok "urlmap"

      // anyone can see a blog but not edit it
      "/wiki/Enduro_Blog" sok "dirt bike"
      "/wikie/edit/Enduro_Blog".s400

      // joe can edit his note
      ("/wikie/edit/Joe's_private_note", u, p) sok "edit"
    }

Note that basic http authentication is supported easily in the form (URL, user, password).

Forms

Combined with the snakking, you can easily test for form submission as well:

      val form = Map (
          "label" -> ("Joe Private Note "+System.currentTimeMillis),
          "markup" -> "md",
          "content" -> "hehe",
          "visibility" -> "Public",
          "wvis" -> "Private",
          "tags" -> "note")

      "/wikie/save/Note:Joe_Private_Note_3".url.basic(u,p).form(form) sok "Private"

Performance testing

You can easily do basic performance testing, hitting your site from many threads

    class SampleTestPerf extends FlatSpec with ShouldMatchers with razie.UrlTester {
      implicit val hostport = "http://localhost:9000"

      "site" should "be fast" in {
        razie.Threads.forkjoin(0 to 100) { i =>
          ((0 to 10) map { x => "/".wget contains "home" }).exists(identity)
        }.exists(p => !p.isDefined || !p.get) === true
      }
    }

Matcher versus test case

The 3 test methods mentioned above produce should test cases. There are also equivalent methods that start with an e instead of s, which create just the matchers instead. The issue with that is the test names - you can play with it to see what I mean.

      // note how you use the 'e' instead of 's' inside a test
      "basic auth" should "fail on wrong user/pass" in {
        ("/wikie/edit/Joe's_private_note", "x", p).e400
        ("/wikie/edit/Joe's_private_note", u, "x").e400
      }

See there and more examples in SampleWebTest.scala.

Enhancements

If this is useful, let me know and we can easily enhance it:

  • use better scalatest matchers syntax
  • test for response codes (useful for REST APIs) etc.
  • check an xpath in the response (buttons/forms/html tags etc)
  • check a regular expression

If you have any ideas on a better syntax or similar tools, please send work / comment below.


Was this useful?    

By: Razie | 2013-05-16 .. 2016-05-11 | Tags: post , scala , programming


See more in: Cool Scala Subscribe

Viewed 1416 times ( | History | Print ) this page.

You need to log in to post a comment!