我在此处测试了以下yaml脚本:online yaml parser
testcases:
- testcase:
desc: 'TEST TC1'
requirement-ref: Doors-10.1.1.0
given:
text: 'A UUT, TEST2 and TEST are connected'
devices:
- Device:
Type: UUT
Status: Connected
- Device:
Type: TEST
Status: Connected
- Device:
Type: TEST2
Status: Connected
- testcase: # next test case...
desc: 'TEST TC2'
然后我有以下代码:
#include <iostream>
#include <unistd.h>
#include <ctime>
#include "yaml-cpp/yaml.h"
using namespace std;
void load_config()
{
cout << "loading yaml file\n";
YAML::Node testfile = YAML::LoadFile("BDD-test-case.yaml");
if (testfile["testcases"])
{
cout << "found testfile[\"testcases\"] - size: " << testfile["testcases"].size() << "\n";
YAML::Node testcases = testfile["testcases"];
for (int i = 0; i < testcases.size(); i++)
{
YAML::Node testcase = testcases[0];
cout << "testcase " << i << ":\n";
cout << "\tsize:" << testcase.size() << "\n";
cout << "\tIsNull:" << testcase.IsNull() << "\n";
YAML::Node desc = testcase["desc"];
if (desc)
{
std::string desc_str = desc.as<std::string>();
cout << "desc: " << desc_str.c_str() << "\n";
}
}
}
cout << "yaml - done\n";
}
int main(int argc, char *argv[])
{
long double lastTimeMS = time(0)*1000;
long double CurrTimeMS = time(0)*1000;
int localPort = 31010;
int remotePort = 31011;
int procId = 1;
load_config();
cout << "Hello World!" << endl;
return 0;
}
我得到以下输出:
loading yaml file
found testfile["testcases"] - size: 2
testcase 0:
size:1
IsNull:0
testcase 1:
size:1
IsNull:0
yaml - done
Hello World!
我正在尝试解析每个测试用例并打印出“desc”字段的值。
从调试中可以看出,我能够找到顶级节点testcases
。我检查它的大小,它是2 - 这是正确的。然后我遍历测试用例并将每个测试用例存储为新节点testcase = testcases[i]
。到现在为止还挺好。但是现在我想看看每个测试用例里面的内容 - 我应该找到一个tsetcase [“desc”] ......但是我没有看到。事实上,testcase似乎是空的......
我在这里做错了什么?
答案 0 :(得分:1)
当你抬头时:
YAML::Node desc = testcase["desc"];
您正在尝试在以下节点中查找desc
密钥:
testcase:
desc: 'TEST TC1'
requirement-ref: Doors-10.1.1.0
given:
text: 'A UUT, TEST2 and TEST are connected'
devices:
- Device:
Type: UUT
Status: Connected
- Device:
Type: TEST
Status: Connected
- Device:
Type: TEST2
Status: Connected
但此节点有一个顶级地图键testcase
。你可能并不打算这样做;我想如果您只是删除该密钥,那么您的文档看起来就像是您的意图。
<强>更新强>
固定的YAML(删除了额外级别的&#39;标记&#39;非预期):
testcases:
- desc: 'TEST TC1'
requirement-ref: Doors-10.1.1.0
given:
text: 'A UUT, TEST2 and TEST are connected'
devices:
- Type: UUT
Status: Connected
- Type: TEST
Status: Connected
- Type: TEST2
Status: Connected
- desc: 'TEST TC2'