本文节选自霍格沃兹测试学院内部教材
Protocol Buffers 是谷歌开源的序列化与反序列化框架。它与语言无关、平台无关、具有可扩展的机制。用于序列化结构化数据,此工具对标 XML
,支持自动编码,解码。比 XML 性能好,且数据易于解析。更多有关工具的介绍可参考官网。
Protocol Buffers官网:https://developers.google.com/protocol-buffers
Protocol Buffers 为跨平台设计,以 Python 为例,使用者配置 .proto 文件,利用 Protocol Buffers
工具即可生成 Python 代码,此代码就是使用者想要的数据结构。
如果编程语言换成了 Java ,使用者可使用相同的 .proto 文件,利用 Protocol Buffers 工具生成 Java
代码,此代码可被 Java 进行解析。
这么做的好处是可以跨语言交流,试想 Java 与 Python 间的数据通信,只需要利用 .proto
确定格式,就可随心编程,这个过程惬意无比。基于 Protocol Buffers 的测试也无比舒服。
你可选择自己的语言进行测试,比如 Python 。由于数据格式基于 .proto 配置文件,获取到这个文件即可生成数据类,比如下述
.proto 内容通过 protoc --python_out=./ ./addressbook.proto
命令即可生成
addressbook_pb2.py 文件:
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;}
测试人员的代码只需导入 addressbook_pb2 ,对其初始化后即可使用,比如对 Person 的字段加入一些测试值:
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
最后,将 person 序列化后即可传输到被测对象。如果你的业务采用文件进行数据传输,可参考官方写文件的例子(采用 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()
也可从被测对象传来的文件中读数据:
#! /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)
如果数据通过 https 传输,可采用 requests
,其它传输方式同理,请自行查阅数据传输工具。如果测试人员代码与被测对象建立了联系,即可收发测试数据,测试人员对接收到的数据编写测试用例即可。
** _
来霍格沃兹测试开发学社,学习更多软件测试与测试开发的进阶技术,知识点涵盖web自动化测试 app自动化测试、接口自动化测试、测试框架、性能测试、安全测试、持续集成/持续交付/DevOps,测试左移、测试右移、精准测试、测试平台开发、测试管理等内容,课程技术涵盖bash、pytest、junit、selenium、appium、postman、requests、httprunner、jmeter、jenkins、docker、k8s、elk、sonarqube、jacoco、jvm-sandbox等相关技术,全面提升测试开发工程师的技术实力
QQ交流群:484590337
公众号 TestingStudio
点击获取更多信息