不同输入数据的Java单元测试 [英] Java unit test for different input data
问题描述
我需要测试一些 API.例如,我在类中有多个 @Test 方法来测试我的功能,在初始化之前我连接到我的服务的某个 url,并使用它.
I need to test some api. For example I have multiple @Test methods in the class to test my functionality, before init I connect to some url of my service, and use it.
如果我的服务位于多个 url(不同的服务器环境),我该如何针对不同的服务 url 测试此功能?
If I have my service at multiple urls(different server environments), how can I Test this functionality for different service urls?
流程:
- 通过 url 初始化连接
- 运行所有测试
- 通过另一个 url 发起连接
- 运行所有测试(相同)
- ...
什么时候我只喜欢这样的一位主持人:
when was only one host I do like this:
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-参数化测试/通过这种方式,您可以使用不同的参数进行多次相同的测试.
Happen to me sometime, and them I found this awesome idea called Parameterized Test, for instance: http://www.mkyong.com/unittest/junit-4-tutorial-6-parameterized-test/ in this way you can all the same tests couple of time with different argument.
这篇关于不同输入数据的Java单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!