在Cucumber测试场景中比较整数值的最佳实践

时间:2017-02-17 12:59:32

标签: java testing automation cucumber scenarios

我需要比较黄瓜测试中大于或小于计数的数字。我应该如何编写有效的方案呢?

Scenario: Search and Compare count value.
Then I Search and Verify "user" count  is "less than" 3
Then I Search and Verify "user" count  is "greater than" 3

从上面的场景我应该如何通过比较运算符并从java stepdef获取。

1 个答案:

答案 0 :(得分:1)

不要依赖比较器,而是使用Hamcrest lessThangreaterThan匹配器。在您的步骤定义中:

import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThan;

...

if(condition.equals("less than")) {
    assertThat(val, lessThan(count));
} else if(condition.equals("greater than")) {
    assertThat(val, greaterThan(count));
}

您可以将其丰富以包含所有可能的条件:

switch(condition.toLowerCase()) {

   case "less than":
   case "<":
       assertThat(val, lessThan(count));
       break;

   case "less than or equal to":
   case "<=":
       assertThat(val, lessThanOrEqualTo(count));
       break;

   case "greater than":
   case ">":
       assertThat(val, greaterThan(count));
       break;

   case "greater than or equal to":
   case ">=":
       assertThat(val, greaterThanOrEqualTo(count));
       break;

   ...
}

请注意,您需要依赖项目中的Hamcrest库(如果它不是依赖项)。