Most of the testing on the server side revolves around interfaces. For interface testing, it is mainly function, automation and performance testing. If I happen to think of a problem, I can write a use case and use it repeatedly in the test scenarios of function, automation and performance, I am sure that I can save a lot of time.

The general idea is the same as the previous interface and performance framework, through the total test project to encapsulate the interface function, leave the test parameters. Functional tests write method calls and then check the human flesh, if automated, take out the interface response, then verify various data, performance directly using the performance framework to call the method directly.

I spent some time making a Demo and sharing it with you.

This is a simple interface request method, where the main method is functional test execution code, use case is the form of documentation, not written here:

public class Headgear extends NajmBase { public Headgear(NajmBase najmBase) { this.loginKey = najmBase.loginKey; this.args = najmBase.args; this.user_id = najmBase.user_id; } private static NajmBase base = NajmBase.getBase(0); public static Headgear drive = new Headgear(base); /** * public int usingHeadgearId; // public JSONObject headgearInfo = new JSONObject(); public Map<Integer, Long> headgearInfo = new HashMap<>(); public static void main(String[] args) { // NajmBase.getUserBalance(base.user_id); // int type = 1, id = 36, packageId = 60, num = 1, price = 1; // NajmBase base1 = new NajmBase(V580User.getUserName(0)); // Headgear headgear = new Headgear(); // headgear.switchHeadgear(34); // output(headgear.getHeadgearInfo()); // output(headgear.getUsingHeadgearId()); // output(base1.loginResponse); // drive.getAllHeadgear(); // new MallBase(base).buy(type, id, packageId, num, price); // drive.getUserHeadgearInfo(); // NajmBase.getUserBalance(base.user_id); // drive.getUserHeadgearInfo(); // drive.getOnsaleHeadgear(); int times = 0; while (true) { times++; int type = 1, id = getRandomInt(2) == 1 ? 34 : 36, packageId = id == 34 ? 56 : 60, num = 1, price = 1; long deadtime1 = drive.getHeadgearInfo().get(id); Verify verify = new Verify(new MallBase(base).buy(type, id, packageId, num, price)); drive.getUserHeadgearInfo(); long deadtime2 = drive.getHeadgearInfo().get(id); if (deadtime2 - deadtime1 ! = DAY) break; } output(" + times "); // output(drive.getHeadgearInfo()); // output(drive.usingHeadgear); // output(drive.loginKey); // output(drive.args); // output(base.loginResponse.getJSONObject(DATAINFO).getJSONObject("headGear").getInt("id")); testOver(); } /** * get all headgear information, Public JSONObject getAllHeadgear() {String URL = HOST + HeadgearApiPath.GET_ALL_HEADGEAR; HttpGet httpGet = getHttpGet(url); JSONObject response = getHttpResponseEntityByJson(httpGet); output(response); return response; } /** * public JSONObject switchHeadgear(int hid) {String URL = HOST + HeadgearApiPath.SWITCH_HEADGEAR + hid + changeJsonToArguments(args); HttpPost httpPost = getHttpPost(url); JSONObject response = getHttpResponseEntityByJson(httpPost); // output(response); return response; } @return */ public JSONObject getUserHeadgearInfo() {sleep(1); String url = HOST + HeadgearApiPath.GET_USER_HEADGEAR; JSONObject response = getHttpResponseEntityByJson(getHttpGet(url, args)); output(response); if (isRightResponse(response)) { headgearInfo.clear(); JSONArray jsonArray = response.getJSONArray(DATAINFO); jsonArray.forEach(json -> { JSONObject jsonObject = JSONObject.fromObject(json.toString()) ; String name = jsonObject.getString("name") ; long deadTime = jsonObject.getLong("deadlineTime") ; int headgearId = jsonObject.getInt("goodId") ; int use = jsonObject.getInt("isUse") ; if (use == 1) usingHeadgearId = headgearId; headgearInfo.put(headgearId, deadTime); output(name, headgearId, getTimeByTimestamp(deadTime)); }); } return response; Public JSONObject getOnsaleHeadgear() {String url = HOST + HeadgearApiPath.GET_ONSALE_HEADGEAR; JSONObject response = getHttpResponseEntityByJson(getHttpGet(url, args)); output(response); return response; } public int getUsingHeadgearId() { getUserHeadgearInfo(); return usingHeadgearId; } public Map<Integer, Long> getHeadgearInfo() { getUserHeadgearInfo(); return headgearInfo; }}Copy the code

The following is an automated test case based on this function. Inside the main method is the process of debugging the use case. The method of executing the use case was written in the previous article, using reflection to record the use case information and execute the test case, and save the test result, output test report, exception warning and so on:

/** * public class HeadgearCase extends SourceCode {static HeadgearCase HeadgearCase = new HeadgearCase(); static NajmBase base = new NajmBase(V580User.getUserName(0)); static Headgear drive = new Headgear(base); public static void main(String[] args) { // headgearCase.testDemo001(); // headgearCase.testDemo002(); // headgearCase.testDemo003(); // headgearCase.testDemo004(); headgearCase.testDemo005(); // headgearCase.testDemo006(); ApiLibrary.testOver(); Public void testDemo001() {String label = "Obtain all header information use case" + TAB + Thread.currentThread().getStackTrace()[1]; Verify verify = new Verify(drive.getAllHeadgear()); JSONObject result = new JSONObject(); Result.put (" status code 0", verify.isright ()); Result.put (" include array ", verify.isarray ("heads")); Result.put (" contains removed headgear ", verify.iscontains (" automatic special 3")); Result.put (" contains the head that is being sold ", verify.iscontains (" Automation special 1")); MySqlTest.saveTestResult(label, result); } public void testDemo002() {String label = "Label" + TAB + Thread.currentThread().getStackTrace()[1]; Verify verify = new Verify(drive.getOnsaleHeadgear()); JSONObject result = new JSONObject(); Result.put (" status code 0", verify.isright ()); Result.put (" contain array ", verify.isarray ("dataInfo")); Result. put(" does not include removed headgear ",! IsContains (" Verify. IsContains "); Result.put (" contains the head that is being sold ", verify.iscontains (" Automation special 1")); Result.put (" contains description information ", verify.iscontains (" test 10 days ")); MySqlTest.saveTestResult(label, result); } /** * Public void testDemo003() {String label = "User label" + TAB + Thread.currentThread().getStackTrace()[1]; Verify verify = new Verify(drive.getUserHeadgearInfo()); JSONObject result = new JSONObject(); Result.put (" status code 0", verify.isright ()); Result. Put (" user head normal ", verify.isContains(" automatic special 1")); Result. The put (" user wearing normal ", verify isContains (isUse \ '\' : "1")); Result. put(" headpack normal ", verify.isContains(" test 1 day ")); MySqlTest.saveTestResult(label, result); Public void testDemo004() {String label = "Insufficient Balance purchase case" + TAB + Thread.currentThread().getStackTrace()[1]; NajmBase base = new NajmBase(V580User.getUserName(1)); int type = 1, id = 36, packageId = 60, num = 1, price = 1; Verify verify = new Verify(new MallBase(base).buy(type, id, packageId, num, price)); JSONObject result = new JSONObject(); Result.put (" status code = 35", 35 == verify.getCode()); MySqlTest.saveTestResult(label, result); Public void testDemo005() {String label = "currentThread()" + TAB + thread.currentThread ().getStackTrace()[1];  int type = 1, id = getRandomInt(2) == 1 ? 34 : 36, packageId = id == 34 ? 56 : 60, num = 1, price = 1; int balance = NajmBase.getUserBalance(drive.user_id); long deadtime1 = drive.getHeadgearInfo().get(id); Verify verify = new Verify(new MallBase(base).buy(type, id, packageId, num, price)); drive.getUserHeadgearInfo(); long deadtime2 = drive.getHeadgearInfo().get(id); int balance1 = NajmBase.getUserBalance(drive.user_id); JSONObject result = new JSONObject(); Result.put (" status code 0", verify.isright ()); Result.put (" the expiration date is correct ", (deadtime2 + EMPTY).equals(verify.getValue("deadlineTime"))); Result. put(" deadtime2 - deadtime1 == DAY ", deadtime2 - deadtime1 == DAY); Result. put(" user balance is normal ", balance-balance1 == 1); result.put(" user balance is normal ", balance-balance1 == 1); MySqlTest.saveTestResult(label, result); Public void testDemo006() {String label = "User label" + TAB + Thread.currentThread().getStackTrace()[1]; drive.getAllHeadgear(); int id1 = drive.getUsingHeadgearId() == 34 ? 36:34; Verify verify = new Verify(drive.switchHeadgear(id1)); int id2 = drive.getUsingHeadgearId(); JSONObject result = new JSONObject(); Result.put (" status code 0", verify.isright ()); Result. put(" headset successfully switched ", id1 == id2); MySqlTest.saveTestResult(label, result); }}Copy the code

Here is a performance test case for one of these functions (there are two types of test cases, one is HTTP single request, I just use the HttpRequestBase object to get the request and then re-send the concurrent request, and one is multi-interface or non-HTTP request, such as Dubbo, mysql, Redis, Message queue, etc., directly call the method to pressure) :

Simple HTTP request:

class CancelReason extends OkayBase{ public static void main(String[] args) { def argsUtil = new ArgsUtil(args) def thread = argsUtil.getIntOrdefault(0, 2) def times = argsUtil.getIntOrdefault(1, 5) def base = getBase() Headgear drive = new Headgear(base); drive.getAllHeadgear() def request = FanLibrary.getLastRequest() def timesthread = new RequestThreadTimes(request, Times) new Concurrent(timesthread, thread," obtain all Concurrent threads "). Start () allOver()}}Copy the code

For non-simple HTTP requests, please refer to the performance Testing Framework Demo: Performance Testing Framework Version 2 written earlier. This kind of method is relatively simple to write and has a high usage range, but needs to solve the security problems of multi-threaded data and objects according to different business requirements.

The entire project is hosted in Git, the functional tests are hosted locally, the automation and performance projects are hosted on the server, run with Groovy scripts, and you can debug locally. The automation project is automatically run at a fixed time or interval, and the performance project receives the command groovy filename.groovy to run.

  • Solemnly declare: the article was first published in the public number “FunTester”, prohibit third parties (except Tencent cloud) reprint, published.

Technical articles selected

  • Java one line of code to print a heart
  • Linux performance monitoring software Netdata Chinese version
  • Performance Testing Framework second edition
  • How to perform performance tests on a Linux command line interface
  • Graphic HTTP brain map
  • Automatically turn the Swagger document into test code
  • Five lines of code to build a static blog
  • Probe into the Linear interface testing framework based on Java
  • Selenium 4.0 Alpha update log
  • Selenium 4.0 Alpha update practices

Selected non-technical articles

  • Why software testing as a career path?
  • Programming thinking for everyone
  • 7 Skills to become an automated test
  • How do YOU introduce automated testing in DevOps
  • Summary of Automatic Test failures on the Web
  • How do YOU introduce automated testing in DevOps
  • Testers often use excuses
  • Browser market share ranking 2019
  • API Testing Basics
  • API Automation Test Guide