当前位置: 首页 > 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://www.yutouwan.com/news/15914/

相关文章:

  • 网站后台程序怎么做网址你知道我的意思的免费
  • 网站服务器崩溃影响深圳营销型网站费用
  • 楚雄州住房和城乡建设局网站移动端英文简称
  • 百度做网站推广的费用企业手机网站建设有
  • 自学做网站要多久订制网站
  • 抄袭网站设计oa系统网页版
  • 奉贤做网站的太原网站怎么做seo
  • 光谷网站制作包装设计报价明细
  • 罗定网站建设郑州建设电商网站
  • 昆明网站建设平台建设银行纪检监察网站
  • 网站建设的作用是什么wordpress power
  • 网站维护的基本内容包括哪些网站设计依赖于什么设计
  • 手机应用市场下载安装到手机谷歌seo外包
  • 如何加强网站信息管理建设怎么做像京东一样的网站
  • 有哪些游戏网站大图模板网站
  • wordpress网站投放广告建设网站内容
  • 网站空间大小 论坛优化的定义
  • 网站站群建设方案wordpress调用内容
  • 网站竞价排名wordpress响应缓慢
  • 网站 三合一免费建站平台哪个稳定
  • 邢台专业网站建设费用网站建设实验总结
  • 青岛高品质网站建设戴尔网站建设的目的
  • ps怎么做网站首页图网站开发项目职责
  • 微信官网与手机网站区别wordpress 修改端口号
  • 企业网站和信息化建设金蝶企业网站推广推广阶段
  • 北京天津网站设计制作多少钱wordpress判断首页选择不同模板
  • 制作简单门户网站步骤东莞制作网站建设的公司
  • 怎样创建个人的网站全县网站建设情况通报
  • 谷歌做英文网站山西招标
  • 哪个网站可以做1040杭州ppt设计公司