TestRail集成

TestRail是一款用于管理、跟踪和组织软件测试的测试用例管理软件。TestRail插件自动将Squish Test Center的测试结果推送到TestRail,并允许由TestRail触发的自动化任务。

测试映射配置

一旦您设置了TestRail实例(无论是基于云的还是服务器的),登录Squish Test Center,在全局设置中开启TestRail集成。填写所需的字段,然后点击更新以保存设置。

TestRail集成设置

为建立到TestRail的连接,请输入以下信息

  • 服务器URL:您的TestRail实例的URL。
  • 用户名:拥有TestRail API访问权限的TestRail账户用户名。
  • API密钥:TestRail API的密码或API密钥。

    注意:强烈建议您使用TestRail API密钥而不是账户密码。有关如何设置您的API密钥的更多信息,请参阅TestRail文档中的用户名和API密钥

点击测试连接以测试是否可以通过服务器URL、用户名和API密钥访问TestRail实例。如果请求被重定向,则实际的URL将被设置为服务器URL的值。您可以点击更新来保存它。

点击同步项目按钮,获取TestRail项目,并使它们出现在关联列表中,在那里您可以映射TestRail项目与其Squish Test Center相反数。

拉取、映射和推送测试

要打开可追溯性视图,在相关项目的历史探索视图中选择TestRail。点击拉取按钮以获取相关TestRail项目的测试用例。

要将拉取的TestRail测试映射到Squish Test Center在可追溯性视图中管理的测试,请点击映射列中的表格条目中的编辑按钮。选择要关联的TestRail测试用例,并点击应用更改

点击推送按钮以创建新的TestRail运行并将关联的(组合)结果推送到每个TestRail测试用例。

注意:如果当前选定的批次包含带有.testrail.run标签的报告,结果将推送到指定的运行,而不是创建新的TestRail运行。例如,带有.testrail.run=R12345标签的报告结果将推送到ID为12345的TestRail运行。

您还可以使用如通过命令行上传中所述的testcentercmd命令行工具来执行推送和拉取操作。

配置自动执行Squish测试用例

注意:此功能仅支持TestRail的服务器(非云)版本,并需要具有API访问功能的Squish Test Center许可证。

要从TestRail中启用自动执行Squish测试用例,请将提供的UI脚本和触发脚本添加到您的TestRail实例,并指定Squish Test Center 测试执行设置。在TestRail案例、套件、运行和计划视图中将出现执行关联的Squish测试按钮。如果TestRail测试用例(或套件或运行中包含的案例)映射到一个或多个Squish测试用例,它将自动执行并将结果上传到Squish Test Center。

