针对不同输入数据的Java单元测试 [英] Java unit test for different input data
问题描述
我需要测试一些api。例如,我在类中有多个@Test方法来测试我的功能,在init之前我连接到我的服务的某个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.
如果我的服务位于多个网址(不同的服务器环境),我该如何针对不同的服务网址测试此功能?
If I have my service at multiple urls(different server environments), how can I Test this functionality for different service urls?
流程:
- 网址初始化
- 运行所有测试
- 另一个网址初始化
- 运行所有测试(相同)
- 。 ..
- Init conncetion by url
- Run all Tests
- Init conncetion by another url
- Run all Tests(the same)
- ...
当我只有一个主人时我喜欢这个:
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-parameterized-test/ 以这种方式
你可以用不同的参数进行所有相同的测试。
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屋!