不在范围内:尝试创建单元测试时类型构造函数或 class ‘Test.Framework.TestSuite’

Not in scope: type constructor or class ‘Test.Framework.TestSuite’ when trying to create unit tests

我正在 Haskell 中编写一个小型库,并希望对其进行测试。为了测试我打算使用HFT,整个项目由堆栈管理。 stack test 由于某种原因失败,输出如下:

[1 of 2] Compiling Main             ( test/Ini.hs, .stack-work/dist/x86_64-linux-tinfo6/Cabal-2.0.1.0/build/ini-tests/ini-tests-tmp/Main.o )                                                                      │····························
[2 of 2] Compiling Paths_schemer    ( .stack-work/dist/x86_64-linux-tinfo6/Cabal-2.0.1.0/build/ini-tests/autogen/Paths_schemer.hs, .stack-work/dist/x86_64-linux-tinfo6/Cabal-2.0.1.0/build/ini-tests/ini-tests-tm│····························
p/Paths_schemer.o )                                                                                                                                                                                               │····························
                                                                                                                                                                                                                  │····························
/stuff/projects/schemer/.stack-work/dist/x86_64-linux-tinfo6/Cabal-2.0.1.0/build/ini-tests/autogen/Paths_schemer.hs:54:39: error:                                                                                 │····························
    Not in scope: type constructor or class ‘Test.Framework.TestSuite’                                                                                                                                            │····························
    No module named ‘Test.Framework’ is imported.                                                                                                                                                                 │····························
                                                                                                                                                                                                                  │····························
/stuff/projects/schemer/.stack-work/dist/x86_64-linux-tinfo6/Cabal-2.0.1.0/build/ini-tests/autogen/Paths_schemer.hs:55:38: error:                                                                                 │····························
    Not in scope: ‘Test.Framework.makeTestSuite’                                                                                                                                                                  │····························
    No module named ‘Test.Framework’ is imported.                                                                                                                                                                 │····························

我的 Ini.hs 稍后将包含测试的文件非常简单,只是

import Test.Framework

main :: IO ()
main = htfMain htf_thisModulesTests

我的 package.yaml

name:                schemer
version:             0.1.0.0
github:              "mpevnev/schemer"
license:             BSD3
author:              "Michail Pevnev"
maintainer:          "mpevnev@gmail.com"
copyright:           ""

extra-source-files: []

# Metadata used when publishing your package
# synopsis:            Short description of your package
# category:            Web

# To avoid duplicated efforts in documentation and dealing with the
# complications of embedding Haddock markup inside cabal files, it is
# common to point users to the README.md file.
description:         Please see the README on GitHub at <https://github.com/mpevnev/schemer#readme>

dependencies:
- attoparsec
- base >= 4.7 && < 5
- text


library:
  source-dirs: src

tests:
  ini-tests:
    main:                Ini.hs
    source-dirs:         test
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    - -F
    - -pgmF htfpp
    dependencies:
    - schemer
    - text
    - HTF

这是自动生成的 schemer.cabal:

-- This file has been generated from package.yaml by hpack version 0.28.2.
--
-- see: https://github.com/sol/hpack
--
-- hash: 17ae623236b8f5b101f56373c975656e898efa7506acb143db7375f229509a79

name:           schemer
version:        0.1.0.0
description:    Please see the README on GitHub at <https://github.com/mpevnev/schemer#readme>
homepage:       https://github.com/mpevnev/schemer#readme
bug-reports:    https://github.com/mpevnev/schemer/issues
author:         Michail Pevnev
maintainer:     mpevnev@gmail.com
license:        BSD3
license-file:   LICENSE
build-type:     Simple
cabal-version:  >= 1.10

source-repository head
  type: git
  location: https://github.com/mpevnev/schemer

library
  exposed-modules:
      Control.Scheme.Ini
      Control.Schemer
  other-modules:
      Paths_schemer
  hs-source-dirs:
      src
  build-depends:
      attoparsec
    , base >=4.7 && <5
    , text
  default-language: Haskell2010

test-suite ini-tests
  type: exitcode-stdio-1.0
  main-is: Ini.hs
  other-modules:
      Paths_schemer
  hs-source-dirs:
      test
  ghc-options: -threaded -rtsopts -with-rtsopts=-N -F -pgmF htfpp
  build-depends:
      HTF
    , attoparsec
    , base >=4.7 && <5
    , schemer
    , text
  default-language: Haskell2010

我不确定哪里出了问题,这个 Paths_schemer 是什么东西。感谢帮助。

选项-F -pgmF htfpp全局开启。这会将 HTF 预处理器应用于测试套件的所有文件,包括自动生成的 Paths_schemer.

更具可扩展性的解决方案是仅在导入 Test.Framework 的文件上启用预处理器,在每个文件中使用 OPTIONS_GHC pragma:

{-# OPTIONS_GHC -F -pgmF htfpp #-}

另一种方法是在 package.yaml 的测试套件部分设置 other-modules: [] 以避免生成 Paths_schemer,尽管这只能解决这个模块的问题。