123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- function result = RunTest(case_name)
- %RUNTEST 运行测试用例
- %% 初始化/加载
- evalin('base', 'clear all');
- caseFile = [case_name '_Case.json'];
- test_case = jsondecode(fileread(caseFile)); %读取测试用例文件
- target_model = test_case.Model.name;
- connectionFile = test_case.Model.connection_file;
- % 初始化模型
- load_system(target_model);
- scopes = find_system(target_model, 'FindAll', 'on', 'BlockType', 'Scope');
- CommentBlocks(scopes, 1);
- evalin('base', test_case.Model.init);
- % 获取模型测试接口
- ports = LoadModelPorts(target_model);
- % 初始化注入信号
- % 默认物理信号为0,使能直通
- for i = 1:length(ports.Physical)
- SetPortSignal(ports.Physical(i), 0);
- SetPortThrough(ports.Physical(i), 1);
- SetPortEnable(ports.Physical(i), 1);
- end
- % 默认软件信号为0,关闭直通
- for i = 1:length(ports.Software)
- SetPortSignal(ports.Software(i), 0);
- SetPortThrough(ports.Software(i), 0);
- SetPortEnable(ports.Software(i), 0);
- end
- % 加载注入信号
- test_connection = jsondecode(fileread(connectionFile)); %读取连接文件
- % 应用模型注入信号
- for i = 1:length(test_case.Injections)
- ApplyInjection(test_case.Injections(i), test_connection, ports);
- end
- % 应用模型初始化信号
- for i = 1:length(test_case.Initializations)
- ApplyInit(test_case.Initializations(i), test_connection)
- end
- %% 运行模型
- if isfield(test_case.Model, 'config')
- for i = 1:length(test_case.Model.config)
- set_param(target_model, test_case.Model.config(i).name, test_case.Model.config(i).value);
- end
- end
- out = sim(target_model);
- assignin('base', 'out', out);
- % s = load('matlab.mat');
- % assignin('base', 'out', s.out);
- close_system(target_model, 0); %仿真完成后必须关闭模型,否则在Accelerator模式下再次仿真将导致Matlab崩溃,原因暂时不明
- %% 获取模型输出
- %% 验证测试结果
- test_check = test_case.Checks;
- check_result = {};
- for i = 1:length(test_check)
- check = ParseCheck(test_check(i), test_case, test_connection);
- [res, msg] = ConditionCheck(check);
- check_result{i} = {check.id, check.tag, res, msg};
- end
- result = check_result;
- end
|