3

我想编写一个表达以下内容的测试套件:function f either is not implemented or it is implemented with the some tests.

所以我可以分别检查这两件事。我想做一些类似于spec1 <|> spec2替代实例类似于Maybe's: fail if everything failed 的事情。

所以让我举个例子

-- if function is not implemented
average :: Float -> Float -> Float
average x y = error "function not implemented"

-- This test passes
average 1 2 `shouldThrow` errorCall "function not implemented"



-- if function is implemented
average :: Float -> Float -> Float
average x y = (x + y) / 2

-- This test passes
average 1 2 `shouldBe` 0.5

问题的背景是我想写一个带有自动化测试的练习表,如果学生没有实施练习或者她做得正确,我希望测试通过。因此,我想写一些类似的东西

-- Using alternative
spec = describe "test group 1" $ do
   it "testing average" $
     (average 1 2 `shouldThrow` errorCall "function not implemented")
     <|>
     (average 1 2 `shouldBe` 0.5)

-- Or maybe using a built-in combinator I'm not aware of.
spec = describe "test group 1" $ do
   it "testing average" $ anySuccess
     [ (average 1 2 `shouldThrow` errorCall "function not implemented")
     , (average 1 2 `shouldBe` 0.5)
     ]
4

0 回答 0