【Junit】Web測試

因爲只寫過一個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();
              }



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章