User-defined test management adapterΒΆ
A user-defined test management adapter (TmUserAdapter) is an interface defined in ecu.test. This interface offers the possibility to implement your own adapters. In this way, the interaction between the test management system and ecu.test can be optimally mapped for your own use case. The specific description of the interface is explained in the api documentation api/userTestmanagement/tmuser-api.html.
To use a TmUserAdapter, this adapter must be installed in the directory for User Python libraries. The TmUserAdapter can then be configured and used in ecu.test. A manual for this can be found under Configure test management.
An example implementation for a TmUserAdapter in TmUserAdapterExample.py contains the
following code:
from __future__ import annotations
"""
This module contains an example of a user-defined test management adapter. The source code of this
example can be found in the ecu.test installation directory under
``Templates/DefaultData/UserPyModules/UserTestmanagement``.
A user-defined test management adapter must be placed in the UserPyModules directory of the current
workspace. If the implementation of the active adapter is changed, it is necessary to reload the
UserPyModules via select *Extras -> Update user libraries* in ecu.test.
Hint: In case of failures prefer to raise errors instead of printing error messages.
"""
from tts.core.api.objectApi.ObjectApiMainInstance import ObjectApiMainInstance
from .api.data.TmObjectInfo import TmObjectInfo
# Required so that ecu.test can discover this module and recognize it as a user test managment
# adapter.
MODULE_TYPE = 'USER_TMS'
class TmUserAdapterExample:
__PROPERTY_AS_STR = 'Option 1'
__PROPERTY_AS_BOOL = 'Option 2'
__PROPERTY_AS_INT = 'Option 3'
__PROPERTY_AS_FLOAT = 'Option 4'
def __init__(self, properties: dict[str, int | str | float | bool | None]) -> None:
"""
This method is called on adapter creation. It can be used to start any external dependencies
needed by this adapter.
The properties contain the configured properties for the concrete adapter as a dictionary.
Key: The name of the property.
Value: The value for the property.
"""
self.__properties = properties
self.__objectApi = ObjectApiMainInstance().GetObjectApi()
self.__tmsClient = TmsRestClient()
def __del__(self) -> None:
"""
This method is called when the instance is about to be destroyed. It can be used to dispose
external dependencies.
"""
if self.IsLoggedIn():
self.Logout()
@classmethod
def GetName(cls) -> str:
"""
The name of the user test management adapter.
This name is displayed in the test management configuration.
"""
return 'Test management user adapter example'
@classmethod
def GetAvailableProperties(cls) -> dict[str, dict]:
"""
Defines the properties available for the user test management adapter. These properties can
be configured in the test management configuration. The configured properties are passed to
the `__init__` method when this adapter is created.
Each property is identified by its name and described within its own dict containing
information on 'Description', 'Type' and 'Default'. Available types are str, bool, int and
float.
"""
props = {
TmUserAdapterExample.__PROPERTY_AS_STR: {
'Description': 'This is Option 1 with a string.',
'Type': str,
'Default': 'default value',
},
TmUserAdapterExample.__PROPERTY_AS_BOOL: {
'Description': 'This is Option 2 with a bool.',
'Type': bool,
'Default': False,
},
TmUserAdapterExample.__PROPERTY_AS_INT: {
'Description': 'This is Option 3 with a int.',
'Type': int,
'Default': 42,
},
TmUserAdapterExample.__PROPERTY_AS_FLOAT: {
'Description': 'This is Option 4 with a float.',
'Type': float,
'Default': 9.99999,
},
}
return props
def Login(self) -> bool:
"""
Log in to the test management system.
"""
self.__tmsClient.Login()
return True
def Logout(self) -> bool:
"""
Log out of the test management system.
"""
self.__tmsClient.Logout()
return True
def IsLoggedIn(self) -> bool:
"""
Returns if there is currently an active user logged in. Depending on that information,
actions will be activated in ecu.test.
"""
return self.__tmsClient.IsLoggedIn()
def ExportProject(self, filePaths: list[str]) -> None:
"""
Exports PRJ files to the test management system.
Convert here the ecu.test project to the specific tms object.
"""
for filePath in filePaths:
project = self.__objectApi.ProjectApi.OpenProject(filePath)
tmsObject = self.__ConvertToTmsObject(project)
newTmsId = self.__tmsClient.CreateItemAtTms(tmsObject)
project.SetTestSuiteId(newTmsId)
def ImportProject(self, targetDirectory: str, tmsIds: list[str]) -> None:
"""
Imports projects from test management system and saves it as PRJ files.
This method needs to contain:
- the selection of the project within the test management system
- the generation of the PRJ file using the ObjectApi.ProjectApi
The passed tms Ids are defined using the GUI in ecu.test. You can choose between a selection
and input mode. The selection mode uses GetProjectsFromTms (if implemented) as source.
"""
for tmsId in tmsIds:
tmsObject = self.__tmsClient.GetItemFromTms(tmsId)
project = self.__objectApi.ProjectApi.CreateProject()
project.SetTestSuiteId(tmsId)
self.__ConvertToEcuTest(project, tmsObject)
project.Save(f'{targetDirectory}/myFancyProjectName.prj')
def ExportPackage(self, filePaths: list[str]) -> None:
"""
Exports PKG files to the test management system. Convert here the ecu.test packages to the
specific object.
"""
for filePath in filePaths:
package = self.__objectApi.PackageApi.OpenPackage(filePath)
tmsObject = self.__ConvertToTmsObject(package)
newTmsId = self.__tmsClient.CreateItemAtTms(tmsObject)
package.SetTestScriptId(newTmsId)
def ImportPackage(self, targetDirectory: str, tmsIds: list[str]) -> None:
"""
Imports packages from the test management system and saves it as PKG files.
This method needs to contain:
- the selection of the package within the test management system
- the generation of the PKG file using the ObjectApi.PackageApi.
The passed tms Ids are defined using the GUI in ecu.test. You can choose between a selection
and input mode. The selection mode uses GetPackagesFromTms (if implemented) as source.
"""
for tmsId in tmsIds:
tmsObject = self.__tmsClient.GetItemFromTms(tmsId)
package = self.__objectApi.PackageApi.CreatePackage()
package.SetTestScriptId(tmsId)
self.__ConvertToEcuTest(package, tmsObject)
package.Save(f'{targetDirectory}/myFancyPackageName.pkg')
def GetProjectsFromTms(self) -> list[TmObjectInfo]:
"""
Returns the projects available from the test management system. The result will be displayed
in the dialog to import projects. For the chosen projects ImportProject() will be called.
"""
return [
TmObjectInfo(
'TestProject',
'TestProjectID_0',
{
'author': 'dummy guy',
'date': '30.02.2025',
'description': 'Object from tms that represents a project at ecu.test.',
},
),
]
def GetPackagesFromTms(self) -> list[TmObjectInfo]:
"""
Returns the packages available from the test management system. The result will be displayed
in the dialog to import packages. For the chosen packages ImportPackage() will be called.
"""
return [
TmObjectInfo(
'TestPackage',
'TestPackageID_0',
{
'author': 'dummy guy',
'date': '30.02.2025',
'description': 'Object from tms that represents a package at ecu.test.',
},
)
]
def __ConvertToTmsObject(self, ecuTestObject: str) -> str:
print(f'Convert ecu.test object {ecuTestObject} to a tms object.')
return 'DummyTmsObject'
def __ConvertToEcuTest(self, targetEcuTestObject, tmsObject) -> None:
print(f'Convert tms object {tmsObject} to ecu.test object {targetEcuTestObject}')
class TmsRestClient:
"""
Class that knows the concrete communication api (e.g. OpenAPI-client via Rest) to the test
management system.
"""
def __init__(self) -> None:
print('TMS: Setup concrete communication details to test management system.')
self.__isLoggedIn = False
def Login(self) -> None:
print('TMS: Process login.')
self.__isLoggedIn = True
def Logout(self) -> None:
print('TMS: Process logout.')
self.__isLoggedIn = False
def IsLoggedIn(self) -> bool:
return self.__isLoggedIn
def CreateItemAtTms(self, tmsObject: str) -> str:
print(f'TMS: Create new item at tms for {tmsObject}.')
return 'newTmsId'
def GetItemFromTms(self, tmsId: str) -> str:
print(f'TMS: Get item from tms for id {tmsId}.')
return 'DummyTmsItem'