Jmeter + Junit


 

在使用jmeter中Junit的功能之前,最好將接口以手動的方式調通,可以參考 Jmeter之HTTP Request

下面開始講一下如何使用Junit功能

1.  編寫測試類 JmeterJunit 代碼如下

package com.test.junit;


import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class JmeterJunit {
    public String urlPath = "http://www.mocky.io/v2/572ee171120000332519491b";
    public HttpURLConnection connection = null;
    public OutputStream output = null;
    public BufferedReader reader = null;
    public static String request = "{ \"type\": \"getName\"}";

    @Before
    public void setUp() throws Exception {
        URL url = new URL(urlPath);
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestMethod("POST");
        connection.setUseCaches(false);
        connection.setInstanceFollowRedirects(true);
        connection.setRequestProperty("Content-type", "application/json");
        connection.setRequestProperty("Content-encoding", "UTF-8");
        connection.setRequestProperty("Accept", "application/json");
        connection.connect();
    }

    @After
    public void tearDown() throws Exception {
        if (null != output) {
            try {
                output.close();
            } catch (IOException e) {
                throw e;
            }
        }
        if (null != reader) {
            try {
                reader.close();
            } catch (IOException e) {
                throw e;
            }
        }
        if (null != connection) {
            connection.disconnect();
        }
    }
    
    public void sendRequest(String request) throws Exception {
        output = connection.getOutputStream();
        output.write(request.getBytes());
        output.flush();
    }

    public String getResponse() throws Exception {
        reader = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        String st;
        StringBuffer stb = new StringBuffer();
        while (null != (st = reader.readLine())) {
            stb.append(st);
        }
        return stb.toString();
    }

    @SuppressWarnings("deprecation")
    public void checkResponse(String response,String name) throws Exception {
        boolean actualResult = response.contains(name);
        assertTrue("the expected status is " + name
                + ", but now it's not", actualResult);
    }

    @Test
    public void test1() throws Exception {
        sendRequest(request);
        checkResponse(getResponse(),"Brett");
    }
    
    @Test
    public void test2() throws Exception {
        sendRequest(request);
        checkResponse(getResponse(),"Jason");
    }
    
    @Test
    public void test3() throws Exception {
        sendRequest(request);
        checkResponse(getResponse(),"Elliotte");
    }
    
    

}

代碼中的請求地址和響應信息在Jmeter之HTTP Request中都有描述,這邊不多說。在代碼中,編寫了三個測試方法,分別檢查響應中是否包含Brett、Jason以及Elliotte這三個名字。

2. 調試用例,結果如下

3.調試通過后,導出Jar包,並將該Jar包導入到.\Jmeter\apache-jmeter-2.13\lib\junit目錄下。(如果編寫代碼時,存在第三方Jar包引入,那么就將該第三方Jar包放在.\Jmeter\apache-jmeter-2.13\lib中)

4. 重新啟動Jmeter,添加三條Junit Request、查看結果樹以及圖形結果。

5點擊運行按鈕,可以在查看結果樹中查看結果,如果想要將測試結果保存到文件,可以如下圖配置

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM