Skip to content

Commit

Permalink
Add Reader monad transformer when running single test
Browse files Browse the repository at this point in the history
- used for including parent group names
- hence create appropriate "classname" for each "testcase". (turns out "classname" is an required field)
- in additions, add type signature to the `runTest` function to help understanding the application.
  • Loading branch information
haishengwu-okta committed Dec 21, 2016
1 parent 019c0cf commit a01df06
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions Test/Tasty/Runners/AntXML.hs
Expand Up @@ -12,7 +12,7 @@ module Test.Tasty.Runners.AntXML (antXMLRunner, AntXMLPath(..) ) where
import Numeric (showFFloat)
import Control.Applicative
import Control.Arrow (first)
import Control.Monad.Trans.Class (lift)
import Control.Monad.IO.Class (liftIO)
import Data.Maybe (fromMaybe)
import Data.Monoid (Monoid(..), Endo(..), Sum(..))
import Data.Proxy (Proxy(..))
Expand All @@ -25,8 +25,11 @@ import System.FilePath (takeDirectory)

import qualified Control.Concurrent.STM as STM
import qualified Control.Monad.State as State
import qualified Control.Monad.Reader as Reader
import qualified Data.Functor.Compose as Functor
import qualified Data.IntMap as IntMap
import qualified Test.Tasty as Tasty
import qualified Test.Tasty.Providers as Tasty
import qualified Test.Tasty.Options as Tasty
import qualified Test.Tasty.Runners as Tasty
import qualified Text.XML.Light as XML
Expand Down Expand Up @@ -75,17 +78,24 @@ antXMLRunner = Tasty.TestReporter optionDescription runner
timeDigits = 3
showTime time = showFFloat (Just timeDigits) time ""

runTest :: (Tasty.IsTest t)
=> Tasty.OptionSet
-> Tasty.TestName
-> t
-> Tasty.Traversal (Functor.Compose (Reader.ReaderT [String] (State.StateT IntMap.Key IO)) (Const Summary))
runTest _ testName _ = Tasty.Traversal $ Functor.Compose $ do
i <- State.get
groupNames <- Reader.ask

summary <- lift $ STM.atomically $ do
summary <- liftIO $ STM.atomically $ do
status <- STM.readTVar $
fromMaybe (error "Attempted to lookup test by index outside bounds") $
IntMap.lookup i statusMap

let testCaseAttributes time = map (uncurry XML.Attr . first XML.unqual)
[ ("name", testName)
, ("time", showTime time)
, ("classname", unwords groupNames)

This comment has been minimized.

Copy link
@liskin

liskin Mar 8, 2017

Contributor

Wouldn't joining groupNames using . make more sense than (a space)? Jenkins uses dots to build hierarchy and test-framework uses dots, too. I just switched our entire codebase from test-framework to tasty + tasty-ant-xml and test results in Jenkins are quite messy.

(Are there other consumers of junit xml that use something other than dots?)

This comment has been minimized.

Copy link
@haishengwu-okta

haishengwu-okta Mar 8, 2017

Author Contributor

sounds reasonable to me.
my special case is that we didnt show hierarchy and group name may have dots.
@liskin would you prefer an PR otherwise I'll do it in tomorrow.

This comment has been minimized.

Copy link
@liskin

liskin Mar 9, 2017

Contributor

I just came home somewhat drunk so it's not unlikely you'll get to this sooner than me. :-)

(But then, we might want to do this more thoroughly. For example deeply nested <testsuite> elements do not seem to be supported by https://wiki.jenkins-ci.org/display/JENKINS/xUnit+Plugin. With classname now being generated, perhaps outputting these nested structures isn't necessary at all. So, um, more testing with actual applications that consume that XML might be needed, is what I'm trying to say. :-))

Anyway, having this discussion in an issue or PR instead of some old soon to be forgotten commit is probably a good idea. :-)

This comment has been minimized.

Copy link
@haishengwu-okta

haishengwu-okta Mar 9, 2017

Author Contributor

hmm, looks like need an thorough fixing and actually more testing...mind open an issue maybe? @liskin

This comment has been minimized.

Copy link
@liskin

liskin Mar 9, 2017

Contributor

Turns out nesting of <testsuite> wasn't the problem, but missing tests="123" was. I fixed both issues here: #20

]

mkSummary contents =
Expand Down Expand Up @@ -120,7 +130,8 @@ antXMLRunner = Tasty.TestReporter optionDescription runner
Const summary <$ State.modify (+ 1)

runGroup groupName children = Tasty.Traversal $ Functor.Compose $ do
Const soFar <- Functor.getCompose $ Tasty.getTraversal children
Const soFar <- Reader.withReaderT (++ [groupName]) $ Functor.getCompose $ Tasty.getTraversal children

let grouped = appEndo (xmlRenderer soFar) $
XML.node (XML.unqual "testsuite") $
XML.Attr (XML.unqual "name") groupName
Expand All @@ -131,7 +142,7 @@ antXMLRunner = Tasty.TestReporter optionDescription runner

in do
(Const summary, tests) <-
flip State.runStateT 0 $ Functor.getCompose $ Tasty.getTraversal $
flip State.runStateT 0 $ flip Reader.runReaderT [] $ Functor.getCompose $ Tasty.getTraversal $
Tasty.foldTestTree
Tasty.trivialFold { Tasty.foldSingle = runTest, Tasty.foldGroup = runGroup }
options
Expand Down

0 comments on commit a01df06

Please sign in to comment.