因为只写过一个Junit测试,又测试项目逻辑比较复杂,以下只介绍几个测试中遇到问题的解决思路。

Web服务器:Linux

客户端:Linux/Windows(只要能执行java程序就行,测试java -jar Test_Client.jar)


问题

1、因为测试项目不同,需要执行部署不同的测试环境,怎么在客户端执行部署操作?

答:可以在java中通过SSH远程执行服务器中的命令,例如。(注,我引用的是网上随便找的包,当然你也可以自己写一个)

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
    @After
    public void tearDown() throws Exception {
        try {
            Connection conn = new Connection(TestConfig.SERVER_IP);
            conn.connect();
            conn.authenticateWithPassword(TestConfig.USERNAME,
                    TestConfig.PASSWORD);
            Session session = conn.openSession();
            String execCommandString = TestConfig.DELETE_APPLICATION;
            session.execCommand(execCommandString);
            Thread.sleep(TestConfig.DELETE_APPLICATION_SLEEPTIME);
            session.close();
            conn.close();
        } catch (Exception e) {
            System.out.println("Delete application error!");
            throw new Exception();
        }
    }

2、怎么模仿浏览器访问网站?

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
// Run test.
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target(TestConfig.TESTURL);
        String response = null;
                for (int i = 0; i < TestConfig.REQUEST_TIMES; i++) {
                   // If there's only request,response may be 404.
                   response = target.request().get(String.class).toString();
                }
        client.close();

3、junit4默认测试所有使用@Test注解的Case,如果想根据测试环境决定测试哪些怎么办?

import junit.framework.TestSuite;
public class TestAll {
    public static void main(String[] args) {
        try {
            //初始化,获取参数等。。。
            TestConfig.init();
        } catch (Exception e) {
            System.out.println("TestConfig init fail.");
            System.out.println("Test over.");
            System.exit(1);
        }
        TestSuite suite = new TestSuite();
        //判断环境XFF
        String XFF = TestUtil.getOriginXFF();
        if (XFF != null) {
            switch (XFF) {
            case TestConfig.XFF2TT:
                //TestConfig.XFF2TT_TESTS是字符串数组,包括当前环境下需要测试的序列号。
                for (int i = 0; i < TestConfig.XFF2TT_TESTS.length; i++) {
                    suite.addTest(new XFF_2_TT("test"
                            + TestConfig.XFF2TT_TESTS[i]));
                }
                break;
            case TestConfig.XFF2TF:
                //TestConfig.XFF2TF_TESTS是字符串数组,包括当前环境下需要测试的序列号。
                for (int i = 0; i < TestConfig.XFF2TF_TESTS.length; i++) {
                    suite.addTest(new XFF_2_TF("test"
                            + TestConfig.XFF2TF_TESTS[i]));
                }
                break;
            default:
                break;
            }
        } else {
            System.out.println("Did not find any tests fit with the environment.");
            System.out.println("Test over.");
            System.exit(0);
        }
        TestUtil.createBuildpack();
        junit.textui.TestRunner.run(suite);
        TestUtil.deleteBuildpack();
    }
}

4、怎么获取配置参数,且用户直接修改文件中的值,而不需要修改测试程序?

在测试的jar包同一目录下创建配置文件,如下所示。

#server系统配置
#======================================================
#分别表示主机名、Server的IP、用户名和密码,必须指定。
host = remote
server.ip = 0.0.0.0
username = root
password = toor
#======================================================

然后在程序中获取参数,如下所示。

   

 public static void init() throws Exception {
         String propertiesFile = null;
         Properties properties = null;
         try {
              propertiesFile = "TestConfig.properties";
              properties = new Properties();
              properties.load(new FileInputStream(propertiesFile));
              } catch (Exception e) { 
                   System.out.println("Load propertiesfile fail.");
                   throw new Exception();
              }      
              try {
                HOST = properties.getProperty("host", "");
                SERVER_IP = properties.getProperty("server.ip");
                USERNAME = properties.getProperty("username");
                PASSWORD = properties.getProperty("password");
                if ("".equals(HOST) || "".equals(SERVER_IP) || "".equals(USERNAME) || "".equals(PASSWORD)) {
                    throw new Exception();
                }
              } catch (Exception e) {
                System.out.println("There are no configuration parameters or  parameter analysis error.");
                throw new Exception();
              }