简体   繁体   English

传播错误导致HUnit测试失败

[英]Propagating errors to cause HUnit tests to fail

I am writing an HUnit test for a function eval :: Wff -> Assignment -> Maybe Bool . 我正在为功能eval :: Wff -> Assignment -> Maybe Bool编写HUnit测试。 Wff is a custom data type which is an abstract parse tree for a simplified subset of boolean expressions: Wff是一种自定义数据类型,它是布尔表达式的简化子集的抽象分析树:

data Wff = Var Name
         | Not Wff
         | Or Wff Wff
         deriving (Eq)

and Assignment is a type alias for an associative list which gives a boolean value for each variable in a Wff : Assignment是关联列表的类型别名,该列表为Wff每个变量提供布尔值:

type Assignment = [(Name, Bool)]

My current test looks like this: 我当前的测试如下:

testEval :: Test
testEval = "Test eval"
        ~: TestList $ zipWith (\e (Just a) -> e ~=? a) expected (eval wff <$> assignments)
    where expected = [True, False]
          assignments = [[('p', True)], [('p', False)]]
          Right wff = parse wffStr
          wffStr = "p"

Both of the tests constructed pass. 两项测试均通过。 However, the test is not very robust. 但是,该测试不是很可靠。 If I modify it with a more complicated value for wffStr but make a typo, the pattern Right wff will fail because parse will return a Left String instead of a Rigth Wff . 如果我使用wffStr的更复杂的值对其进行wffStr但输入错误,则模式Right wff将失败,因为parse将返回Left String而不是Rigth Wff This causes the test run to abort and I'd rather get a failure for the few bad tests and the actual results for the rest. 这会导致测试运行中止,而我宁愿因为一些不好的测试而失败,而其余的则是实际结果。 How can I modify my current structure so that the error propagates to cause the test to fail instead of aborting all together? 如何修改当前结构,使错误传播导致测试失败,而不是一起放弃?

I don't know anything about HUnit, but: can you just tell it what to do when the parse fails? 我对HUnit一无所知,但是:您能告诉它解析失败时该怎么做吗?

testEval = case parse wffStr of
    Left _ -> {- use HUnit's functions to make a failing test case -}
    Right wff -> "Test eval" ~: TestList $ {- ... -}
    where expected = [True, False]
          {- ... -}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使Xcode 5单元测试过早失败? - How to cause Xcode 5 unit tests to fail early? 如果测试有错误,Karma不会失败 - Karma don’t fail if tests have errors 为什么我的 HUnit 测试套件失败但在 Cabal 中成功通过? - Why does my HUnit test suite fail but pass successfully in Cabal? 在未捕获的异常情况下,如何使Xcode单元测试失败而不崩溃? - How to cause Xcode unit-tests to fail and not crash in case of an uncaught exception? 如何忽略 Next.js 组件中的 CSS 模块导入导致单元测试中的解析错误 - How do I ignore CSS Module imports in a Next.js component that cause parse errors in unit tests 导致TestNG测试失败? - Cause a TestNG test to fail? 为什么使用NSubstitue.Arg.Any <string> 在一组单元测试中,导致另一组中的最后一个测试失败 - Why does using NSubstitue.Arg.Any<string> in one group of unit tests cause the last test in another set to fail 创建表单测试:测试断言失败时如何查看表单错误 - creating tests for forms: how to see the form's errors when the test assertions fail 是什么导致EnumProcesses()失败? - What may cause EnumProcesses() to fail? 如果测试失败,则停止/失败 docker 构建 - Stop/fail docker build if tests fail
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM