『壹』 yaml配置文件的@project.name@不识别
直接在根目录陵轮的pom文件的build下面添加如下配旦携置尺迟信即可。
在yaml文件配置即可
『贰』 C语言操作yaml配置文件通用操作工具
在go语言中使用viper之类的库很方便的处理yaml配置文件,但是在c语言中就比较麻烦,经过一番思索和借助强大的github,发现了一个libyaml c库,但是网上的例子都比较麻烦,而且比较繁琐,就想法作了一个相对比较容易配置的解析应用,可以简单地类似viper 的模式进行配置实现不同的配置文件读取。如你的配置文件很复杂请按格式修改KeyValue 全局变量,欢迎大家一起完善
库请自行下载 GitHub - yaml/libyaml: Canonical source repository for LibYAML
直接上代码
yaml示例文件
%YAML 1.1
---
mqtt:
subtopic: "Control/#"
pubtopic: "bbt"
qos: 1
serveraddress: "tcp://192.168.0.25:1883"
clientid: "kvm_test"
writelog: false
writetodisk: false
outputfile: "./receivedMessages.txt"
hearttime: 30
#ifndef __CONFIG_H__
#define __CONFIG_H__
#ifdef __cplusplus
extern "C" {
#endif
/************************/
/* Minimum YAML version */
/************************/
#define YAML_VERSION_MAJOR 1
#define YAML_VERSION_MINOR 1
#define STRUCT_TYPE_NAME 100
#define INT_TYPE_NAME 101
#define STRING_TYPE_NAME 102
#define BOOL_TYPE_NAME 103
#define FLOAT_TYPE_NAME 104
#define MAP_TYPE_NAME 105
#define LIST_TYPE_NAME 106
typedef struct{
char *key;
void *value;
int valuetype;
char *parent;
}KeyValue,*pKeyValue;
#ifdef __cplusplus
}
#endif
#endif
#include
#include
#include
#include
#include
#include
#include
#include "config.h"
typedef struct {
char *SUBTOPIC; //string `yaml:"subtopic" mapstructure:"subtopic"` //"topic1"
char *PUBTOPIC; //string `yaml:"pubtopic" mapstructure:"pubtopic"`
int QOS; //byte `yaml:"qos" mapstructure:"qos"` //1
char *SERVERADDRESS; //string `yaml:"serveraddress" mapstructure:"serveraddress"` //= "tcp://mosquitto:1883"
char *CLIENTID; //string `yaml:"clientid" mapstructure:"clientid"` //= "mqtt_subscriber"
int HEARTTIME; //int `yaml:"hearttime" mapstructure:"hearttime"`
// CommandLocalPath string `yam:"commanlocalpath"`
}mqttSection,*pmqttSection;
typedef struct {
mqttSection Mqtt;// `yaml:"mqtt" mapstructure:"mqtt"`
// KVM kvmSection `yaml:"kvm" mapstructure:"kvm"`
}ConfigT;
ConfigT config;
static KeyValue webrtcconfig[]={
{"mqtt",&config,STRUCT_TYPE_NAME,NULL},
{"subtopic",&(config.Mqtt.SUBTOPIC),STRING_TYPE_NAME,"mqtt"},
{"pubtopic",&(config.Mqtt.PUBTOPIC),STRING_TYPE_NAME,"mqtt"},
{"qos",&(config.Mqtt.QOS),INT_TYPE_NAME,"mqtt"},
{"serveraddress",&(config.Mqtt.SERVERADDRESS),STRING_TYPE_NAME,"mqtt"},
{"clientid",&(config.Mqtt.CLIENTID),STRING_TYPE_NAME,"mqtt"},
{"hearttime",&(config.Mqtt.HEARTTIME),INT_TYPE_NAME,"mqtt"},
{NULL,NULL,0,NULL},
};
int printConfig(ConfigT * pconfig){
if(pconfig==NULL) return -1;
printf("mqtt:r ");
if(pconfig->Mqtt.SUBTOPIC!=NULL) {printf("subtopic: %sr ",pconfig->Mqtt.SUBTOPIC); }
if(pconfig->Mqtt.SUBTOPIC!=NULL) {printf("pubtopic: %sr ",pconfig->Mqtt.PUBTOPIC); }
printf("qos: %dr ",config.Mqtt.QOS);
if(pconfig->Mqtt.SERVERADDRESS!=NULL) {printf("serveraddress: %sr ",pconfig->Mqtt.SERVERADDRESS); }
if(pconfig->Mqtt.CLIENTID!=NULL) {printf("clientid: %sr ",pconfig->Mqtt.CLIENTID); }
printf("hearttime: %dr ",config.Mqtt.HEARTTIME);
}
int freeConfig(ConfigT * pconfig){
if(pconfig==NULL) return -1;
if(pconfig->Mqtt.SERVERADDRESS!=NULL) {free(pconfig->Mqtt.SERVERADDRESS); }
if(pconfig->Mqtt.CLIENTID!=NULL) {free(pconfig->Mqtt.CLIENTID); }
if(pconfig->Mqtt.SUBTOPIC!=NULL) {free(pconfig->Mqtt.SUBTOPIC); }
}
char currentkey[100];
void getvalue(yaml_event_t event,pKeyValue *ppconfigs){
char *value = (char *)event.data.scalar.value;
pKeyValue pconfig=*ppconfigs;
char *pstringname;
while(pconfig->key!=NULL){
if(currentkey[0]!=0){
if(!strcmp(currentkey,pconfig->key))
{
switch(pconfig->valuetype){
case STRING_TYPE_NAME:
pstringname=strp(value);
printf("get string value %sr ",pstringname);
*((char**)pconfig->value)=pstringname;
memset(currentkey, 0, sizeof(currentkey));
break;
case INT_TYPE_NAME:
*((int*)(pconfig->value))=atoi(value);
memset(currentkey, 0, sizeof(currentkey));
break;
case BOOL_TYPE_NAME:
if(!strcmp(value,"true")) *((bool*)(pconfig->value))=true;
else *((bool*)(pconfig->value))=false;
memset(currentkey, 0, sizeof(currentkey));
break;
case FLOAT_TYPE_NAME:
*((float*)(pconfig->value))=atof(value);
memset(currentkey, 0, sizeof(currentkey));
break;
case STRUCT_TYPE_NAME:
case MAP_TYPE_NAME:
case LIST_TYPE_NAME:
memset(currentkey, 0, sizeof(currentkey));
strncpy(currentkey,value,strlen(value));
break;
default:
break;
}
break;
}
//continue;
}else{
if(!strcmp(value,pconfig->key)){
strncpy(currentkey,pconfig->key,strlen(pconfig->key));
break;
}
}
pconfig++;
}
}
int Load_YAML_Config( char *yaml_file, KeyValue *(configs[]) )
{
struct stat filecheck;
yaml_parser_t parser;
yaml_event_t event;
bool done = 0;
unsigned char type = 0;
unsigned char sub_type = 0;
if (stat(yaml_file, &filecheck) != false )
{
printf("[%s, line %d] Cannot open configuration file '%s'! %s", __FILE__, __LINE__, yaml_file, strerror(errno) );
return -1;
}
FILE *fh = fopen(yaml_file, "r");
if (!yaml_parser_initialize(&parser))
{
printf("[%s, line %d] Failed to initialize the libyaml parser. Abort!", __FILE__, __LINE__);
return -1;
}
if (fh == NULL)
{
printf("[%s, line %d] Failed to open the configuration file '%s' Abort!", __FILE__, __LINE__, yaml_file);
return -1;
}
memset(currentkey, 0, sizeof(currentkey));
/* Set input file */
yaml_parser_set_input_file(&parser, fh);
while(!done)
{
if (!yaml_parser_parse(&parser, &event))
{
/* Useful YAML vars: parser.context_mark.line+1, parser.context_mark.column+1, parser.problem, parser.problem_mark.line+1, parser.problem_mark.column+1 */
printf( "[%s, line %d] libyam parse error at line %ld in '%s'", __FILE__, __LINE__, parser.problem_mark.line+1, yaml_file);
}
if ( event.type == YAML_DOCUMENT_START_EVENT )
{
//yaml file first line is version
//%YAML 1.1
//---
yaml_version_directive_t *ver = event.data.document_start.version_directive;
if ( ver == NULL )
{
printf( "[%s, line %d] Invalid configuration file. Configuration must start with "%%YAML 1.1"", __FILE__, __LINE__);
}
int major = ver->major;
int minor = ver->minor;
if (! (major == YAML_VERSION_MAJOR && minor == YAML_VERSION_MINOR) )
{
printf( "[%s, line %d] Configuration has a invalid YAML version. Must be 1.1 or above", __FILE__, __LINE__);
return -1;
}
}
else if ( event.type == YAML_STREAM_END_EVENT )
{
done = true;
}
else if ( event.type == YAML_MAPPING_END_EVENT )
{
sub_type = 0;
}
else if ( event.type == YAML_SCALAR_EVENT )
{
getvalue(event,configs);
}
}
return 0;
}
int main(int argc, char *argv[]){
pKeyValue pconfig=&webrtcconfig[0];
Load_YAML_Config("../../etc/kvmagent.yml",&pconfig);
printConfig(&config);
freeConfig(&config);
}
『叁』 2020-06-15关于iOS 使用yaml配置文件总结
yaml,json,ini这三种格式用来做配置文件优缺点
适合人类编写:ini > toml > yaml > json > xml > plist
可以存储的数据复杂度:xml > yaml > toml ~ json ~ plist > ini
yaml解析库: https://github.com/mirek/YAML.framework
编译导出iOS版本的framwork, YAML.framework
导入进你的iOS工程,这里有个坑
但是发现虽然不报错了,但是解析不出数据
里,然后就可以正常使用了
结果
附上yaml格式文件的编写规则: https://www.jianshu.com/p/5d7cc8690f15
它的基本语法规则如下:
大小写敏感
使用缩进表示层级关系
缩进时不允许使用Tab键,只允许使用空格。
缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
纯量是最基本的、不可再分的值。以下数据类型都属于 JavaScript 的纯量。
后面引用和正则表达式是一些语法,参 https://blog.csdn.net/michaelhan3/article/details/69664932
『肆』 calsh无法启动主程序
calsh无法启动主程序?首先是我的解决方法,直接关闭windows防火墙,如果你不想关闭也可以采用以下方法:
打开windows安全喊雀中心首渗闭--防火墙与网络保护--允许应用通过防火墙--更改设置--允许其他应用--浏览--选择clash目录下的resources\static\files\win\x64\clash-win64.exe--点添加
我是win10系统,其他系统没测试。
2.如果还不行,设置那一栏有个Secret核心,点更新,重新导入订阅的URL链接。
3.clash 启动失败,者裂显示Parse config error: proxy 0: TLS must be true with h2/grpc network
clash 的 vmess的H2添加服务器里面没有 TLS的选项,所以启动不起来
在yaml文件 添加一个 tls的设置。
4.关闭杀毒软件,以管理员方式运行。
5.端口被占用了,参考 https://blog.51cto.com/u_15127578/4207165 这位大佬得心得!
『伍』 YAML 配置文件语言
YAML 是专门用来写配置文件的语言,非常简洁和强大,远比 JSON 格式方便。YAML 语言的设计目标,就是方便人类读写。它实质上是一种通用的数据串行化格式。它的基本语法规则如下:
对象的一组键值对,使用冒号结构表示
一组连词线开头的行,构成一个数组
数据结构的子成员是一个数组,则可以在该项下面缩进一个空格
对象和数组可以结合使用,形成复合结构
纯量是最基本的、不可再分的值。以下数据类型都属于 JavaScript 的纯量
课程演示会采用多虚拟机模拟分布式场景,为防止 IP 冲突,无法联网等问题,需要预先设置好主机名、IP、DNS 配置
修改 cloud.cfg 防止重启后主机名还原
修改主机名
编辑 vi /etc/netplan/50-cloud-init.yaml 配置文件,修改内容如下
使用 netplan apply 命令让配置生效
『陆』 glados使用linux
glados支持Linux和网关路由器等全平台客户端。
从中选择适合自己设备的版本,以下以腊哪神clash-linux-amd64-v1.8.0.gz为例。将准备缓激好的yaml文件(如config.yaml)复制到clash的目录。
即可启动clash,启动后还需要更改系统代理,默认情况下的地址和端口,参数是指定config的位置,由于将config.yaml放在了clash目录下,因此轮亏需要指定当前工作目录为config位置。
『柒』 Golang项目中引入yaml.v2配置文件
在Go语言项目中,常用的配置文件yaml、toml、json、xml、ini几种,因为本章主要讲解yaml配置文件的使用方法,其他几种配置文件在这里就不展开了介绍了,大家有兴趣可以自行网络。
yaml文件的语法网上有很多的教程,大家自行网络,这里也推荐两个链接:
yaml文件解析使用的是github上第三方开源框架 gopkg.in/yaml.v2 ,下面详细介绍安装和使用的方法:
参考链接: https://blog.csdn.net/wade3015/article/details/83351776
『捌』 SpringBoot中yaml文件配置属性
首先,在SpringBoot中,有两种配置文件的方式。一种是application.properties,另一种application.yaml(或者是application.yml)。
yaml文件格式是SpringBoot支持的一种JSON超集文件格式,相对于传统的Properties配置文件,yaml文件以数据为核心,是一种更为直观且容易被计算机识别的数据序列化格式。application.yaml配置文件的工作原理和application.properties是一样的,只是yaml格式配置文件看起来要跟简洁一些。
application.yaml文件使用 key:(空格) value 格式配置属性,使用缩进控制层关系
注意:此时port和path属性,属于同一层级
其中缩进式写法有两种表示形式,一种为:
另一种为:
上述两种缩进式写法为person对象的hobby属性赋值,其中一种是通过“-(空格)属性值”的形式为属性赋值,另一种是直接赋值使用英文逗号分隔属性值。
行内式的写法显然比缩进式写法更加简洁。使用行内式写法设置属性值时,中括号“[ ]”是可以省略的,程序会自动匹配校对属性的值
在yaml配置的属性值为Map或对象类型时,缩进式的形式按照yaml文件格式编写即可,而行内式写法的属性值要用大括号“{ }”包含
『玖』 shellclash清理配置文件
config.yaml:clash基础配置文件。
默认安装目录为/etc/clash,可手动指定任意目录安装,安装完成后可以使用echo$clashdir命令查询安装目录。目录包含以下文件:1、clash:clash核心文件。
2、clash.sh/start.sh/getdate.sh:ShellClash运行脚本文件。
3、Country.mmdb:Geoip数据库文件。
4、config.yaml:clash基础配置文件。
5、config.yaml.bak:clash配置文件备份。
6、mark:脚本运行配置文件。
7、mac:脚本mac过滤功能配置文件等。
『拾』 使用yaml文件管理测试数据
知道ddt的基本使用方法之后,练习把之前用excel文件来维护的接口测试用例改用unittest+ddt来实现。
这里我选用yaml文件来管理接口参数,开始本来想用json,但是json无法添加注释,可读性不好。
下面截图是接口文档中的各个接口,每个接口都有一个固定的序号,所以在设计每个接口的测试数据时,以序号来区分不同接口
yaml文件内容如下,需要注意的是yaml的语法:
(1)键值对用冒号分割,但是冒号后需要加一个空格
(2)禁止使用tab缩进,只能使用空格键;缩进长度没有限制,只要元素对齐就表示这些元素属于一个层级
(3)字符串可以不用引号标注,也可以加引号,如果想把数字变为字符串,加引号即可
(4)使用#表示注释
详情可以参考博客: https://blog.csdn.net/vincent_hbl/article/details/75411243
2. 简单 demo : python 读取 yaml 文件,取出接口参数
import yaml
fp = open('../dataconfig/信息互动模块接口.yaml', encoding='utf-8') #有中文字符的话,加编码格式
testdata = yaml.load(fp)
t = testdata['5.2.1.4']
print(t)
(1)封装读取yaml文件方法
handle_yaml.py
# coding: utf-8
# author: hmk
importyaml
importos
classHandleYaml:
def __init__(self,file_path=None):
if file_path:
self.file_path = file_path
else:
root_dir =os.path.dirname(os.path.abspath('.'))
# os.path.abspath('.')表示获取当前文件所在目录;os.path.dirname表示获取文件所在父目录;所以整个就是项目的所在路径self.file_path = root_dir +'/dataconfig/信息互动模块接口.yaml' #获取文件所在的相对路径(相对整个项目)
#elf.data = self.get_data()
def get_data(self):
fp =open(self.file_path, encoding='utf-8')
data =yaml.load(fp)
return data
if __name__ == '__main__':
test = HandleYaml()
p = test.get_data()
print(p['5.2.1.1'])
[if !vml][endif]
(2)封装requests请求方法
[if !vml][endif]
# coding: utf-8
# author: Archer
importrequests
importjson
classRunMethod:
defpost_main(self, url, data, header=None):if header is notNone:
res =requests.post(url=url, data=data, headers=header)
else:
res =requests.post(url=url, data=data)
# print(res.status_code)
# return res.json()
return res #为了方便后面断言,这里不再对服务器响应进行json格式编码
def get_main(self, url, data=None, header=None):if header is notNone:
res =requests.get(url=url, params=data, headers=header)
else:
res =requests.get(url=url, params=data)
print(res.status_code)
# return
res.json()
return res
def run_main(self, method, url, data=None, header=None):
if method== 'POST':
res =self.post_main(url, data, header)
else:
res =self.get_main(url, data, header)
returnres
# returnjson.mps(res, indent=2, sort_keys=False, ensure_ascii=False) #使用json模块格式化显示结果
[if !vml][endif]
(3)一个接口测试用例
[if !vml][endif]
# coding: utf-8
# author: Archer
importunittest
importddt
from base.run_method importRunMethod
from utils.handle_yaml importHandleYaml
get_data = HandleYaml() # 从yaml文件中取出该接口的参数
params = get_data.get_data()['5.2.1.4']
@ddt.ddt
classTest(unittest.TestCase):
"""加载咨询详情接口"""
defsetUp(self):
self.url ='http://localhost:8088/ApprExclusiveInterface/api/enterprise/info/consult/loadDetail.v'
self.run =RunMethod()
@ddt.data(*params)
deftest(self, value):
r =self.run.run_main("GET", self.url, value)
print(r)
self.assertTrue(value['assert'] inr.text)
if __name__ == '__main__':
unittest.main()
(4)利用HTMLTestRunner生成测试报告
run_report.py
# coding: utf-8
# author: hmk
from HTMLTestRunner importHTMLTestRunner
importunittest
importtime, os
root_dir = os.path.dirname(os.path.abspath('.')) # 获取当前文件所在目录的父目录的绝对路径,也就是项目所在路径E:\DDT_Interface
case_dir = root_dir + '/test_case/' # 根据项目所在路径,找到用例所在的相对项目的路径
print(root_dir)
print(case_dir)
"""定义discover方法"""
discover = unittest.defaultTestLoader.discover(case_dir,
pattern='test*.py', top_level_dir=None)
"""
1.case_dir即测试用例所在目录
2.pattern='test_*.py' :表示用例文件名的匹配原则,“*”表示任意多个字符
3.top_level_dir=None:测试模块的顶层目录。如果没顶层目录(也就是说测试用例不是放在多级目录中),默认为None
"""
if __name__ == "__main__":
"""直接加载discover"""
now = time.strftime("%Y-%m-%d%H_%M_%S")
filename = root_dir +'/report/' + now + '_result.html' #定义报告存放路径
print(filename)
fp = open(filename,'wb')
runner =HTMLTestRunner(stream=fp, title='个人网企业网接口测试报告', description='测试结果如下: ')
runner.run(discover)
fp.close()
ok ,unittest+ddt进行接口测试就完成了,还有很多不足,yaml配置文件还可以继续设计优化,例如可以把请求url也加进去。
其实感觉如何设计测试用例,组织测试数据也是一件很有意思的事情,很多事情都必须先有一个良好的设计思路才会进行的更顺畅。总之勤于思考,多参考他人的思路。不是有句话吗,学而不思则罔,思而不学则殆。