要自动执行Squish测试用例

  1. 作为TestRail管理员,在TestRail网络应用中创建一个新的UI脚本,并将其替换为提供的UI脚本内容
    name: Trigger Execution of associated Squish tests + PLANS AND RUNS
    description: Triggers execution of Squish tests for a run, test plan, suite or single case
    author: froglogic GmbH
    version: 1.0
    includes: ^(suites/view|cases/view|plans|runs)
    excludes: ^(runs/overview)
    
    js:
    $(document).ready( function() {
        /* Create the button. */
        var button = $( [
            '<div class="toolbar content-header-toolbar">',
            '<a class="toolbar-button toolbar-button-last toolbar-button-first content-header-button button-start" href="javascript:void(0)">',
            'Execute Associated Squish Test(s)',
            '</a>',
            '</div>'
        ].join('') );
        /* Add it to the toolbar. */
        $('#content-header .content-header-inner').prepend(button);
        /* Bind the click event to trigger the associated Squish tests. */
        $('a', button).click( function() {
            // find out which page we are on (suite/case or plan or run)
            // NOTE: a case always has suite context, but runs are not necessarily part of a plan, thus not always having a plan context
            if ( uiscripts.context.run ) {
                // run view page
                fetchExecutionFromRun(
                    // on success
                    function( execution ) {
                        triggerSquishTestExecutions( [ execution ] );
                    },
                    // on failure
                    function( errormsg ) {
                        App.Dialogs.error( errormsg );
                    }
                );
            } else if ( uiscripts.context.plan ) {
                // plan view page
                fetchExecutionListFromPlan(
                    // on success
                    function( executionlist ) {
                        triggerSquishTestExecutions( executionlist );
                    },
                    // on failure
                    function( errormsg ) {
                        App.Dialogs.error( errormsg );
                    }
                );
            } else if ( uiscripts.context.case ) {
                // case view page
                var execution = {};
                execution['user'] = uiscripts.context.user.email;
                execution['tests'] = [ uiscripts.context.case.id ];
                triggerSquishTestExecutions( [ execution ] );
            } else if ( uiscripts.context.suite ) {
                // suite view page
                fetchExecutionFromSuite(
                    // on success
                    function( execution ) {
                        triggerSquishTestExecutions( [ execution ] );
                    },
                    // on failure
                    function( errormsg ) {
                        App.Dialogs.error( errormsg );
                    }
                );
            } else {
                App.Dialogs.error( 'Could not find any context to execute a test in.' );
            }
    
        } );
    } );
    
    function fetchExecutionFromRun( onSuccess, onFailure )
    {
        get( 'get_run/' + uiscripts.context.run.id,
            function( run ) {
                get( 'get_tests/' + uiscripts.context.run.id,
                    function( tests ) {
                        get( 'get_configs/' + uiscripts.context.project.id,
                            function( configs ) {
                                var execution = {};
                                execution['user'] = uiscripts.context.user.email;
                                execution['tests'] = tests.map( function( item ) { return item.case_id; } );
                                execution['labels'] = [ { 'key': '.testrail.run', 'value': 'R' + uiscripts.context.run.id.toString() } ];
                                // if the run is part of a plan, attach a batch name and - if available - configurations
                                if ( uiscripts.context.plan ) {
                                    execution['batch'] = createPlanBatchName();
                                    execution['labels'] = execution['labels'].concat( createLabelsFromConfigIds( configs, run.config_ids ) );
                                }
                                onSuccess( execution );
                            },
                            function() {
                                onFailure( 'An error occurred while fetching run configurations for this project.' );
                            }
                        );
                    },
                    function() {
                        onFailure( 'An error occurred while fetching the test cases for this run.' );
                    }
                );
            },
            function() {
                onFailure( 'An error occurred while fetching the run.' );
            }
        );
    }
    
    function fetchExecutionListFromPlan( onSuccess, onFailure )
    {
        get( 'get_plan/' + uiscripts.context.plan.id,
            function( plan ) {
                get( 'get_configs/' + uiscripts.context.project.id,
                    function( configs ) {
                        var run_ids = [];
                        plan.entries.forEach( function( entry ) { run_ids = run_ids.concat( entry.runs.map( function( run ) { return run.id; } ) ) } );
                        if ( run_ids.length == 0 ) {
                            onFailure( 'Selected test plan does not contain any runs.' );
                            return;
                        }
                        var executionlist = [];
                        var batchName = createPlanBatchName();
                        var collectTests = function( runIds ) {
                            get( 'get_tests/' + runIds[0],
                                function( tests ) {
                                    var execution = {};
                                    execution['user'] = uiscripts.context.user.email;
                                    execution['tests'] = tests.map( function( item ) { return item.case_id; } );
                                    execution['labels'] = [ { 'key': '.testrail.run', 'value': 'R' + runIds[0].toString() } ];
                                    execution['batch'] = batchName;
                                    // get run and add configuration labels
                                    plan.entries.forEach(
                                        function( entry ) {
                                            entry.runs.forEach(
                                                function( run ) {
                                                    if ( run.id == runIds[0] ) {
                                                        execution['labels'] = execution['labels'].concat( createLabelsFromConfigIds( configs, run.config_ids ) );
                                                    }
                                                }
                                            );
                                        }
                                    );
                                    executionlist.push( execution );
                                    if ( runIds.length == 1 ) {
                                        onSuccess( executionlist );
                                    } else {
                                        collectTests( runIds.slice( 1, runIds.length ) );
                                    }
                                },
                                function() {
                                    onFailure( 'An error occurred while fetching tests.' );
                                }
                            );
                        };
                        collectTests( run_ids );
                    },
                    function() {
                        onFailure( 'An error occurred while fetching run configurations for this project.' );
                    }
                );
            },
            function() {
                onFailure( 'An error occurred while fetching the test plan.' );
            }
        );
    }
    
    function fetchExecutionFromSuite( onSuccess, onFailure )
    {
        get( 'get_cases/' + uiscripts.context.project.id + '&suite_id=' + uiscripts.context.suite.id,
            function( cases ) {
                var execution = {};
                execution['user'] = uiscripts.context.user.email;
                execution['tests'] = cases.map( function( item ) { return item.id; } );
                onSuccess( execution );
            },
            function() {
                onFailure( 'An error occurred while fetching the list cases for this suite.' );
            }
        );
    }
    
    function triggerSquishTestExecutions( executions )
    {
        $.ajax( {
            url: 'squishtestcenterintegration.php',
            dataType: 'json',
            contentType: 'application/json',
            data: JSON.stringify( executions ),
            type: 'POST',
            success: function()
            {
                location.reload();
            },
            error: function( data )
            {
                App.Dialogs.error(
                    'An error occurred while trying to trigger the associated Squish tests:\n' +
                    JSON.stringify( data )
                );
            }
        } );
        App.Dialogs.message(
            'Sending test execution(s) to Squish TestCenter...\n' +
            'The page will reload once the Squish test executions have been scheduled.',
            'Confirmation'
        );
    }
    
    function get( apiCall, onSuccess, onFailure ) {
        $.ajax( {
            url: 'index.php?/api/v2/' + apiCall,
            dataType: 'json',
            contentType: 'application/json',
            type: 'GET',
            success: function( data ) {
                onSuccess( data );
            },
            error: function( data ) {
                onFailure( data );
            }
        } );
    }
    
    function createLabelsFromConfigIds( configData, configIds ) {
        var groupTable = {};
        var configTable = {};
        configData.forEach(
            function( configGroup ) {
                groupTable[configGroup.id] = configGroup.name;
                configGroup.configs.forEach(
                    function( config ) {
                        configTable[config.id] = { name: config.name, groupId: config.group_id };
                    }
                );
            }
        );
        var labels = configIds.map(
            function( id ) {
                var config = configTable[id];
                return { key: groupTable[config.groupId], value: config.name };
            }
        );
        return labels;
    }
    
    function createPlanBatchName() {
        return uiscripts.context.plan.name + '-' + Date.now();
    }
  2. squishtestcenterintegration.php触发脚本文件放入TestRail安装目录。有关更多信息,请参阅TestRail文档中的测试自动化UI脚本
  3. 在触发php脚本中,编辑以下行,以包含您的Squish Test Center实例的URL地址以及用户登录名和密码。
    define('SQUISHTESTCENTER_ADDRESS', '[Squish TestCenter URL]');
    define('SQUISHTESTCENTER_API_USER', '[User login]');
    define('SQUISHTESTCENTER_API_PASSWORD', '[User password]');

    提供的用户必须具有访问Squish TestCenter API的权限。

  4. 检查Squish Test Center中的测试自动化设置全局设置 > 测试执行视图。对Squish找到您的测试套件,必须设置Squish测试套件目录
    • Squish安装目录:要使用的Squish安装的位置。它必须位于与Squish Test Center相同的计算机上。
    • 自定义Squishserver主机地址:运行squishserver的(远程)主机的地址或URL。
    • 自定义Squishserver端口:(远程)squishserver使用的端口。
    • Squish测试套件目录:将执行Squish Test Center的Squish测试套件的文件夹的文件路径。

如果在TestRail运行或计划视图中指定了运行,Squish Test Center结果上传将包含指定具有前置字母R的TestRail运行ID的.testrail.run标签。例如,.testrail.run=R12345

您可以在调度视图中查看计划的TestRail测试执行。

©2023 本文件内的Qt Company Ltd. 文档贡献都是各自所有者的版权。
此处提供的文档是根据由自由软件基金会发布的GNU自由文档许可协议版本1.3授权。
Qt及其相应标志是芬兰和/或世界其他国家的Qt Company Ltd.的商标。所有其他商标均为其各自所有者的财产。