I'm trying to get my head around the reason why the test file containing unit-tests which is defined as a module fails when run with stack build --test
.
Say having a simple test module defined from scratch with:
stack new test-module
cd test-module
vim package.yaml # remove "executables" section, add "hspec" as tests dependency
Following "getting started" instructions from Hspec documentation I've modified files such as:
Step 1: Describe your desired behavior
-- file test/Spec.hs
module LibSpec where
import Test.Hspec
import Lib
main :: IO ()
main = hspec $ do
describe "divides" $ do
it "returns True when the first number divides the second" $
2 `divides` 4 `shouldBe` True
Step 2: Write some code
-- file src/Lib.hs
module Lib (divides) where
divides :: Integer -> Integer -> Bool
divides d n = rem n d == 0
Running stack build --test
throws the following error:
<no location info>: error:
output was redirected with -o, but no output will be generated
because there is no Main module.
When I comment out the "module definition" line from the test/Spec.hs
file the build succeeds and the unit-test passes:
-- file test/Spec.hs
-- Notice the next line is commented out:
-- module LibSpec where
import Test.Hspec
import Lib
main :: IO ()
main = hspec $ do
describe "divides" $ do
it "returns True when the first number divides the second" $
2 `divides` 4 `shouldBe` True
Is that Hspec related or Stack related? Or maybe am I missing something obvious?
stack build --test
I putsrc/Lib.hs
file andtest/Spec.hs
file in the top directory and following the Hspec tutorial I invokestack exec -- runhaskell Spec.hs
WITH the module definition (uncommented) it doesn't throw an error. Now it's even more confusing : P – Romilda