When i google for how to integrate unit tests with cabal files, i either find
http://www.haskell.org/haskellwiki/How_to_write_a_Haskell_program which does not seem to describe the integration of HUnit/QuickCheck with the Cabal file
or i see messages like "wait for Cabal x.y which will support cabal test" but i can not find any documentation for this either
How would you run all unit test using cabal (for example everytime i do a "cabal build") today?
Make sure you have the latest version of Cabal and cabal-install installed.
Have a test-suite section in your .cabal file. See this section of cabal's documentation for an explanation of how to write a test-suite section in your Cabal file and this section for instructions on how to run it.
I've been using the built-in test support for some time and it has saved me from having to maintain fragile Makefiles just for my tests. There are still some rough edges in the command line output of cabal test, but they have been fixed in HEAD so in the next Cabal/cabal-install release everything should be very smooth.
Related
Running stack test --coverage generates a nice HTML report showing what lines your test suite covers. How can I achieve the same thing using cabal new-test?
I'm able to pass --enable-coverage to generate a .tix file but I'm not sure what to run on the .tix file to generate the HTML report. I'm pretty sure it involves hpc but I haven't been able to work out the right command.
I have the standard Cabal configuration of my application being a library, with a test-suite for that library.
It appears it's as easy as passing --enable-coverage to cabal new-test. I had previously been running tests with cabal new-run test:test to workaround some limitations of new-test (e.g. lacking streaming and colors), so the fix is to use new-test instead of new-run.
Cabal 3.6 should be able to generate the HPC report. There is one caveat; this error may appear:
Error:
Internal libraries only supported with per-component builds.
Per-component builds were disabled because program coverage is enabled
https://github.com/haskell/cabal/issues/6440
To avoid the error, add to cabal.project:
package *
coverage: True
library-coverage: True
then cabal test (without --enable-coverage). The report should be somewhere in dist-newstyle.
cabal test will compile the test suite executable and then run it. However, it removes the console colouring (because it logs the result). I'd like to use cabal to build the executable and then run it from a script, but I can't figure out how to just build the executable.
If you cabal configure --enable-tests, then cabal build will build not just the library/executables, but also the test suites. You can also build individual test suites by name, saying cabal build name-of-test-suite.
You can run them manually from the appropriate subdirectories under dist/build if you don't want to use cabal test.
Furthermore, if it's e.g. a tasty test suite, you may be able to get color output by saying something like
cabal test --show-details=always --test-option=--color --test-option=always
You can also try --show-details=streaming. I don't know how robust this is, though, and whether it works may also depend on the platform you are on.
I'm following the GettingStarted guide and when I run 'cabal test' and got this message:
Deprecated: "Please use the new testing interface instead!"
What is the new testing interface?
This message is actually from cabal, not darcs, and is referring to the cabal testing interface.
Most Haskell projects include a file Setup.hs or Setup.lhs that cabal runs in order to build the project. Usually it's sufficient to just call the default Distribution.Simple.defaultMain, but UserHooks in the cabal library defines many hooks to extend cabal's default behavior. One of those hooks is runTests, and this used to be the only way to tell Cabal about the package's test suite. However runTests is now deprecated in favor of the test suite section of the cabal package file. darcs's Setup.lhs file overrides the runTests hook, which triggers the deprecation warning.
As a darcs user, you don't need to worry about this message. If you're interested in contributing to the darcs project, fixing this should be relatively straightforward and I'm sure the patches would be gratefully accepted.
When using cabal install --enable-tests --enable-library-coverage to generate code coverage reports it will also generate code coverage reports for the testcode. Is there an option for cabal to let hpc ignore testcode?
I'd be pleasantly surprised if cabal yet has an option for this. Getting HPC to ignore things is (or perhaps used to be) moderately complicated as this example shows: http://www.haskell.org/haskellwiki/Haskell_program_coverage#Example
I love the snap framework but I hate running 'cabal install' with each iteration (minor code change) I want to try out.
Is there an alternative for rapid iteration?
Start with
cabal install --reinstall -fhint snap
Then, for your project:
cabal clean
cabal configure -fdevelopment
cabal build
./dist/build/projname/projname
You shouldn't ever use cabal install for binaries that you don't want to be able to execute from arbitrary locations, anyway. You should be using cabal build for things you only want to run locally.
You will need to run cabal build and start the program again when you change Main.hs or your project's .cabal file.
If you have any further questions, comment - I'm the guy who implemented this functionality for
Snap.
Yesod provides yesod devel which automatically reloads code changes. I am not aware of a comparable capability in snap, but it is highly likely that they can reuse much of the Yesod code that does this.
Given the existence of Snap.Loader.Devel I'm guessing they might already provide something like what you are asking for, but I can't find the documentation on how to use it. The FAQ question How do I run my app in development mode still requires a cabal install; it's unclear from the docs whether you only need to do this once, or every time the code changes.