写点什么

软件测试 / 测试开发丨 Pb 协议的接口测试

作者:测试人
  • 2023-03-20
    北京
  • 本文字数:2389 字

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

发布于: 刚刚阅读数: 5
用户头像

测试人

关注

专注于软件测试开发 2022-08-29 加入

霍格沃兹测试开发学社,测试人社区:https://ceshiren.com/t/topic/22284

评论

发布
暂无评论
软件测试/测试开发丨Pb协议的接口测试_软件测试_测试人_InfoQ写作社区