java 不同输入数据的Java单元测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10028188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java unit test for different input data
提问by Sergey
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.
我需要测试一些api。例如,我在类中有多个 @Test 方法来测试我的功能,在初始化之前我连接到我的服务的某个 url,并使用它。
If I have my service at multiple urls(different server environments), how can I Test this functionality for different service urls?
如果我的服务位于多个 url(不同的服务器环境),我如何针对不同的服务 url 测试此功能?
Flow:
流动:
- Init conncetion by url
- Run all Tests
- Init conncetion by another url
- Run all Tests(the same)
- ...
- 通过 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();
}
}
回答by shem
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.
有一次发生在我身上,他们我发现了一个很棒的想法,叫做参数化测试,例如:http: //www.mkyong.com/unittest/junit-4-tutorial-6-parameterized-test/这样你就可以相同的测试几次,但参数不同。
回答by Ray Tayek
there are prameterized tests: http://www.mkyong.com/unittest/junit-4-tutorial-6-parameterized-test/and http://ourcraft.wordpress.com/2008/08/27/writing-a-parameterized-junit-test/.
有参数化测试:http://www.mkyong.com/unittest/junit-4-tutorial-6-parameterized-test/和http://ourcraft.wordpress.com/2008/08/27/writing-a-参数化的junit-test/。
alternatively, you can make your test case abstract and subclass it with each subclass setting up the url.
或者,您可以使您的测试用例抽象并使用每个设置 url 的子类对其进行子类化。