1

您将如何编写一个 Tasty HUnit 测试,该测试在单个测试中包含针对单个夹具变量的多个检查,或者至少是一组整洁的此类测试?

例如,考虑这个 Gherkin 规范:

Scenario: A 3-Vector has x, y and z components
  Given: a <- Vec(1.0, 2.0, 3.0)
  Then: a.x = 1.0
  And a.y = 2.0
  And a.z = 3.0

我可以做这样的事情,但它非常重复:

unitTests = testGroup "All Unit Tests"
  [ testCase "A 3-Vector has x, y and z components" $ assertEqual [] (x $ Vec 1.0 2.0 3.0) 1.0
  , testCase "A 3-Vector has x, y and z components" $ assertEqual [] (y $ Vec 1.0 2.0 3.0) 2.0
  , testCase "A 3-Vector has x, y and z components" $ assertEqual [] (z $ Vec 1.0 2.0 3.0) 3.0
  ]

我对此的担忧是我已经重复了三次场景名称,并且我还创建了三次夹具。我想找到一种方法将所有三个断言分组为一个标题为“A 3-Vector has x, y and z components”的组,并且只指定一次夹具 Vec。

我可以扩展测试规范以尽量减少一些描述重复,但如果可以的话,我宁愿坚持 Gherkin 规范:

unitTests = testGroup "All Unit Tests"
  [ testCase "A 3-Vector has x component" $ assertEqual [] (x $ Vec 1.0 2.0 3.0) 1.0
  , testCase "A 3-Vector has y component" $ assertEqual [] (y $ Vec 1.0 2.0 3.0) 2.0
  , testCase "A 3-Vector has z component" $ assertEqual [] (z $ Vec 1.0 2.0 3.0) 3.0
  ]

我不知道为组定义一次 Vec 的方法。

我想做的是这样的(不是真正的代码!):

unitTests = testGroup "All Unit Tests"
  [ testScenario "A 3-Vector has x, y and z components" 
    let v = Vec 1.0 2.0 3.0 in
    [ testCase "x" assertEqual [] (x $ v) 1.0
    , testCase "y" assertEqual [] (y $ v) 2.0
    , testCase "z" assertEqual [] (z $ v) 3.0
    ]
  ]
4

2 回答 2

2

感谢Joachim Breitner,他建议我的“非真实代码”并没有离题太远。他是对的。

通过一些调整,我最终得到了这个,它可以按我的意愿工作:

data Vec = Vec { x, y, z :: Double } deriving (Show)

unitTests = testGroup "All Unit Tests"
  [ testGroup "A 3-Vector has x, y and z components" $
    let v = Vec 1.0 2.0 3.0 in
    [ testCase "x" $ assertEqual [] (x v) 1.0
    , testCase "y" $ assertEqual [] (y v) 2.0
    , testCase "z" $ assertEqual [] (z v) 3.0
    ]
  ]
于 2020-05-08T23:35:49.863 回答
0

在一个 testCase 中有多个断言是完全可以的。所以你可以这样做:

unitTests = testGroup "All Unit Tests"
  [ testCase "A 3-Vector has x, y and z components" $ do
    let v = Vec 1.0 2.0 3.0
    assertEqual [] (x v) 1.0
    assertEqual [] (y v) 2.0
    assertEqual [] (z v) 3.0
  ]
于 2020-10-24T20:16:10.743 回答