当使用"堆栈测试"时,我的hspec测试输出没有着色

时间:2016-03-21 00:36:13

标签: unit-testing haskell haskell-stack

这是一件令人愤怒的事情,因为我已经构建了基于Hspec的测试套件,其中颜色都表现正常。但是在这个项目中,当我一次运行所有测试套件时,我无法显示颜色。

我的project.cabal设置如下:

test-suite unit
  type:               exitcode-stdio-1.0
  main-is:            SpecMain.hs
  hs-source-dirs:     tests/unit
  other-modules:      WikiSpec
  default-language:   Haskell2010
  ghc-options:        -Wall -fno-warn-orphans -threaded
  build-depends:      base                    >=4.6
  ...

test-suite integration
  type:               exitcode-stdio-1.0
  main-is:            SpecMain.hs
  hs-source-dirs:     tests/integration, webapp
  other-modules:      ApiSpec
  default-language:   Haskell2010
  ghc-options:        -Wall -fno-warn-orphans -threaded
  build-depends:      base                    >=4.6
  ...

然后我的SpecMain.hs文件(相同)包含:

{-# OPTIONS_GHC -F -pgmF hspec-discover #-}

因此,当我运行stack test时,我的所有测试都会运行,但输出没有着色。如果我运行stack build --file-watch --test,测试会运行,但如果有任何失败,那么所有输出都会显示为红色。最后,如果我运行stack test weblog:unitstack test weblog:integration,那么颜色最终会完全按照原样运行。标题为白色,通过测试为绿色,失败测试为红色,待定测试为黄色。

当我进行积极的开发时,我倾向于依赖stack build --file-watch --test,但我确实需要颜色才是正确的。

有没有人知道发生了什么,我如何解决这个问题,或者我需要提供哪些其他信息?

2 个答案:

答案 0 :(得分:4)

默认情况下,hspec仅在a terminal上显示输出时以及环境变量TERM不是"dumb"(或未设置)时才使用颜色。除非您将环境变量设置为"dumb",否则终端检测可能会发生变化。

无论哪种方式,stack build都允许您使用--test-arguments的测试套件参数,hspec解释几个命令行参数,包括--color--no-color它会覆盖默认行为。因此,您可以强制颜色:

stack test --file-watch --test-arguments "--color"

答案 1 :(得分:0)

Stack会在您为其提供多个程序包时使用您所看到的行为。通常,这是因为您在stack.yaml文件的软件包节中列出了多个位置。

最新版本的堆栈在自动生成的stack.yaml文件中提及以下内容:

# A package marked 'extra-dep: true' will only be built if demanded by a
# non-dependency (i.e. a user package), and its test suites and benchmarks
# will not be run. This is useful for tweaking upstream packages.

如果您将包中的所有位置标记为extra-dep,则堆栈将在测试时恢复其单包行为,并按预期显示您的彩色测试结果。