User Tools

Site Tools


toolsetup:testframework

This is an old revision of the document!


Test framework setup

Preconditions / configuration

  • Installations:
  • Haskel project has been setup with stack
    • stack new <ProjectName>

Steps

  1. Adapt package.yaml by adding the packages “test-framework”, “test-framework-hunit”, and so on as dependencies below section “tests”, as in the code shown below.
    • code below tests:, and <ProjectName>-test::
      •     dependencies:
            - <ProjectName>
            - test-framework
            - test-framework-hunit
            - test-framework-quickcheck2
            - HUnit
            - QuickCheck
    • complete file of package.yaml
      • name:                <ProjectNAme>
        version:             ...
        github:              ...
        license:             ...
        author:              ...
        maintainer:          ...
        copyright:           ...
        
        extra-source-files:
        - ...
        
        ...
        
        description:         Please see the README on GitHub at <https://github.com/githubuser/Test1#readme>
        
        dependencies:
        - base >= 4.7 && < 5
        
        library:
          source-dirs: src
        
        executables:
          ...
        
        tests:
          <ProjectName>-test:
            main:                Spec.hs
            source-dirs:         test
            ghc-options:
            - -threaded
            - -rtsopts
            - -with-rtsopts=-N
            dependencies:
            - <ProjectName>
            - test-framework
            - test-framework-hunit
            - test-framework-quickcheck2
            - HUnit
            - QuickCheck
  2. On windows setups, in order to avoid problems with regex (see https://github.com/haskell-hvr/regex-posix/issues/4), we have to adapt the file stack.yaml.
    1. Add lib “regex-posix-clib-2.7” into section extra-deps.
    2. Add flag “_regex-posix-clib: true” and “regex-posix:” below section flags
    • # This file was automatically generated by 'stack init'
      #
      # Some commonly used options have been documented as comments in this file.
      
      ...
      
      resolver: ...
      
      # User packages to be built.
      
      ...
      
      packages:
      - .
      
      ...
      
      extra-deps:
        - regex-posix-clib-2.7
      
      # Override default flag values for local packages and extra-deps
      flags:
         regex-posix:
            _regex-posix-clib: true
      
      ...
      
      # compiler-check: newer-minor
  3. Implement the test in file “Spec.hs” that is below folder “test” of the project by importing the 5 packages “Test.Framework”, and so on as shown in the code below.
    • import Test.Framework
      import Test.Framework.Providers.HUnit
      import Test.Framework.Providers.QuickCheck2
      import Test.HUnit
      import Test.QuickCheck
      
      import qualified Lib as Lib
      
      main :: IO ()
      main = defaultMainWithOpts
             [ testCase "rev" testRev
             , testProperty "listRevRevId" propListRevRevId
             ] mempty
      
      testRev :: Assertion
      testRev = Lib.reverse2 [1, 2, 3] @?= [3, 2, 1]
      
      propListRevRevId :: [Int] -> Property
      propListRevRevId xs = not (null xs) ==> Lib.reverse2 (Lib.reverse2 xs) == xs
  4. Run stack test and it will build your tests and run the tests
    • Test1-0.1.0.0: unregistering (dependencies changed)
      Test1> configure (lib + exe + test)
      Configuring Test1-0.1.0.0...
      Test1> build (lib + exe + test)
      Preprocessing library for Test1-0.1.0.0..
      Building library for Test1-0.1.0.0..
      [1 of 2] Compiling Lib
      [2 of 2] Compiling Paths_Test1
      Preprocessing executable 'Test1-exe' for Test1-0.1.0.0..
      Building executable 'Test1-exe' for Test1-0.1.0.0..
      [1 of 2] Compiling Main [Lib changed]
      [2 of 2] Compiling Paths_Test1
      Linking .stack-work\dist\274b403a\build\Test1-exe\Test1-exe.exe ...
      Preprocessing test suite 'Test1-test' for Test1-0.1.0.0..
      Building test suite 'Test1-test' for Test1-0.1.0.0..
      [2 of 2] Compiling Main
      Linking .stack-work\dist\274b403a\build\Test1-test\Test1-test.exe ...
      Test1> copy/register
      Installing library in ...\Test1\.stack-work\install\261a8b8d\lib\x86_64-windows-ghc-8.10.4\Test1-0.1.0.0-584XNC80AeFHEA877FoaQr
      Installing executable Test1-exe in ...\Test1\.stack-work\install\261a8b8d\bin
      Registering library for Test1-0.1.0.0..
      Test1> test (suite: Test1-test)
      
      rev: [OK]
      listRevRevId: [OK, passed 100 tests]
      
               Properties  Test Cases  Total
       Passed  1           1           2
       Failed  0           0           0
       Total   1           1           2
      
      Test1> Test suite Test1-test passed
      Completed 2 action(s).
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
You could leave a comment if you were logged in.
toolsetup/testframework.1616511991.txt.gz · Last modified: (external edit)

Except where otherwise noted, content on this wiki is licensed under the following license: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki