写点什么

软件测试 | pb 协议的接口测试

  • 2023-03-05
    北京
  • 本文字数:2309 字

    阅读完需:约 8 分钟

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/pythonimport 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/pythonimport 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 ,其它传输方式同理,请自行查阅数据传输工具。如果测试 人员代码与被测对象建立了联系,即可收发测试数据,测试人员对接收到的数据编写测试用例即可。


搜索微信公众号:TestingStudio 霍格沃兹的干货都很硬核

用户头像

社区:ceshiren.com 微信:ceshiren2023 2022-08-29 加入

微信公众号:霍格沃兹测试开发 提供性能测试、自动化测试、测试开发等资料、实事更新一线互联网大厂测试岗位内推需求,共享测试行业动态及资讯,更可零距离接触众多业内大佬

评论

发布
暂无评论
软件测试 | pb协议的接口测试_测试_测吧(北京)科技有限公司_InfoQ写作社区