Python implements automated testing of interfaces based on RPC protocol

What is RPC

The RPC (Remote Procedure Call) remote procedure call protocol is a protocol for establishing an appropriate framework. Essentially, it enables a program on one machine to call a subroutine on another without realizing it's remote.

RPC is a software communication protocol that a program can use to request services from a program located on another computer on a network without having to know the details of the network. RPC is used to call other processes on the remote system just like the local system. A procedure call is also sometimes called a function call or a subroutine call

02 RPC framework

RPC itself is a set of protocol specifications. The following frameworks are more popular to implement according to this set of specifications:

grpc framework

gRPC is a high-performance, general-purpose open source RPC framework developed by Google. It is mainly designed for mobile application development and based on the HTTP/2 protocol standard, and supports most popular programming languages. gRPC is based on HTTP/2 protocol transmission

dubbo framework

Dubbo is a high-performance and excellent service framework open sourced by Alibaba, which enables applications to realize the output and input functions of services through high-performance RPC, and can be seamlessly integrated with the Spring framework.

Dubbo is a high-performance, lightweight open source Java RPC framework, which provides three core capabilities:

① Interface-oriented remote method invocation

② Intelligent fault tolerance and load balancing

③ Service automatic registration and discovery

thrift framework

Thrift is an interface description language and binary communication protocol. Originally developed by Facebook in 2007, it was officially submitted to the Apache Foundation for hosting in 2008 and became an open source project under Apache.

Thrift is an RPC communication framework designed with a custom binary communication protocol. Compared with the traditional HTTP protocol, it is more efficient and takes up less bandwidth for transmission. Also, Thrift is cross-language

Hetty frame

Hetty is a high-performance RPC framework built on the basis of Netty and Hessian

Hessian is an RPC framework based on the HTTP protocol, which uses the binary RPC protocol, which is very lightweight and fast.

Netty is an event-driven NIO framework for rapid development of high-performance, high-reliability web server and client programs. The Hetty client is completely implemented by Hessian, but the server is re-implemented using Netty
 

03 Interface test based on grpc framework service

01 Create a grpc service interface

Note that your grpc service in the company may not be implemented using python. In order to test the grpc interface in class, we use python to create the grpc service interface first.

Install dependent libraries

 

Create grpc interface protocol document

Create a grpc_study package under the project, create an add.proto file under the package, and copy the following content into it

 

generate service code

Under the terminal, enter the directory where the proto file is located, and execute the following command:

create server

 

 

02 Call the grpc interface client

The following code is to call the grpc interface in the first step

 

03 Adapt grpc encapsulation in the interface framework

1. Encapsulate the underlying channel initialization

Create grpc.yml in the config directory, and write the address of the grpc interface in it:

 Add the following code to the client in the common directory:

 Add the following code in conftes.py:

 

2. Encapsulate the grpc interface call

Create a grpcapi package under the api package, copy the definition proto file of grpc into it, and then enter the grpcapi directory under the terminal to execute the following command

 The files in this directory are as follows:

 Modify the import in add_pb2_grpc.py as follows:

Encapsulate the interface call according to the above code, create an api_client.py, and write the following code to call the add interface provided by grpc 

 3. Write grpc interface test cases

Create a grpcapi package under the testcases package, create a test_grpc_api.py, and write a test case for the add interface as follows:

 Execute the test

04 Interface test based on dubbo framework service

dubbo service management address:

http://...:**/dubbo-admin-2.6.0

Username and password are both root

01 Dubbo service management is easy to use

First set to Chinese, select the language on the right

Choose Service Governance - Service 

 Click to open MarketService, then click ip and port, open as follows:

 

02 Actual interface description
In the first step, we saw two interfaces, exchange and lottery. The following is the business description of these two interfaces

exchange

The business is point exchange. The parameter of this interface is an object. The object type corresponds to a certain class cn.testfan.dubbo.model.ExchangeRequest in the background java code. The properties corresponding to this class object are as follows. These properties are actually our parameters.

lottery

The business is a lottery. There are two parameters, both of which are numbers. There is no parameter name. In order, the first one represents the participating activity id, and the second one represents the user id.

03 Python calls dubbo interface

Install third-party libraries 

 dubbo interface call

 

04 Adapt the dubbo package in the interface framework

1. Encapsulate the underlying dubbo initialization

Create a dubbo.yml file in the config directory and write the following content

Add the following code in client.py 

 

2. Encapsulate the api layer

The dubbo interface is usually divided according to the service. There are multiple interfaces under one service, and an object is created for the service, and then each interface is called.

Create a dubboapi package under the api package, create a market_api.py file for the market service, and write the following code:

 

3. Test case layer

Create the dubboapi package under the testcases package, create test_market_service.py under it, and write the following code

 

Information sharing

The complete software testing video learning tutorial below has uploaded the QR code officially certified by CSDN. If you need it, you can get it for free 【保证100%免费】

Guess you like

Origin blog.csdn.net/lzz718719/article/details/130683246