Java单元测试不同的输入数据
问题内容:
我需要测试一些api。例如,我在类中有多个@Test方法来测试我的功能,在初始化之前,我先连接到服务的某些URL并使用它。
如果我的服务位于多个URL(不同的服务器环境),如何为不同的服务URL测试此功能?
流:
- 通过url初始化连接
- 运行所有测试
- 通过另一个URL初始化连接
- 运行所有测试(相同)
- …
当只有一位主持人时,我会这样:
public class TestAPI{
@org.junit.Before
public void init() {
service = new Service("www.test.com");
}
@org.junit.Test
public void testA(){
service.CallA();
}
@org.junit.Test
public void testB(){
service.CallB();
}
}
问题答案:
有一天我发生了,他们发现了这个很棒的主意,称为参数化测试,例如:http : //www.mkyong.com/unittest/junit-4-tutorial-6-parameterized-
test/
这样,您就可以相同的测试几次,但参数不同。