RunTest.m 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. function result = RunTest(case_name)
  2. %RUNTEST 运行测试用例
  3. %% 初始化/加载
  4. evalin('base', 'clear all');
  5. caseFile = [case_name '_Case.json'];
  6. test_case = jsondecode(fileread(caseFile)); %读取测试用例文件
  7. target_model = test_case.Model.name;
  8. connectionFile = test_case.Model.connection_file;
  9. % 初始化模型
  10. load_system(target_model);
  11. scopes = find_system(target_model, 'FindAll', 'on', 'BlockType', 'Scope');
  12. CommentBlocks(scopes, 1);
  13. evalin('base', test_case.Model.init);
  14. % 获取模型测试接口
  15. ports = LoadModelPorts(target_model);
  16. % 初始化注入信号
  17. % 默认物理信号为0,使能直通
  18. for i = 1:length(ports.Physical)
  19. SetPortSignal(ports.Physical(i), 0);
  20. SetPortThrough(ports.Physical(i), 1);
  21. SetPortEnable(ports.Physical(i), 1);
  22. end
  23. % 默认软件信号为0,关闭直通
  24. for i = 1:length(ports.Software)
  25. SetPortSignal(ports.Software(i), 0);
  26. SetPortThrough(ports.Software(i), 0);
  27. SetPortEnable(ports.Software(i), 0);
  28. end
  29. % 加载注入信号
  30. test_connection = jsondecode(fileread(connectionFile)); %读取连接文件
  31. % 应用模型注入信号
  32. for i = 1:length(test_case.Injections)
  33. ApplyInjection(test_case.Injections(i), test_connection, ports);
  34. end
  35. % 应用模型初始化信号
  36. for i = 1:length(test_case.Initializations)
  37. ApplyInit(test_case.Initializations(i), test_connection)
  38. end
  39. %% 运行模型
  40. if isfield(test_case.Model, 'config')
  41. for i = 1:length(test_case.Model.config)
  42. set_param(target_model, test_case.Model.config(i).name, test_case.Model.config(i).value);
  43. end
  44. end
  45. out = sim(target_model);
  46. assignin('base', 'out', out);
  47. % s = load('matlab.mat');
  48. % assignin('base', 'out', s.out);
  49. close_system(target_model, 0); %仿真完成后必须关闭模型,否则在Accelerator模式下再次仿真将导致Matlab崩溃,原因暂时不明
  50. %% 获取模型输出
  51. %% 验证测试结果
  52. test_check = test_case.Checks;
  53. check_result = {};
  54. for i = 1:length(test_check)
  55. check = ParseCheck(test_check(i), test_case, test_connection);
  56. [res, msg] = ConditionCheck(check);
  57. check_result{i} = {check.id, check.tag, res, msg};
  58. end
  59. result = check_result;
  60. end