针对不同输入数据的Java单元测试 [英] Java unit test for different input data

查看:116
本文介绍了针对不同输入数据的Java单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要测试一些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?

流程:


  1. 网址初始化

  2. 运行所有测试

  3. 另一个网址初始化

  4. 运行所有测试(相同)

  5. 。 ..

  1. Init conncetion by url
  2. Run all Tests
  3. Init conncetion by another url
  4. Run all Tests(the same)
  5. ...

当我只有一个主人时我喜欢这个:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