内容参考1: 从零开始学习CANoe(五)—— CAPL 测试节点_蚂蚁小兵-CSDN博客
内容参考2:《CANoe开发从入门到精通》
在工程建立后(基于第三个仿真工程),有下面两种方式创建CAPL Test Module。
目录
1. 在Test SetUp创建CAPL Test Module
2 基于Simulation Setup创建CAPL Test Module
3. 附录:CAPL代码
1-1 点击Test-TestSetUp,打开Test Setup for Test Modules窗口。
1-2 在窗口右键,创建或使用已经建立好的Test Environment
1-3 选中创建的Test Environment 测试环境,创建CAPL TestModule。
1-4 选中创建的Test Module 右键编辑,进入.can文件,编写CAPL测试用例。
1-5 CAPL编辑完成后,双击Test Module名,可以在窗口看到CAPL中定义的测试用例。启动工程后,点击右下角三角形可以运行测试用例。
1-5 TestModule运行结束后,可以查看测试报告。测试报告的格式可以选择Test Viewer,或者XML/HTML格式打开。
至此,使用TestSetup创建CAPL Test Module完成。
2-1 直接在仿真工程面板Simulation上,在总线上右键,添加CAPL Test Module
2-2 创建后 会显示一个CAPL节点,右键点击Configuration可配置此节点属性,
2-3 配置完成后可点击小铅笔,编辑CAPL文件编写测试用例,并保存编译通过。
2-4 工程运行后,点击CAPL节点,可打开CAPL TestModule窗口,显示编辑好的CAPL测试用例。点击三角形,可运行TestModule。
2-5 TestModule运行结束后,可以查看测试报告。
至此,使用Simulation Setup创建CAPL Test Module完成。
/*@!Encoding:936*/
/*
1.测试用例——检测报文周期
2.测试用例——检测报文长度DLC
3.测试用例——检测未定义报文undefined msg
4.测试用例——功能测试
5.测试用例——测试模块入口函数
*/
includes
{
}
variables
{
//TC1
dword gCycCheckId;//声明检测事件的ID
int gUndefinedMsgCheckResult;//声明未定义报文的检测结果
const long kMIN_CYCLE_TIME = 40;//一般最小周期时间常量
const long kMAX_CYCLE_TIME = 60;//一般最大周期时间常量
const long Lingh_MIN_CYCLE_TIME = 490;//定义报文Light_Info最小周期时间常量
const long Lingh_MAX_CYCLE_TIME = 510;//定义报文Light_Info最大周期时间常量
const long kTIMEOUT = 4000;//定义测试等待时间常量
}
//周期时间检测结果函数
CheckMsgCyc(float aCycMinCycleTime, float aCycMaxCycleTime)
{
long lQueryResultProbeAvg;//声明平均时间
long lQueryResultProbeMin;//声明最小测量时间
long lQueryResultProbeMax;//声明最大测量时间
char lbuffer[100];
testAddCondition(gCycCheckId);//在该函数中添加事件
testWaitForTimeout(kTIMEOUT);//等待测试时间结束
//统计平均时间
lQueryResultProbeAvg = ChkQuery_StatProbeIntervalAvg(gCycCheckId);
//统计min时间
lQueryResultProbeMin = ChkQuery_StatProbeIntervalMin(gCycCheckId);
//统计max时间
lQueryResultProbeMax = ChkQuery_StatProbeIntervalMax(gCycCheckId);
if(ChkQuery_NumEvents(gCycCheckId)>0)
{
//统计异常次数//打印报告
snprintf(lbuffer,elCount(lbuffer),"Valid values %.0fms - %.0fms",aCycMinCycleTime,aCycMaxCycleTime);
testStepFail("",lbuffer);
snprintf(lbuffer,elCount(lbuffer),"Average cycle time: %dms",lQueryResultProbeAvg);
testStepFail("",lbuffer);
snprintf(lbuffer,elCount(lbuffer),"Min cycle time: %dms",lQueryResultProbeMin);
testStepFail("",lbuffer);
snprintf(lbuffer,elCount(lbuffer),"Average cycle time: %dms",lQueryResultProbeMax);
testStepFail("",lbuffer);
}
else
{
snprintf(lbuffer,elCount(lbuffer),"Valid values %.0fms - %.0fms",aCycMinCycleTime,aCycMaxCycleTime);
testStepPass("",lbuffer);
snprintf(lbuffer,elCount(lbuffer),"Average cycle time: %dms",lQueryResultProbeAvg);
testStepPass("",lbuffer);
snprintf(lbuffer,elCount(lbuffer),"Min cycle time: %dms",lQueryResultProbeMin);
testStepPass("",lbuffer);
snprintf(lbuffer,elCount(lbuffer),"Average cycle time: %dms",lQueryResultProbeMax);
testStepPass("",lbuffer);
}
ChkControl_Destroy(gCycCheckId);//销毁事件
}
//TC1:Check Cycle time of msg EngineData
testcase CheckMsgEngineData()
{
float lCycMinCycleTime;//声明最小周期时间
float lCycMaxCycleTime;//声明最大周期时间
lCycMinCycleTime = kMIN_CYCLE_TIME;//赋值
lCycMaxCycleTime = kMAX_CYCLE_TIME;
//测试报告提示信息
testCaseTitle("TC-1","TC-1:Check cycle time of msg EngineData");
//开始观察待测报文
gCycCheckId = ChkStart_MsgAbsCycleTimeViolation(EngineData,lCycMinCycleTime,lCycMaxCycleTime);
CheckMsgCyc(lCycMinCycleTime,lCycMaxCycleTime);//周期时间检测结果函数
testRemoveCondition(gCycCheckId);//移除测试条件
}
//TC-2:Check Cycle time of msg VehicleData
testcase CheckMsgVehicleData()
{
float lCycMinCycleTime;
float lCycMaxCycleTime;
lCycMinCycleTime = kMIN_CYCLE_TIME;
lCycMaxCycleTime = kMAX_CYCLE_TIME;
testCaseTitle("TC-2","TC-2:Check cycle time of msg VehicleData");
gCycCheckId = ChkStart_MsgAbsCycleTimeViolation(VehicleData,lCycMinCycleTime,lCycMaxCycleTime);
CheckMsgCyc(lCycMinCycleTime,lCycMaxCycleTime);
testRemoveCondition(gCycCheckId);
}
//TC-3:Check Cycle time of msg Gear_Info
testcase CheckMsgGear_Info()
{
float lCycMinCycleTime;
float lCycMaxCycleTime;
lCycMinCycleTime = kMIN_CYCLE_TIME;
lCycMaxCycleTime = kMAX_CYCLE_TIME;
testCaseTitle("TC-3","TC-3:Check cycle time of msg Gear_Info");
gCycCheckId = ChkStart_MsgAbsCycleTimeViolation(Gear_Info,lCycMinCycleTime,lCycMaxCycleTime);
CheckMsgCyc(lCycMinCycleTime,lCycMaxCycleTime);
testRemoveCondition(gCycCheckId);
}
//TC-4:Check Cycle time of msg Ignition_Info
testcase CheckMsgIgnition_Info()
{
float lCycMinCycleTime;
float lCycMaxCycleTime;
lCycMinCycleTime = kMIN_CYCLE_TIME;
lCycMaxCycleTime = kMAX_CYCLE_TIME;
testCaseTitle("TC-4","TC-4:Check cycle time of msg Ignition_Info");
gCycCheckId = ChkStart_MsgAbsCycleTimeViolation(Ignition_Info,lCycMinCycleTime,lCycMaxCycleTime);
CheckMsgCyc(lCycMinCycleTime,lCycMaxCycleTime);
testRemoveCondition(gCycCheckId);
}
//TC-5:Check Cycle time of msg Light_Inf
testcase CheckMsgLight_Info()
{
float lCycMinCycleTime;
float lCycMaxCycleTime;
lCycMinCycleTime = kMIN_CYCLE_TIME;
lCycMaxCycleTime = kMAX_CYCLE_TIME;
testCaseTitle("TC-5","TC-5:Check cycle time of msg Light_Info");
gCycCheckId = ChkStart_MsgAbsCycleTimeViolation(Light_Info,lCycMinCycleTime,lCycMaxCycleTime);
CheckMsgCyc(lCycMinCycleTime,lCycMaxCycleTime);
testRemoveCondition(gCycCheckId);
}
//TC6:DLC 报文长度测试
testcase CheckDLCLock_Info()
{
dword checkId;
//测试报告提示信息
testCaseTitle("TC-6","TC-6:Check msg DLC of Lock_Info");
//管事观测报文Lock_Info的DLC
checkId = ChkStart_InconsistentDlc(Lock_Info);
testAddCondition(checkId);
//等待测试时间结束
testWaitForTimeout(kTIMEOUT);
testRemoveCondition(checkId);
}
//TC-7:检测未定义信号
testcase CheckUndefinedMessage()
{
long lEventUndefineMessageId;//声明未定义报文Id
char lbuffer[100];
gUndefinedMsgCheckResult = 0;//?初始化未定义报文数量为0
testCaseTitle("TC-7","TC-7:Check CAN channel for undefined message");
//开始观测当前总线
gCycCheckId = ChkStart_UndefinedMessageReceived("UndefinedMsgCallback");
//延时,即测量该时间段
testWaitForTimeout(kTIMEOUT);
switch(gUndefinedMsgCheckResult)
{
case 1:
write("Iam case1");
//获取未定义报文ID
lEventUndefineMessageId = ChkQuery_EventMessageId(gCycCheckId);
snprintf(lbuffer,elCount(lbuffer),"Undefined message detected: Id 0x%x",lEventUndefineMessageId);
testStepFail("",lbuffer);
break;
default:
write("Iamdefault");
testStepPass("","No undefined message detected!");
break;
}
ChkControl_Destroy(gCycCheckId);//销毁事件
}
UndefinedMsgCallback(dword aCheckId)
{
//回调函数,检测到未定义报文时调用
write("Iam here");
ChkQuery_EventStatusToWrite(aCheckId);
gUndefinedMsgCheckResult=1;//将未定义报文个数置为1
}
//TC-8:功能测试
testcase CheckEngine_Speed()
{
dword checkId;
testCaseTitle("TC-8","TC-8:Check Engine Speed Value");
@Vehicle_Key::Unlock_Car = 1;
@Vehicle_Key::Car_Driver = 0;
@Vehicle_Key::Key_State = 2;
@Vehicle_Control::Eng_Speed = 2000;
//开始观测,信号值是否在范围内
checkId = ChkStart_MsgSignalValueInvalid(EngineData::EngSpeed,1900,2100);
testWaitForTimeout(kTIMEOUT);
if(ChkQuery_EventSignalValue(checkId))
{
testStepPass("","Correct Engine Speed Value");
}
else
{
testStepFail("","Incorrect Engine Speed Value");
}
}
//测试模块入口函数
void MainTest()
{
testModuleTitle("NetworkTester");
testModuleDescription("Message Specification Test and Function Test Demo.");
testGroupBegin("Check msg cycle time","Check the differ mesage cycle time");
Init_Test_Condition();
CheckMsgEngineData();
CheckMsgVehicleData();
CheckMsgGear_Info();
CheckMsgIgnition_Info();
CheckMsgLight_Info();
testGroupEnd();
testGroupBegin("Check msg DLC","Check DLC of a message");
CheckDLCLock_Info();
testGroupEnd();
testGroupBegin("Check undefined msg","Check the undefined message");
CheckUndefinedMessage();
testGroupEnd();
testGroupBegin("Fucntion Test","Check the engine speed after setup");
CheckEngine_Speed();
testGroupEnd();
}
//初始化仿真工程状态,确保各个模块处于Online
Init_Test_Condition()
{
@Vehicle_Key::Unlock_Car = 1;
@Vehicle_Key::Car_Driver = 0;
@Vehicle_Key::Key_State = 2;
testWaitForTimeout(500);
}
on sysvar_update Test::IPC_Cluster_Info_off
{
if (@this==1)
{
}
}
END