您将如何编写一个 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
]
]