0

Yesod provides the yesod test tool. A test is a value of type Spec which can be executed by hspec.

The scaffolding comes with a predefined spec in tests/HomeTest.hs which is explicitly called in tests/main.hs, the file that is apparently compiled and executed when you run yesod test. I assume you are supposed to manually add all your specs to main.hs as you create them, although perhaps there is a way to use hspec's automated test discovery.

This is great for regression testing, but what about test-driven development? What is the proper way to write a single test and then run it repeatedly during development? The help text for yesod test suggests that there is no way to do this:

$ yesod test --help
Usage: yesod test
  Build and run the integration tests

Running a spec file directly doesn't work (and I suppose you wouldn't expect it to, since it doesn't contain a main definition):

$ runhaskell tests/HomeTest.hs
tests/HomeTest.hs:4:8:
    Could not find module ‘TestImport’
    Use -v to see a list of the files searched for.

$ cabal run tests/HomeTest.hs
cabal: Cannot build the executable 'myproject' because the component is marked as disabled in the .cabal file.

I'm not sure why my project's executable is disabled. Should I change that? Or should I create a temporary copy of main.hs and comment out all the other tests? What's the cleanest solution?

sjy
  • 2,702
  • 1
  • 21
  • 22
  • It's the same as with every compiled lang.: you will have to compile and run the test - yesod test is fine for this. Some people set up a `rake` environment to run the test automatically but TDD and the Web are non-trivial no matter which lang. - concerning your last remark: as far as I understand/practice TDD you should always run your complete test-suite anyway. – Random Dev Sep 24 '14 at 08:51
  • It seems unnecessarily time-consuming to run your entire test suite every time you want to check whether your new code passes the new test you have written. – sjy Sep 24 '14 at 09:35
  • yep ... but when you refactor you will want the complete suit. I don't know of any Haskell tools that try to do this in smaller parts - if someone knows anything let me know (in .net NCrunch and co comes to mind) - anyway I think most do their small-step tests in GHCi anyway (I do) and only have integration/acceptance tests in their test-suite – Random Dev Sep 24 '14 at 10:02
  • That makes sense; maybe I just need to get more fluent with GHCi. Since you asked about Haskell tools, though: [hspec](http://hspec.github.io/running-specs.html) allows you to run tests matching a certain pattern with `runhaskell Spec.hs -m `. – sjy Sep 24 '14 at 12:49
  • thanks I indeed did not know that :D ... but it would not help - the tools I was refering to look at what changed in your code and figure (or try to figure) out which tests are affected and only run those - I don't know anything similar in Haskell – Random Dev Sep 24 '14 at 13:23
  • There is a [Guard plugin for Haskell (Hspec)](http://rubygems.org/gems/guard-haskell). – Simon Hengel Nov 01 '14 at 05:34

0 Answers0