Interface test of Pb protocol

Posted by figo2476 on Tue, 04 Jan 2022 13:06:56 +0100

This article is excerpted from the internal textbook of Hogwarts Testing Institute

Protocol Buffers is Google's open source serialization and deserialization framework. It has nothing to do with language, platform and extensible mechanism. Used to serialize structured data, this tool benchmarkes XML
, support automatic encoding and decoding. It has better performance than XML, and the data is easy to parse. For more information about the tools, please refer to the official website.

Protocol Buffers official website: https://developers.google.com/protocol-buffers

Protocol Buffers are designed for cross platform, taking Python as an example and configured by users proto file, using Protocol Buffers
The tool can generate Python code, which is the data structure that the user wants.

If the programming language is changed to Java, users can use the same proto file, using Protocol Buffers tool to generate Java
Code, which can be parsed by Java.

The advantage of this is that it can communicate across languages. Imagine the data communication between Java and Python proto
After determining the format, you can program at will. This process is very comfortable. Protocol Buffers based testing is also very comfortable.

You can choose your own language to test, such as Python. Because the data format is based on proto configuration file. After obtaining this file, you can generate data classes, such as the following
. proto content through protoc --python_out=./ ./addressbook.proto command can be generated
addressbook_pb2.py file:

syntax = "proto2";  
package tutorial;  
message Person {  optional string name = 1;  optional int32 id = 2;  optional string email = 3;  
  enum PhoneType {    MOBILE = 0;    HOME = 1;    WORK = 2;  }  
  message PhoneNumber {    optional string number = 1;    optional PhoneType type = 2 [default = HOME];  }  
  repeated PhoneNumber phones = 4;}  
message AddressBook {  repeated Person people = 1;}

The tester's code simply imports the addressbook_pb2, which can be used after initialization. For example, add some test values to the Person field:

import addressbook_pb2person = addressbook_pb2.Person()person.id = 1234person.name = "John Doe"person.email = "jdoe@example.com"phone = person.phones.add()phone.number = "555-4321"phone.type = addressbook_pb2.Person.HOME

Finally, after serializing the person, it can be transferred to the tested object. If your business uses files for data transmission, you can refer to the official example of writing files (using python2):

#! /usr/bin/python  
import addressbook_pb2import sys  
# This function fills in a Person message based on user input.def PromptForAddress(person):  person.id = int(raw_input("Enter person ID number: "))  person.name = raw_input("Enter name: ")  
  email = raw_input("Enter email address (blank for none): ")  if email != "":    person.email = email  
  while True:    number = raw_input("Enter a phone number (or leave blank to finish): ")    if number == "":      break  
    phone_number = person.phones.add()    phone_number.number = number  
    type = raw_input("Is this a mobile, home, or work phone? ")    if type == "mobile":      phone_number.type = addressbook_pb2.Person.PhoneType.MOBILE    elif type == "home":      phone_number.type = addressbook_pb2.Person.PhoneType.HOME    elif type == "work":      phone_number.type = addressbook_pb2.Person.PhoneType.WORK    else:      print "Unknown phone type; leaving as default value."  
# Main procedure:  Reads the entire address book from a file,#   adds one person based on user input, then writes it back out to the same#   file.if len(sys.argv) != 2:  print "Usage:", sys.argv[0], "ADDRESS_BOOK_FILE"  sys.exit(-1)  
address_book = addressbook_pb2.AddressBook()  
# Read the existing address book.try:  f = open(sys.argv[1], "rb")  address_book.ParseFromString(f.read())  f.close()except IOError:  print sys.argv[1] + ": Could not open file.  Creating a new one."  
# Add an address.PromptForAddress(address_book.people.add())  
# Write the new address book back to disk.f = open(sys.argv[1], "wb")f.write(address_book.SerializeToString())f.close()

Data can also be read from the file transmitted from the tested object:

#! /usr/bin/python  
import addressbook_pb2import sys  
# Iterates though all people in the AddressBook and prints info about them.def ListPeople(address_book):  for person in address_book.people:    print "Person ID:", person.id    print "  Name:", person.name    if person.HasField('email'):      print "  E-mail address:", person.email  
    for phone_number in person.phones:      if phone_number.type == addressbook_pb2.Person.PhoneType.MOBILE:        print "  Mobile phone #: ",      elif phone_number.type == addressbook_pb2.Person.PhoneType.HOME:        print "  Home phone #: ",      elif phone_number.type == addressbook_pb2.Person.PhoneType.WORK:        print "  Work phone #: ",      print phone_number.number  
# Main procedure:  Reads the entire address book from a file and prints all#   the information inside.if len(sys.argv) != 2:  print "Usage:", sys.argv[0], "ADDRESS_BOOK_FILE"  sys.exit(-1)  
address_book = addressbook_pb2.AddressBook()  
# Read the existing address book.f = open(sys.argv[1], "rb")address_book.ParseFromString(f.read())f.close()  
ListPeople(address_book)

If the data is transmitted through https, requests can be used
, the same is true for other transmission methods. Please consult the data transmission tools yourself. If the tester code has established contact with the tested object, it can send and receive test data, and the tester can write test cases for the received data.

** _
Come to Hogwarts test and development society to learn more advanced technologies of software testing and test development. The knowledge points include web automated testing, app automated testing, interface automated testing, test framework, performance testing, security testing, continuous integration / continuous delivery / DevOps, test left, test right, precision testing, test platform development, test management, etc, The course technology covers bash, pytest, junit, selenium, appium, postman, requests, httprunner, jmeter, jenkins, docker, k8s, elk, sonarqube, Jacobo, JVM sandbox and other related technologies, so as to comprehensively improve the technical strength of test and development engineers
QQ communication group: 484590337
The official account TestingStudio
Click for more information