测试 protobuff 消息中是否存在重复字段

Ysh*_*Ysh 5 python unit-testing protocol-buffers

我有一条 google protobuf 消息:

message Foo {
  required int bar = 1;
}
Run Code Online (Sandbox Code Playgroud)

我知道为了测试消息的字段,我们可以使用:

foo.bar = 1
assert foo.HasField("bar")
Run Code Online (Sandbox Code Playgroud)

但是“HasField”不适用于重复的字段类型。如何测试“重复类型”字段的字段是否存在?

message Foo {
  repeated int bar = 1;
}
Run Code Online (Sandbox Code Playgroud)

Emm*_*Jay 4

您可以尝试测试重复字段的长度:

assert len(foo.bar) == 0
Run Code Online (Sandbox Code Playgroud)