CAPL脚本中 getSignal和 setSignal 函数的封装,看完本节足够适应所有测试场景

Posted 蚂蚁小兵

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CAPL脚本中 getSignal和 setSignal 函数的封装,看完本节足够适应所有测试场景相关的知识,希望对你有一定的参考价值。

系列用的CANoe演示工程我放在了Git上,不定时根据博客更新。

CANoe-Demn


前言

  • 测试软硬件环境:
    CANoe 11 SP2
    Win10 X64

  • 下图是读取信号值的两种方式,一种参数时signal类型。另一种时字符串类型,一般情况我们使用第一种方式(建立在DBC文件中定义此信号,否则就报错),但是在一些项目移植中,无法做到兼容性,所以有时候期望代码更具平台性,我们会选择第二种方式

  • 基于信号操作的API再测试中使用频率比较高,下面我就进行了一系列的封装,可以在实际测试中,提高复用性


文章目录


代码配置

  • 习惯于建XML Test Node ,如下图用于测试代码。


函数(一):读取信号(参数类型是signal )(物理值和原始值)

  • float TestStep_GetSignal (char Tstep[], signal *aSignal,byte physRaw) 函数原型

① xml 代码如下,新建了两个CASE,:

  • 读取信号的物理值
  • 读取信号的原始值
<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<testmodule title="bmw" version="">
		<testgroup title="signal functions test ">		
				<capltestcase name="SignalFunctionTest" title = "get_signal(signal type)">
					<caplparam type = "string" name = "TestType">get_signal</caplparam>
				</capltestcase>
				<capltestcase name="SignalFunctionTest" title = "get_signal(signal type raw)">
					<caplparam type = "string" name = "TestType">get_signal_raw</caplparam>
				</capltestcase>
			
		</testgroup>				
</testmodule>


② CAPL代码如下,一个CASE SignalFunctionTest,根据传参的字符串,选择运行的函数


/*@!Encoding:1252*/
variables

    // addressing type
    const byte gcPhys = 0;
    const byte gcRaw  = 1;


testcase SignalFunctionTest(char TestType[])
 
  int TStep_i ;
  char Tstep[10];
   
	TStep_i  = 1;
	snprintf(Tstep, elcount(Tstep), "%d", TStep_i);
   
  if (strncmp(TestType,"get_signal",elCount(TestType)) == 0)
  
    TestStep_GetSignal(Tstep,EngineSpeed_Float,gcPhys);
  
  else if (strncmp(TestType,"get_signal_raw",elCount(TestType)) == 0)
  
    TestStep_GetSignal(Tstep,EngineSpeed_Float,gcRaw);
  




float TestStep_GetSignal (char Tstep[],  signal *aSignal,byte physRaw)

    byte subStepId;
    char tempText[255];
    char signalNameStr[100];
    float retVal;     
  
    snprintf(signalNameStr,elcount(signalNameStr),"%s", aSignal.name);       
    snprintf(tempText,elcount(tempText),"Get value for signal %s.", signalNameStr);

    subStepId = 0;
    snprintf(Tstep,elcount(Tstep),"%s.%d",Tstep,subStepId);
    TestStep(Tstep, tempText);
    
    retVal = -1;//Initialize variable for pass/fail verdict
    if(physRaw == gcPhys)
    
      retVal = getSignal(aSignal);
    
 
    else
    
      retVal =  GetRawSignal(aSignal);
            
    
    if(retVal != -1)
    
        snprintf(tempText,elcount(tempText),"Signal value was getted. %s = %10.5f.", signalNameStr, retVal);
        testStepPass(Tstep, tempText); 
    
    else
    
       snprintf(tempText,elcount(tempText),"Value for signal %s was not getted.", signalNameStr);
       testStepFail(Tstep, tempText);
    
   
   return retVal;



③ 测试结果如下图



函数(二):读取信号而且比较(参数类型是signal )(物理值和原始值)

  • 函数原型: byte TestStep_GetAndCompareSignal(char Tstep[], signal *aSignal, double compareValue, long timeOut, byte physRaw , byte compareMode)

① xml 代码如下,新建了两个CASE,:

  • 读取信号而且比较物理值
  • 读取信号而且比较原始值
