setting TestPattern programmatically

13 views
Skip to first unread message

Andreas Reuleaux

unread,
Dec 15, 2014, 9:45:21 AM12/15/14
to haskel...@googlegroups.com
I am new to tasty, so please bear with me.

Having read some of the introductory docs I managed to
get some simple tests working from two different groups, say sortingU (U for
unit tests), and parsingU:


main = defaultMain tests
tests = testGroup "Tests" [sortingU, parsingU]

# ... etc - details omitted here

now, I can run this

* either from some kind of a command line (bash) script à la

runghc -package-db=.cabal-sandbox/x86_64-linux-ghc-7.6.3-packages.conf.d MyTests.hs "$@"

* or from within ghci:

>>> :l MyTests
>>> main

running from the command line gives me the comfort that I can choose
the test pattern, say

./tests.sh -p parsing

and only the tests from the parsing group will be run.

But working within ghci (reloading MyTests) is faster.
So I thought, I can programmatically run only those tests
that match my pattern, I thought somehow like this would do:

mainp pat = defaultMain $ adjustOption (\_ -> parseTestPattern pat) $ testGroup "Tests" [sortingU, parsingU]

but apparently this has no effect: still all of the tests are run.

What should I do instead?

Of course I could have different functions like so


mainSorting = defaultMain testsS
testsS = testGroup "Tests" [sortingU]


mainParsing = defaultMain testsP
testsP = testGroup "Tests" [parsingU]

But this is tedious. I would rather have the mainp function
above, that takes a single pattern argument.

I have found the article about custom options:
however, in this case I don't want to create a new option, but
just set the already existing pattern option.


Thanks in advance,
Andreas

Roman Cheplyaka

unread,
Dec 15, 2014, 10:39:14 AM12/15/14
to Andreas Reuleaux, haskel...@googlegroups.com
Hi Andreas,

The reason why you can't set the pattern this way is because pattern is
a "global" option, and adjustOption can only set "local" options (such
as the number of quickcheck tests etc.).

I agree this is not intuitive and I should find a way to improve it.

Here are some ideas to work around for now.

First, you can set it in ghci like this:

> :set args -p mypattern

Second, you can pass options through environment variables. So, for
instance, you could launch ghci like this:

TASTY_PATTERN=mypattern ghci MyTest.hs

You can also set the environment variable programmatically (so you could
write your mainp if you wanted, and if you don't mind its non-local
effects):

> :m +System.Environment
> setEnv "TASTY_PATTERN" "findShortestPrefix"

Andreas Reuleaux

unread,
Dec 15, 2014, 3:16:08 PM12/15/14
to haskel...@googlegroups.com
OK, thanks a lot for these explanations, so it's
not my fault or ignorance at least.

I tried the
setEnv "TASTY_PATTERN" "findShortestPrefix"
approach as suggested below:

but apparently setEnv is not in my System.Environment as of
ghc 7.6.3 / base-4.6.0.1, cf

http://downloads.haskell.org/~ghc/7.6.3/docs/html/libraries/base-4.6.0.1/System-Environment.html

but will be again for me in ghc 7.8.3 / base-4.7.0.1, cf

http://hackage.haskell.org/package/base-4.7.0.1/docs/System-Environment.html

(no idea why these things are changing), anyway I got it working
with a similar solution:


import System.Environment

main' pat = do
withArgs ["-p", pat] $ defaultMain $ testGroup "Tests" [sortingU, parsingU]


Thanks again,
Andreas
Reply all
Reply to author
Forward
0 new messages