重构 TDD 到一个测试数组中

huangapple go评论157阅读模式
英文:

Refactoring TDD down to an array of tests

问题

以下是您提供的文本的翻译:

所以,我有一个用于TDD的Uncle Bob经典保龄球游戏示例的JUnit测试。

我重构了测试以使用游戏数组和预期分数。

优点是很容易添加新的测试。

缺点是它没有“自我说明”代码或测试。

有关此问题的任何最佳实践吗?

public class ScoreTest {
  int[][] games = {
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10}
  };
  int[] scores = {0, 300};
  @Test
  public void testScore() {
    for (int i = 0; i < games.length; i++) {
      int[] game = games[i];
      int expectedScore = scores[i];
      Score score = new Score();
      score.roll(game); // 掷整场比赛
      int actualScore = score.total(); // 计算保龄球得分
      assertEquals(expectedScore, actualScore);
    }
  }
}
英文:

So, I have a JUnit test for Uncle Bob's classic Bowling game example for TDD.

I refactored the test to use an array of games and expected scores.

The advantage is it's easy to add new tests.

The disadvantage is that it doesn't "self document" the code or the test.

Are there any best practices surrouding this?

public class ScoreTest {
int[][] games = {
  {0,0,0,0,0,0,0,0,0,0},
  {10,10,10,10,10,10,10,10,10,10,10,10
};
int[] scores = {0, 300};
@Test
public void testScore() {
  for(int i=0; i&lt;games.length; i++) {
    let game = games[i];
    let expectedScore = scores[i];
    let score = new Score();
    score.roll(game); // roll the entire game
    let actualScore = score.total() // calculate the bowling score
    assertEquals(expectedScore, actualScore);
  }
}
}

答案1

得分: 2

请注意,以下是您要翻译的部分:

"Instead of an int[][], you might make a little inner class, and make an array of those.

private static class BowlingTestGame {
private String name;
private int[] game;
private int expectedResult;
}

BowlingTestGame[] games = {
{"Perfect Game", {10,10,10,10,10,10,10,10,10,10,10,10}, 300},
// ... more tests ...
}

Then you can include the name of the game as the failure message in the assert.

Also, this gets rid of you trying to maintain two parallel arrays, which is always a bad idea."

英文:

Instead of an int[][], you might make a little inner class, and make an array of those.

private static class BowlingTestGame {
    private String name;
    private int[] game;
    private int expectedResult;
}

BowlingTestGame[] games = {
    {&quot;Perfect Game&quot;, {10,10,10,10,10,10,10,10,10,10,10,10}, 300},
    //  ... more tests ...
}

Then you can include the name of the game as the failure message in the assert.

Also, this gets rid of you trying to maintain two parallel arrays, which is always a bad idea.

答案2

得分: 0

我认为,如果有人想要走上“输入 x 应该产生输出 y”测试的道路,那么可读性不会比这更高。我只建议在处理琐碎问题或众所周知的序列(例如生成 斐波那契数列 的测试)时使用这种类型的测试。

对于更复杂的情况,我建议使用行为驱动的方法,例如 Given-When-Then 测试。这些测试通常更具表现力和自我描述性。


一点说明:如果您使用 JUnit5,我建议使用 参数化测试

英文:

I think that if one does want to go down the path of "input x should produce output y"-tests, then one will not get much more readable as this. I would only recommend those kind of tests for trivial problems or well-known sequences (e.g. tests for something producing the fibonacci sequence).

For everything more complex, I would advice to use a behaviour-driven approach with, e.g., Given-When-Then tests. Those tests tend to be more expressive and more self-documenting.


A remark: If you are using JUnit5, I would propose to use parameterized tests.

huangapple
  • 本文由 发表于 2020年8月13日 02:20:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63382592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定