Back to TTCN-3 Refactoring Catalog

Parameterize Module

Parameterize modules to specify environment specific parameters at tool level.

Motivation

TTCN-3 test cases are executed in an automated way and there is no possibility to interact with the user through a GUI at TTCN-3 level for example. Therefore, information that is specific to a local test environment (such as IP addresses of SUTs) may sometimes be found in constants. However, through module parameters, this task can be deferred to the TTCN-3 tooling. As a result, TTCN-3 code does not have to be changed when used in different test environments in the ideal case. Hence, reusability is improved and maintenance work reduced.

Mechanics

  • Find constants and magic numbers that are specific to the test environment in the source module. For magic numbers, apply the Replace Magic Number with Symbolic Constant refactoring. The resulting constant declarations are the source declarations.
  • Move all source constant declarations into the modulepar section. If there is no modulepar section yet, it must be created. Remove the const keyword from each declaration.
  • Specify custom environment-specific actual parameters for the module parameters at tool level.
  • Compile and validate using the original magic numbers from the source module as module parameters at tool level.

Example

The first listing shows the unrefactored version. There are two templates where custom IP addresses are specified. Apparently, this is not optimal as the specified IP addresses would have to be modified for each test environment.

type record ExampleType {
	boolean ipv6,
	charstring ipAddress
}

template ExampleType a_firstRemoteAddressTemplate := { 
	ipv6		:= false,
	ipAddress	:= "64.233.187.99" 
} 

template ExampleType a_secondRemoteAddressTemplate := { 
	ipv6		:= false,
	ipAddress	:= "134.72.13.2" 
} 
testcase tc_exampleTestCase() runs on ExampleComponent {
	pt.send( a_firstRemoteAddressTemplate );
	pt.send( a_secondRemoteAddressTemplate );
}

After application of the Replace Magic Number with Symbolic Constant refactoring, two constants are introduced for the local and remote IP address. The templates a_localAddressTemplate and a_remoteAddressTemplate now reference these constants.

const charstring c_firstRemoteIPAddress := "64.233.187.99"; 
const charstring c_secondRemoteIPAddress := "134.72.13.2"; 

template ExampleType a_firstRemoteAddressTemplate := {
	ipv6		:= false,
	ipAddress	:= c_firstRemoteIPAddress 
}
template ExampleType a_secondRemoteAddressTemplate := {
	ipv6		:= false,
	ipAddress	:= c_secondRemoteIPAddress 
}

In the next and final step, the constants are converted to module parameters by putting them into the modulepar section and removing the const keyword from the declaration. Now, they can be overwritten externally by TTCN-3 test execution management tools.

modulepar { 
	charstring mp_firstRemoteIPAddress := "64.233.187.99";
	charstring mp_secondRemoteIPAddress := "134.72.13.2";
} 

template ExampleType a_firstRemoteAddressTemplate := {
	ipv6		:= false,
	ipAddress	:= mp_firstRemoteIPAddress
}
template ExampleType a_secondRemoteAddressTemplate := {
	ipv6		:= false,
	ipAddress	:= mp_secondRemoteIPAddress
}

Back to TTCN-3 Refactoring Catalog