文創(chuàng)產(chǎn)品設(shè)計(jì)方案范本優(yōu)化關(guān)鍵詞排名推廣
【摘要】 Protocol Buffers 是谷歌開源的序列化與反序列化框架。它與語言無關(guān)、平臺無關(guān)、具有可擴(kuò)展的機(jī)制。用于序列化結(jié)構(gòu)化數(shù)據(jù),此工具對標(biāo) XML ,支持自動編碼,解碼。比 XML 性能好,且數(shù)據(jù)易于解析。更多有關(guān)工具的介紹可參考官網(wǎng)。Protocol Buffers官網(wǎng):https://developers.google.com/protocol-buffers?1Protocol B...
Protocol Buffers 是谷歌開源的序列化與反序列化框架。它與語言無關(guān)、平臺無關(guān)、具有可擴(kuò)展的機(jī)制。用于序列化結(jié)構(gòu)化數(shù)據(jù),此工具對標(biāo) XML ,支持自動編碼,解碼。比 XML 性能好,且數(shù)據(jù)易于解析。更多有關(guān)工具的介紹可參考官網(wǎng)。
Protocol Buffers官網(wǎng):https://developers.google.com/protocol-buffers?1
Protocol Buffers 為跨平臺設(shè)計(jì),以 Python 為例,使用者配置 .proto 文件,利用 Protocol Buffers 工具即可生成 Python 代碼,此代碼就是使用者想要的數(shù)據(jù)結(jié)構(gòu)。
如果編程語言換成了 Java ,使用者可使用相同的 .proto 文件,利用 Protocol Buffers 工具生成 Java 代碼,此代碼可被 Java 進(jìn)行解析。
這么做的好處是可以跨語言交流,試想 Java 與 Python 間的數(shù)據(jù)通信,只需要利用 .proto 確定格式,就可隨心編程,這個過程愜意無比?;?Protocol Buffers 的測試也無比舒服。
1080×303 63.9 KB
你可選擇自己的語言進(jìn)行測試,比如 Python 。由于數(shù)據(jù)格式基于 .proto 配置文件,獲取到這個文件即可生成數(shù)據(jù)類,比如下述 .proto 內(nèi)容通過 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;
}
測試人員的代碼只需導(dǎo)入 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 序列化后即可傳輸?shù)奖粶y對象。如果你的業(yè)務(wù)采用文件進(jìn)行數(shù)據(jù)傳輸,可參考官方寫文件的例子(采用 python2 ):
#! /usr/bin/pythonimport 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 = emailwhile True:number = raw_input("Enter a phone number (or leave blank to finish): ")if number == "":breakphone_number = person.phones.add()phone_number.number = numbertype = raw_input("Is this a mobile, home, or work phone? ")if type == "mobile":phone_number.type = addressbook_pb2.Person.PhoneType.MOBILEelif type == "home":phone_number.type = addressbook_pb2.Person.PhoneType.HOMEelif type == "work":phone_number.type = addressbook_pb2.Person.PhoneType.WORKelse: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()
也可從被測對象傳來的文件中讀數(shù)據(jù):
#! /usr/bin/pythonimport 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.idprint " Name:", person.nameif person.HasField('email'):print " E-mail address:", person.emailfor 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)
如果數(shù)據(jù)通過 https 傳輸,可采用 requests ,其它傳輸方式同理,請自行查閱數(shù)據(jù)傳輸工具。如果測試人員代碼與被測對象建立了聯(lián)系,即可收發(fā)測試數(shù)據(jù),測試人員對接收到的數(shù)據(jù)編寫測試用例即可。