700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 参数化测试 junit_使用JUnitParams进行参数化的JUnit测试

参数化测试 junit_使用JUnitParams进行参数化的JUnit测试

时间:2020-12-09 11:22:20

相关推荐

参数化测试 junit_使用JUnitParams进行参数化的JUnit测试

参数化测试 junit

参数化的单元测试用于在不同条件下测试相同的代码。 借助参数化的单元测试,我们可以建立一种测试方法,该方法从某个数据源中检索数据。 该数据源可以是测试数据对象,外部文件甚至数据库的集合。 一般的想法是使使用相同的单元测试方法测试不同的条件变得容易,这将限制我们需要编写的源代码并使测试代码更健壮。 我们可以将这些测试称为数据驱动的单元测试。

在JUnit中实现数据驱动的单元测试的最佳方法是使用JUnit的自定义运行程序-Parameterized或JUnitParams的JUnitParamsRunner。 使用JUnit的方法可能在许多情况下都可以使用,但后者似乎更易于使用且功能更强大。

基本例子

在我们的例子中,一个扑克骰子,我们需要计算满屋的分数。 就像纸牌扑克一样,“满座”是一副掷骰子,您同时拥有3个和一对。 为了简单起见,分数是一卷中所有骰子的总和。 因此,让我们看一下代码:

