网站建设促销文案,seo优化排名公司,毕业生网站建设方案书,saas系统多少钱这篇来学习一个新的注解#xff0c;叫Parameters, 参数化的意思。什么时候需要用到参数化呢#xff0c;在selenium中这个例子很好#xff0c;就是我有一个500个用户数据#xff0c;我需要重复测试这500个用户的登录功能。我们肯定只需要写一个方法#xff0c;然后调用这个…这篇来学习一个新的注解叫Parameters, 参数化的意思。什么时候需要用到参数化呢在selenium中这个例子很好就是我有一个500个用户数据我需要重复测试这500个用户的登录功能。我们肯定只需要写一个方法然后调用这个方法500次每次方法传入参数不一样这样应该能够完成这个测试需求。在单元测试中如果一个方法的用例采用边界值划分有时候会产生十几个测试用例这个时候我们就需要使用参数化。 1.参数化的数据源
一般来说参数化需要提供数据源数据源可以是数据库表也可以是excel中一行行记录也可以是代码中写死的一个列表的数据或者数组数据。大概过程就是我们提前准备好这个数据源例如一个二元数组里面有5组数据。然后在单元测试用例类中的具体方法使用参数化注解告诉Junit,执行这个方法需要使用外部参数化提供的数据源进行多次测试这个方法。
在Junit中参数化Parameters里面写的是一个静态方法。 2.参数化测试举例
这里新建一个测试类ParameterizedTests.java, 写入下面的代码。
package test;import static org.junit.Assert.assertEquals;import java.util.Arrays;
import java.util.List;import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;import com.anthony.protein.TrackingService;RunWith(Parameterized.class)
public class ParametrizedTests {private int input;private int expected;private static TrackingService ts new TrackingService();//构造方法public ParametrizedTests(int input, int expected) {this.input input;this.expected expected;}Parameterspublic static ListObject[] data() {return Arrays.asList(new Object[][] {{5, 5},{5, 10},{-12, 0},{50, 50},{1, 51}});}Testpublic void test() {if(input 0) {ts.addProtein(input);}else {ts.removeProtein(-input);}//断言期待结果和total是不是相等assertEquals(expected, ts.getTotal());}
}运行结果