UnitTest implement H5 Page interface function Test _ interface Test

Source: Internet
Author: User
Tags connection pooling string format xpath

Original http://tmq.qq.com/2016/07/h5interfacetestwithunittest/

I. BACKGROUND

The current mainstream H5 page dynamic access to content is the use of Ajax asynchronous request background data to achieve real-time refresh, is actually using the Get/post HTTP request backend interface, and then the returned data (typically JSON or XML format) rendered on the page, Therefore, to ensure the correctness of the H5 page interface is the key to the right of the page content data. Ordinary H5 page tests usually take the form of manual testing, so that only normal scenarios can be simulated, data requests for exceptions cannot be overwritten, and for many of the request parameters, the efficiency is low. There is also a part of the data content interface is no page, such as external cooperation interface, only provide data, the corresponding page by the partners to do their own, you can not use the manual way to test.

In fact, the H5 page interface test is consistent with the traditional interface test. It is through the data preparation (including normal and abnormal parameters), interface calls, the results of the observation of these three processes to verify that the interface's functional logic is in line with the expected, and for unexpected anomalies have better fault tolerance and robustness, Only the invocation parameters of the interface differ in form, as shown in the following table.

In addition, in the H5 page interface test, it is necessary to simulate the HTTP headers of the request based on the actual interface, which is also part of the validation interface data and logic, which is different from the traditional interface test.

The next part of the article provides a way to complete the H5 page interface test, which is mainly applied to the functional test of the newly developed H5 page interface.

Second, why Choose UnitTest

Through the first part of the elaboration, we have H5 page interface testing the basic process of understanding, so in order to achieve H5 page test, we need a tool, it needs the following features:

1. Make Get&post request call

2, Analog HTTP headers to request

3, Convenient construction parameters

4, a rich assertion library, to judge the results

5, clear results show, succ/fail

6, easy to run and debug, use case fail to give debugging information

The above requirements are prioritized, where 1 and 2 are required, and Python is used in the previous test to make HTTP requests, because Python's third-party libraries can easily simulate various HTTP requests, and the call is very concise, so it's natural to think of Python, At the same time, in order to be able to assert the results and clearly show the test results, we used the unittest.

Third, what is UnitTest

UnitTest is a Python self-contained unit test module, similar to the Java Unit Test framework JUnit, a test class can contain multiple test cases, each use case begins with a test_, performs initialization work in the Setup method before execution, Completes exit cleanup in the Teardown method after execution. Multiple test classes can be tested by composing the test suite at the same time. UnitTest also contains a rich assertion library that can validate multiple aspects of the result type, content, and, in the result presentation, using the Python IDE pycharm to get clear results and error messages, as shown in the following illustration.

In the test results above, be able to see the total number of use cases clearly, the number of use cases run and the number of failed use cases, click on the use case name on the left, you can see the failure information, you can jump directly to the corresponding error line, the left-most ribbon of the second button can rerun all failed use cases. For use cases that fail, simply right-click the use case area and select the corresponding debug ... option to debug the use case individually to determine if the problem with the test case is causing the execution to fail, thus ensuring the accuracy of the test results.

Basic usage of 3.1 unittest

Here is the basic use of unittest, different test cases may be slightly different, but the idea is consistent.

1) Import UnitTest

2 Define a test case class that inherits from Unittest.testcase:

3 define setup and teardown, and do some ancillary work before and after each test case:

4 Define the test case, the first name begins with test:

5 Write use cases, a test case should only test one aspect, the test purpose and test content should be very clear. After the operation required to obtain the results of the test, and then call Assertequal, assertraises, and other assertions to determine whether the program execution results and expected values are consistent, UnitTest provides a number of results assertions are mainly the following, the latter part of the comment is the meaning of each assertion:

6) Invoke Unittest.main () Start test:

7 Observe the test results and adjust the script or feedback problem according to the result.

Iv. what needs to be done

Before you start writing test cases, you need to determine how the interface is requested (get/post), request parameters, HTTP headers required by the request, the result type of the return, and the basic elements of the content. The request parameter needs to know the type and scope of the parameter. Based on this information, you can combine scenarios that require testing, and each scenario is a test case that requires coverage of both normal and unusual situations. A basic use-case process is shown below:

The following describes how to implement functional testing of the H5 page interface.

4.1 Request parameter Settings

This section needs to set the required request parameters based on the test scenario, including call parameters and HTTP headers (mainly Referer, content-type, cookies, etc.), and for multiple normal combinations of call parameters, you can loop through the combination of parameters using a single parameter, You need only one test case, which reduces the amount of work done and the cost of maintaining the use case.

4.2 Interface Calls

Interface calls can be implemented using Python's Third-party library-requests, requests based on Urllib, HTTP library with APACHE2 licensed Open source Protocol, support for HTTP connection retention and connection pooling, support for using cookies to hold sessions, Supports file uploads, supports automatic coding of response content, and supports internationalized URL and post data encoding automatically. It is more convenient than urllib, can save us a lot of work, fully meet the requirements of HTTP testing. HTTP requests using requests are very simple, with the following basic requests for GET and post.

Get:

Requests allows these parameters to be supplied in a dictionary using the params keyword parameter. The code above will request url:http://httpbin.org/get?key2=value2&key1=value1.

POST:

For post requests, requests supports automatic encoding, as long as the body is requested to pass in the JSON keyword parameter, the above code will request http://httpbin.org/post and request the body as: {"Key1": "Value1", "Key2" : "Value2"}, if the request body is not in JSON format, but in string format, it can be set directly with the Data keyword parameter. In addition, requests also automatically URL-encode the parameters.

If you need to add an HTTP header for the request, simply pass a dict to the header parameter:

The above code example is a POST request and, of course, the same for GET requests.

For the response to the request, requests also has a wealth of methods to deal with, a number of key methods:

Code is the status code of the response, R_text is the content of the response, and R_json is the JSON format of the response content.

4.3 Result assertion

Get the state and content of the response, you can use UnitTest Assertion Library to verify the results, first of all need to verify the response status code, which describes how to get the response status code, directly with the expected value of the sentence, such as:

Second, the response header, you may need to Content-type, content-length and other key fields to verify, the same from the response to get the value of the corresponding field, and then directly with the Assertrue assertion to verify.

Finally, the response content, HTTP interface response content in general there are three formats: JSON (most important), XML and HTML, the JSON format of the response can be directly requests with the JSON parser to parse, and then judge the value of the key keys, etc. The following code asserts whether "success" is "True" in the response; FOR XML-formatted responses, you can get the textual content of the response and then use Python's third-party library, such as lxml, to turn the XML text into a label tree, and then read the value and verify it by the corresponding XPath ; For HTML-formatted responses, get the text content of the response with a regular match or XPath to get the value that needs to be validated.

V. Summary

This paper introduces a method of H5 page interface function test using Python third party class library requests and Unit Test tool UnitTest, the main application scenario is the function test for the newly developed H5 page interface. Can simulate the manual test can not cover the exception scene and effectively reduce the test workload, and can be repeated testing, rapid verification of problems. Of course, for the function has been relatively stable interface, the need for other H5 interface testing tools to monitor the interface and quality assurance, the combination of a variety of ways to better control the quality of H5 page interface.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.