class FullHouse implements Scoreable {@Overridepublic Score getScore(Collection dice) {Score pairScore = Scorables.pair().getScore(dice);Score threeOfAKindScore = Scorables.threeOfAKind().getScore(pairScore.getReminder());if (bothAreGreaterThanZero(pairScore.getValue(), threeOfAKindScore.getValue())) {return new Score(pairScore.getValue() + threeOfAKindScore.getValue()); // no reminder}return new Score(0, dice);}private boolean bothAreGreaterThanZero(int value1, int value2) {return value1 > 0 && value2 > 0;}}

我想确保该掷骰正确得分(当然我已经对Pair和ThreeOfAKind进行了单元测试)。 因此,我想测试以下条件:

分数是11:1、1、3、3、3 2、2、2、1、1的得分是8 分数是0代表:2、3、4、1、1 分数是25,表示:5、5、5、5、5

让我们研究为该方法编写数据驱动的测试的两种可能方法。 首先,JUnit的参数化

@RunWith(Parameterized.class)public class FullHouseTest {private Collection rolled;private int score;public FullHouseTest(Collection rolled, int score) {this.rolled = rolled;this.score = score;}@Testpublic void fullHouse() {assertThat(new FullHouse().getScore(rolled).getValue()).isEqualTo(score);}@Parameterized.Parameterspublic static Iterable data() {return Arrays.asList(new Object[][]{{roll(1, 1, 3, 3, 3), score(11)},{roll(2, 2, 2, 1, 1), score(8)},{roll(2, 3, 4, 1, 1), score(0)},{roll(5, 5, 5, 5, 5), score(25)}});}private static int score(int score) {return score;}}

另一个是JUnitParams

@RunWith(JUnitParamsRunner.class)public class FullHouseTest {@Test@Parameterspublic void fullHouse(Collection rolled, int score) {assertThat(new FullHouse().getScore(rolled).getValue()).isEqualTo(score);}public Object[] parametersForFullHouse() {return $($(roll(1, 1, 3, 3, 3), score(11)),$(roll(2, 2, 2, 1, 1), score(8)),$(roll(2, 3, 4, 1, 1), score(0)),$(roll(5, 5, 5, 5, 5), score(25)));}private static int score(int score) {return score;}}

乍一看,两者看起来非常相似。 没错 那么JUnitParameterized(1)和JUnitParams(2)之间有什么区别? 最重要的一种是传递参数的方法,因此实际上是解决方案的体系结构。 在(1)中,参数在构造函数中传递,而在(2)中,参数直接传递到测试方法。 我应该在乎吗? 是。 原因之一是,在(2)中,我们可以有多个参数化测试方法,每种方法的数据都不同,如以下示例所示:

@RunWith(JUnitParamsRunner.class)public class NumberOfAKindTest {@Test@Parameterspublic void pair(Collection rolled, int[] expected, int score) {NumberOfAKind sut = new NumberOfAKind(2);doTest(rolled, expected, score, sut);}@Test@Parameterspublic void threeOfAKind(Collection rolled, int[] expected, int score) {NumberOfAKind sut = new NumberOfAKind(3);doTest(rolled, expected, score, sut);}public Object[] parametersForPair() {return $($(roll(1, 1, 1, 2, 3), hand(1, 1), score(2)),$(roll(2, 1, 1, 1, 1), hand(1, 1), score(2)),$(roll(2, 3, 4, 1, 1), hand(1, 1), score(2)),$(roll(2, 3, 5, 5, 5), hand(5, 5), score(10)),$(roll(2, 1, 5, 4, 3), null, score(0)));}public Object[] parametersForThreeOfAKind() {return $($(roll(1, 1, 1, 2, 3), hand(1, 1, 1), score(3)),$(roll(2, 1, 1, 1, 3), hand(1, 1, 1), score(3)),$(roll(2, 3, 1, 1, 1), hand(1, 1, 1), score(3)),$(roll(2, 3, 5, 5, 5), hand(5, 5, 5), score(15)),$(roll(2, 5, 5, 5, 6), hand(5, 5, 5), score(15)),$(roll(2, 2, 5, 5, 3), null, score(0)));}private static int[] hand(int... dice) {return dice;}private static int score(int score) {return score;}}

在更简单的示例中,可以通过值方法直接在@Parameters批注中将参数定义为String数组。 我们还可以将数据提取到一个外部类中,并使我们的测试更加清晰易读。 对NumberOfAKind的完整测试如下:

@RunWith(JUnitParamsRunner.class)public class NumberOfAKindTest {@Test@Parameters(source = NumberOfAKindProvider.class, method = "providePair")public void pair(Collection rolled, int[] expected, int score) {NumberOfAKind sut = new NumberOfAKind(2);doTest(rolled, expected, score, sut);}@Test@Parameters(source = NumberOfAKindProvider.class, method = "provideThreeOfAKind")public void threeOfAKind(Collection rolled, int[] expected, int score) {NumberOfAKind sut = new NumberOfAKind(3);doTest(rolled, expected, score, sut);}@Test@Parameters(source = NumberOfAKindProvider.class, method = "provideFourOfAKind")public void fourOfAKind(Collection rolled, int[] expected, int score) {NumberOfAKind sut = new NumberOfAKind(4);doTest(rolled, expected, score, sut);}@Test@Parameters(source = NumberOfAKindProvider.class, method = "provideFiveOfAKind")public void fiveOfAKind(Collection rolled, int[] expected, int score) {NumberOfAKind sut = new NumberOfAKind(5);doTest(rolled, expected, score, sut);}private void doTest(Collection rolled, int[] expected, int score, NumberOfAKind sut) {Collection consecutiveDice = sut.getConsecutiveDice(rolled);assertDiceContainsValues(consecutiveDice, expected);assertThat(sut.getScore(rolled).getValue()).isEqualTo(score);}private void assertDiceContainsValues(Collection dice, int[] expected) {Collection values = toInts(dice);if (expected == null) {assertThat(values).isEmpty();return;}for (int i = 0; i < expected.length; i++) {assertThat(values).hasSize(expected.length).contains(expected[i]);}}private Collection toInts(Collection dice) {return Collections2.transform(dice, new Function() {@Overridepublic Integer apply(Dice input) {return input.getValue();}});}}

每个方法都指定提供程序类和提供程序的方法名称。 查看以下提供者:

public class NumberOfAKindProvider {public static Object[] providePair() {return $($(roll(1, 1, 1, 2, 3), hand(1, 1), score(2)),$(roll(2, 1, 1, 1, 1), hand(1, 1), score(2)),$(roll(2, 3, 4, 1, 1), hand(1, 1), score(2)),$(roll(2, 3, 5, 5, 5), hand(5, 5), score(10)),$(roll(2, 1, 5, 4, 3), null, score(0)));}public static Object[] provideThreeOfAKind() {return $($(roll(1, 1, 1, 2, 3), hand(1, 1, 1), score(3)),$(roll(2, 1, 1, 1, 3), hand(1, 1, 1), score(3)),$(roll(2, 3, 1, 1, 1), hand(1, 1, 1), score(3)),$(roll(2, 3, 5, 5, 5), hand(5, 5, 5), score(15)),$(roll(2, 5, 5, 5, 6), hand(5, 5, 5), score(15)),$(roll(2, 2, 5, 5, 3), null, score(0)));}public static Object[] provideFourOfAKind() {return $($(roll(1, 1, 1, 1, 3), hand(1, 1, 1, 1), score(4)),$(roll(2, 1, 1, 1, 1), hand(1, 1, 1, 1), score(4)),$(roll(2, 5, 5, 5, 5), hand(5, 5, 5, 5), score(20)),$(roll(2, 3, 4, 5, 5), null, score(0)));}public static Object[] provideFiveOfAKind() {return $($(roll(1, 1, 1, 1, 1), hand(1, 1, 1, 1, 1), score(5)),$(roll(6, 6, 6, 6, 6), hand(6, 6, 6, 6, 6), score(30)),$(roll(6, 6, 6, 6), null, score(0)),$(roll(2, 3, 4, 6, 6), null, score(0)));}private static int[] hand(int... dice) {return dice;}private static int score(int score) {return score;}}

摘要

对我而言,JUnitParams是编写良好的数据驱动的单元测试的更好的解决方案。 但是上面介绍的并不是库必须提供给开发人员的所有内容。 还有更多功能。 参数可以作为CSV字符串传递,我们可以混合参数化测试和非参数化测试,仅举几例。

请访问该项目的网站以了解有关此库的更多信息: https : ///p/junitparams

参考:来自Codeleak.pl博客的JCG合作伙伴 Rafal Borowiec的JUnitParams进行的 参数化JUnit测试 。

翻译自: //12/parameterized-junit-tests-with-junitparams.html

参数化测试 junit

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。