乐趣区

关于软件测试:软件测试Pb协议的接口测试

Protocol Buffers 是谷歌开源的序列化与反序列化框架。它与语言无关、平台无关、具备可扩大的机制。用于序列化结构化数据,此工具对标 XML,反对自动编码,解码。比 XML 性能好,且数据易于解析。更多无关工具的介绍可参考官网。

Protocol Buffers 官网:https://developers.google.com…

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_pb2
person = addressbook_pb2.Person()
person.id = 1234
person.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_pb2
import 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_pb2
import 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,其它传输方式同理,请自行查阅数据传输工具。如果测试人员代码与被测对象建设了分割,即可收发测试数据,测试人员对接管到的数据编写测试用例即可。

退出移动版