<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<testmodule title="bmw" version="">
		<testgroup title="signal functions test ">		
				<capltestcase name="SignalFunctionTest" title = "get_signal(signal type)">
					<caplparam type = "string" name = "TestType">get_signal</caplparam>
				</capltestcase>
				<capltestcase name="SignalFunctionTest" title = "get_signal(signal type raw value)">
					<caplparam type = "string" name = "TestType">get_signal_raw</caplparam>
				</capltestcase>
				<capltestcase name="SignalFunctionTest" title = "get_signal_and_compare(signal type)">
					<caplparam type = "string" name = "TestType">get_signal_and_compare</caplparam>
				</capltestcase>
				<capltestcase name="SignalFunctionTest" title = "get_signal_and_compare(signal type raw value)">
					<caplparam type = "string" name = "TestType">get_signal_and_compare_raw</caplparam>
				</capltestcase>			
		</testgroup>				
</testmodule>

② CAPL代码如下,因为代码太占篇幅,就不把上面已经存在的代码贴过来了



/*@!Encoding:1252*/
variables

    // addressing type
    const byte gcPhys = 0;
    const byte gcRaw  = 1;
  
    const byte gcEqual              = 0;
    const byte gcNotEqual           = 1;
    const byte gcGreaterThen        = 2;
    const byte gcLessThen           = 3;
    const byte gcEqualTo            = 4;   
    const byte gcGreaterEqual       = 5;
    const byte gcLessEqual          = 6;
    const byte gcTrue               = 1;
    const byte gcFalse              = 0;
    const byte gcOk                 = 1;
    const byte gcNok                = 0;


testcase SignalFunctionTest(char TestType[])
 
  int TStep_i ;
  char Tstep[10];
  long wait_time = 100;//unit :ms
   
	TStep_i  = 1;
	snprintf(Tstep, elcount(Tstep), "%d", TStep_i);
   
  if (strncmp(TestType,"get_signal",elCount(TestType)) == 0)
  
    TestStep_GetSignal(Tstep,EngineSpeed_Float,gcPhys);
  
  else if (strncmp(TestType,"get_signal_raw",elCount(TestType)) == 0)
  
    TestStep_GetSignal(Tstep,EngineSpeed_Float,gcRaw);
  
    else if (strncmp(TestType,"get_signal_and_compare",elCount(TestType)) == 0)
  
    TestStep_GetAndCompareSignal(Tstep,EngineSpeed_Float,100.0,wait_time,gcRaw,gcEqual);
  
    else if (strncmp(TestType,"get_signal_and_compare_raw",elCount(TestType)) == 0)
  
    TestStep_GetAndCompareSignal(Tstep,EngineSpeed_Float,1600,wait_time,gcRaw,gcEqual);
  


