最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

Pb協(xié)議的接口測(cè)試

2022-11-01 14:37 作者:愛測(cè)軟件測(cè)試  | 我要投稿

Protocol Buffers 是谷歌開源的序列化與反序列化框架。它與語(yǔ)言無關(guān)、平臺(tái)無關(guān)、具有可擴(kuò)展的機(jī)制。用于序列化結(jié)構(gòu)化數(shù)據(jù),此工具對(duì)標(biāo) XML ,支持自動(dòng)編碼,解碼。比 XML 性能好,且數(shù)據(jù)易于解析。更多有關(guān)工具的介紹可參考官網(wǎng)。

Protocol Buffers官網(wǎng):https://developers.google.com/protocol-buffers 1

Protocol Buffers 為跨平臺(tái)設(shè)計(jì),以 Python 為例,使用者配置 .proto 文件,利用 Protocol Buffers 工具即可生成 Python 代碼,此代碼就是使用者想要的數(shù)據(jù)結(jié)構(gòu)。

如果編程語(yǔ)言換成了 Java ,使用者可使用相同的 .proto 文件,利用 Protocol Buffers 工具生成 Java 代碼,此代碼可被 Java 進(jìn)行解析。

這么做的好處是可以跨語(yǔ)言交流,試想 Java 與 Python 間的數(shù)據(jù)通信,只需要利用 .proto 確定格式,就可隨心編程,這個(gè)過程愜意無比?;?Protocol Buffers 的測(cè)試也無比舒服。

你可選擇自己的語(yǔ)言進(jìn)行測(cè)試,比如 Python 。由于數(shù)據(jù)格式基于 .proto 配置文件,獲取到這個(gè)文件即可生成數(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;?

}

測(cè)試人員的代碼只需導(dǎo)入 addressbook_pb2 ,對(duì)其初始化后即可使用,比如對(duì) Person 的字段加入一些測(cè)試值:

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(cè)對(duì)象。如果你的業(yè)務(wù)采用文件進(jìn)行數(shù)據(jù)傳輸,可參考官方寫文件的例子(采用 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()

也可從被測(cè)對(duì)象傳來的文件中讀數(shù)據(jù):

#! /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)

如果數(shù)據(jù)通過 https 傳輸,可采用 requests ,其它傳輸方式同理,請(qǐng)自行查閱數(shù)據(jù)傳輸工具。如果測(cè)試人員代碼與被測(cè)對(duì)象建立了聯(lián)系,即可收發(fā)測(cè)試數(shù)據(jù),測(cè)試人員對(duì)接收到的數(shù)據(jù)編寫測(cè)試用例即可。


Pb協(xié)議的接口測(cè)試的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
苍南县| 林芝县| 思茅市| 盱眙县| 湄潭县| 洛川县| 赤水市| 永城市| 洛浦县| 贡觉县| 祁阳县| 泉州市| 宜章县| 临安市| 吉水县| 万年县| 琼结县| 三门峡市| 涿鹿县| 珲春市| 灵宝市| 海伦市| 青浦区| 闻喜县| 丹凤县| 含山县| 宝山区| 镶黄旗| 泸溪县| 峨山| 聊城市| 中卫市| 奉化市| 高邮市| 板桥市| 佳木斯市| 鹿泉市| 齐齐哈尔市| 会东县| 股票| 阜宁县|