Unit-Testing: Can module re-exports avoid having to “expose” all testable modules

前端 未结 1 1841
挽巷
挽巷 2020-12-18 15:59

TLDR: Is it possible to use module re-exports to avoid having \"expose\" all testable modules?

I\'ve used something similar to the Chris Done template for my Haskell

相关标签:
1条回答
  • 2020-12-18 16:08

    So in the end this is unavoidable. However, on reflection, it is also good behaviour. Since I'm creating a library, and the Haskell language can't control module visibility, I have to specify which modules are to be exported in Cabal.

    In order to function as tests, my tests should not have any additional access to an application that my library client does. Therefore it's acceptable that my tests can't access any modules that are also inaccessible to clients of my library.

    In practice however, there are some internal elements I will want to test which are not exposed. Therefore, I've split my test-suite in two, one internal, and the other external, which test these different aspects.

    test-suite ventureforth-test-external
      type:                exitcode-stdio-1.0
      hs-source-dirs:      test
      main-is:             Spec.hs
      build-depends:       base >= 4.7 && < 5,
                           ventureforth -any,
                           hspec -any
      ghc-options:         -Wall -Werror -threaded -rtsopts -with-rtsopts=-N
      default-language:    Haskell2010
    
    test-suite ventureforth-test-internal
      type:                exitcode-stdio-1.0
      hs-source-dirs:      test,src
      main-is:             Spec.hs
      build-depends:       base >= 4.7 && < 5,
                           hspec -any
      ghc-options:         -Wall -Werror -threaded -rtsopts -with-rtsopts=-N
      default-language:    Haskell2010
    

    The internal test has access to the source-code directly, and so doesn't import the library.

    Lastly as a second source of "internal" testing, I'm also using doctests, as described in the Haskeleton project setup guide

    Finally, for anyone else structuring a Haskell project, note that you can use the Haskell Init tool to create a skeleton project called my-project-name in a directory of the same name with all the testing bits and bobs set up and ready to go

    hi my-project-name \
        --repository git://github.com/tfausak/haskeleton.git \
        --package-name my-project-name \
        --module-name MyProjectName \
        --author Bryan Feeney \
        --email bryan.feeney@mailserver.com
    

    The repository flag gives a path to a template project. The Haskeleton template features unit-tests, documentation tests and benchmarks. Other project tempaltes, such as web-apps, can be found on the HI templates page. To install the Haskell Init tool, just type

    cabal install hi
    
    0 讨论(0)
提交回复
热议问题