byte TestStep_GetAndCompareSignal(char Tstep[],  signal *aSignal, double compareValue, long timeOut, byte physRaw , byte compareMode)

    byte subStepId;
 
    char signalName[200];
    char tempInfo[255]; 
    char tempText[255];
    double signalVal;
    byte conditionFulfilled;
    dword startTime;
    dword time2wait = 3;

    
    snprintf(signalName,elcount(signalName),"%s", aSignal.name);     
  
    
    switch(compareMode)
    
        case(gcEqual) :
            snprintf(tempText,elcount(tempText),"%s equal to %g", signalName, compareValue);
            break;
        case(gcNotEqual) :
            snprintf(tempText,elcount(tempText),"%s not equal to %g", signalName, compareValue);
            break;
        case(gcGreaterThen) :
            snprintf(tempText,elcount(tempText),"%s greater then %g", signalName, compareValue);
            break;
        case(gcLessThen) :
            snprintf(tempText,elcount(tempText),"%s less then %g", signalName, compareValue);
            break;
        case(gcGreaterEqual) :
            snprintf(tempText,elcount(tempText),"%s greater or equal to %g", signalName, compareValue);
            break;
        case(gcLessEqual) :
            snprintf(tempText,elcount(tempText),"%s less or equal to %g", signalName, compareValue);
            break;
        default :
            snprintf(tempText,elcount(tempText),"ERROR TestStep_GetAndCompareSignal: Invalid compare mode: %d", compareValue);
    
    
    subStepId = 0;
    snprintf(Tstep,elcount(Tstep),"%s.%d",Tstep,subStepId);
    TestStep(Tstep, tempText);
    

    conditionFulfilled = gcFalse;
    startTime = timeNow();   //current time
    
    while((timeNow() - startTime) < timeOut * 100.0 && conditionFulfilled == gcFalse || timeOut == 0)
    
      
      if(physRaw == gcPhys)
      
        signalVal = getSignal(aSignal);
      
   
      else
      
        signalVal =  GetRawSignal(aSignal);
      

        switch(compareMode)
        
            case(gcEqual) :
                if(signalVal == compareValue)
                
					          conditionFulfilled = gcTrue;
				        
                break;
            case(gcNotEqual) :
                if(signalVal != compareValue)
                
					          conditionFulfilled = gcTrue;
					          snprintf(tempInfo, elcount(tempInfo), "different than");
				        
                break;
            case(gcGreaterThen) :
                if(signalVal > compareValue)
                
					          conditionFulfilled = gcTrue;
					          snprintf(tempInfo, elcount(tempInfo), "greater than");
				        
                break;
            case(gcLessThen) :
                if(signalVal < compareValue)
                
					          conditionFulfilled = gcTrue;
					          snprintf(tempInfo, elcount(tempInfo), "less than");
				        
                break;
            case(gcGreaterEqual) :
                if(signalVal >= compareValue)
				        
                    conditionFulfilled = gcTrue;
					          snprintf(tempInfo, elcount(tempInfo), "greather than or equal to");
				        
                break;
            case(gcLessEqual) :
                if(signalVal <= compareValue)
				        
                    conditionFulfilled = gcTrue;
					          snprintf(tempInfo, elcount(tempInfo), "less than or equal to");
				        
                break;
            default :
                conditionFulfilled = gcFalse;
        
        
        if(timeOut != 0)
        
            testWaitForTimeout(time2wait);
        
        else
        
            break;
        
    
    
    //pass or fail
    if(conditionFulfilled == gcTrue)
    
		  snprintf(tempText,elcount(tempText),"%s = %g.", signalName, signalVal);
      testStepPass(Tstep, tempText); 
      return gcOk;
    
    else
    
		if(timeOut == 0)
			snprintf(tempText,elcount(tempText),"%s = %g. Expected to be %s %g.", signalName, signalVal, tempInfo, compareValue);
		else
			snprintf(tempText,elcount(tempText),"%s = %g. Expected to be %s %g. Verification time: %5.1f s.", signalName, signalVal, tempInfo, compareValue, (double)(timeNow() - startTime)/100.0);
		
    testStepFail(Tstep, tempText); 
    return gcNok;
    




③ 测试结果如下图



函数(三):设置信号(参数类型是signal )(物理值和原始值)

  • 函数原型: TestStep_SetSignal (char Tstep[], signal *aSignal, double value2Set,long timeOut, byte physRaw)

① xml 代码如下,新建了两个CASE,:

  • 读取信号而且比较物理值
  • 读取信号而且比较原始值
<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<testmodule title="bmw" version="">
		<testgroup title="signal functions test ">		
				<capltestcase name="SignalFunctionTest" title = "get_signal(signal type)">
					<caplparam type = "string" name = "TestType">get_signal</caplparam>
				</capltestcase>
				<capltestcase name="SignalFunctionTest" title = "get_signal(signal type raw value)">
					<caplparam type = "string" name = "TestType">get_signal_raw</caplparam>
				</capltestcase>
				以上是关于CAPL脚本中 getSignal和 setSignal 函数的封装,看完本节足够适应所有测试场景的主要内容,如果未能解决你的问题,请参考以下文章

CANoe CAPL文件操作目录合集

CAPL 脚本中对信号,系统变量,环境变量的 事件响应

基于CAPL脚本,造一个分割字符串的轮子

CAPL 脚本对.ini 配置文件的高阶操作

CAPL脚本,数组的一些查找操作,包括查找某一个值和某一些值

CAPL脚本要注意区分elcount和strlen求数组长度的区别,不然要吃大亏