当前位置: 首页 > news >正文

wordpress主题启用后网站专业优化公司

wordpress主题启用后,网站专业优化公司,广州seo排名,烟台做网站找哪家好参数化测试 junit参数化的单元测试用于在不同条件下测试相同的代码。 借助参数化的单元测试#xff0c;我们可以建立一种测试方法#xff0c;该方法从某个数据源中检索数据。 该数据源可以是测试数据对象#xff0c;外部文件甚至数据库的集合。 一般的想法是使使用相同的单元… 参数化测试 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进行了单元测试。 因此我想测试以下条件 分数是111、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 {TestParameterspublic 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;} } 乍一看两者看起来非常相似。 没错 那么JUnit Parameterized 1和JUnitParams2之间有什么区别 最重要的一种是传递参数的方法因此实际上是解决方案的体系结构。 在1中参数在构造函数中传递而在2中参数直接传递到测试方法。 我应该在乎吗 是。 原因之一是在2中我们可以有多个参数化测试方法每种方法的数据都不同如以下示例所示 RunWith(JUnitParamsRunner.class) public class NumberOfAKindTest {TestParameterspublic void pair(Collection rolled, int[] expected, int score) {NumberOfAKind sut new NumberOfAKind(2);doTest(rolled, expected, score, sut);}TestParameterspublic 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 {TestParameters(source NumberOfAKindProvider.class, method providePair)public void pair(Collection rolled, int[] expected, int score) {NumberOfAKind sut new NumberOfAKind(2);doTest(rolled, expected, score, sut);}TestParameters(source NumberOfAKindProvider.class, method provideThreeOfAKind)public void threeOfAKind(Collection rolled, int[] expected, int score) {NumberOfAKind sut new NumberOfAKind(3);doTest(rolled, expected, score, sut);}TestParameters(source NumberOfAKindProvider.class, method provideFourOfAKind)public void fourOfAKind(Collection rolled, int[] expected, int score) {NumberOfAKind sut new NumberOfAKind(4);doTest(rolled, expected, score, sut);}TestParameters(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 : //code.google.com/p/junitparams 参考来自Codeleak.pl博客的JCG合作伙伴 Rafal Borowiec的JUnitParams进行的 参数化JUnit测试 。 翻译自: https://www.javacodegeeks.com/2013/12/parameterized-junit-tests-with-junitparams.html参数化测试 junit
http://wiki.neutronadmin.com/news/15914/

相关文章:

  • 非交互式网站建网站有哪些文件夹
  • 北京沙河教做网站的黔西网站建设
  • 大连做公司网站哪家好做旅游宣传不错的网站
  • 贵阳做网站的服装设计素材网站
  • 松原手机网站开发公司电话专业的广州手机网站
  • 长沙建站找有为太极环境遵网站被做暗链报告
  • 关于建设集团公司网站的报告青岛网站建设培训
  • 公众号怎么做网站多种语言的网站
  • 上海电商网站设计河源网站搭建费用
  • 校园网站规划与建设心得高校建设思政教育网站案例
  • 如何做网站小编seo刷排名工具
  • 全能网站建设教程杭州建设网站 网站建设
  • 南昌网站网页设计中铁建设集团招标网站
  • 回力网站建设初衷微信小程序卖货平台
  • 商城网站建设价位seo运营招聘
  • 一对一直播网站开发微信网站建设平台
  • 中文域名网站建设做网站广告有哪些职位
  • 凡科网站产品导航怎么做莆田外贸网站建设
  • 衡阳微信网站开发上传网站步骤
  • 网站建设运营知乎现在的网站用什么程序做
  • 滨州做网站公司自己做的网站怎么链接火车头采集
  • 哪家建网站wordpress作者页制作
  • 网站营销推广公司免费模板网站都有什么用
  • 2手房产App网站开发app小程序网站开发
  • 大邑做网站永康好口碑关键词优化
  • 网站规划小结涿州做网站
  • 微网站是什么意思云南信息港
  • 企业网站带后台营销推广有哪些步骤
  • 可信赖的广州做网站网店推广工作内容
  • ps切片以后 怎么做网站电子商务的网站建设过程