在TeamCity中使用MBUnit

时间:2020-03-05 18:37:57  来源:igfitidea点击:

我正在使用TeamCity Continuous Integration服务器在Linux上编译一个NAnt项目。我已经能够通过命令行运行器在单声道上运行NAnt来生成测试报告,但是没有像NAnt Runner这样使用报告的选项。我还将MBUnit用于测试框架。

如何在测试报告中合并并为构建显示"测试失败:1(1个新功能,已通过:3049)"?

更新:看一下MBUnitTask,它是一个NAnt任务,它使用TeamCity期望从NUnit发送消息,因此它使我们可以使用TeamCity的所有功能进行测试。

MBUnitTask

更新:Galio具有更好的支持,因此我们只需要引用Galio MBUnit 3.5 dll而不是MBUnit 3.5 dll,然后切换到galio运行器即可使其运行。

解决方案

回答

TeamCity监视构建中的命令行输出。我们可以通过在输出中插入某些标记来让它知道测试的进行情况。请参阅http://www.jetbrains.net/confluence/display/TCD3/Build+Script+Interaction+with+TeamCity。例如

##teamcity[testSuiteStarted name='Test1']

将使TeamCity知道已开始一组测试。使用MbUnit,我们无法在测试运行时输出这些标记,但是可以转换它输出的XML文件。这是我正在使用的XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:template match="/">

        <xsl:apply-templates/>

    </xsl:template>

    <xsl:template match="assemblies/assembly">
##teamcity[testSuiteStarted name='<xsl:value-of select="@name" />']

        <xsl:apply-templates select="//run" />

##teamcity[testSuiteFinished name='<xsl:value-of select="@name" />']
    </xsl:template>

    <xsl:template match="run">

        <xsl:choose>
            <xsl:when test="@result='ignore' or @result='skip'">
        ##teamcity[testIgnored name='<xsl:value-of select="@name" />' message='Test Ignored']
            </xsl:when>
            <xsl:otherwise>
        ##teamcity[testStarted name='<xsl:value-of select="@name" />']
            </xsl:otherwise>
        </xsl:choose>

        <xsl:if test="@result='failure'">
            ##teamcity[testFailed name='<xsl:value-of select="@name" />' message='<xsl:value-of select="child::node()/message"/>' details='<xsl:value-of select="normalize-space(child::node()/stack-trace)"/>']
        </xsl:if>

        <xsl:if test="@result!='ignore' and @result!='skip'">
        ##teamcity[testFinished name='<xsl:value-of select="@name" />']
        </xsl:if>

    </xsl:template>

</xsl:stylesheet>

回答

这是我想出的

如何合并到测试报告中?

首先,我们需要获得mbunit才能生成XML和HTML报告。命令行参数如下所示

/rt:Xml /rt:Html /rnf:mbunit /rf:..\reports

这会将报告生成到一个名为reports的目录中,该文件将被称为mbunit.xml和mbunit.html

接下来,我们要将这些文件添加为构建中的工件

build\reports\* => Reports

最后一步是告诉teamcity将其添加为构建的选项卡

找到.BuildServer \ config \ main-config.xml并添加此行
(在Windows中,该目录位于c:\ Documents and Settings \中,在Linux中,该目录位于/ root目录中)

<report-tab title="Tests" basePath="Reports" startPage="mbunit.html" />

如何为构建显示"测试失败:1(1个新),通过:3049"?

TeamCity寻找一个名为teamcity-info.xml的文件,我们可以在其中粘贴消息以进行显示。实际测试计数实际上只是纯文本。我认为我们可以将文件作为工件添加,但我也将其放在构建的根目录中。

在NAnt中,我们将要使用此命令在MBUnit XML报表上执行XSLT

<style style="includes\teamcity-info.xsl" in="reports\mbunit.xml" out="..\teamcity-info.xml" />

实际的xsl看起来像这样。
(注意:{和}在xsl中保留,因此我们必须使用params)

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="cbl" select="'{'"/>
<xsl:param name="cbr" select="'}'"/>
<xsl:template match="/">
<xsl:for-each select="report-result/counter">

<build number="1.0.{concat($cbl,'build.number',$cbr)}">
    <xsl:if test="@failure-count &gt; 0">
        <statusInfo status="FAILURE">    
            <text action="append"> Tests failed: <xsl:value-of select="@failure-count"/>, passed: <xsl:value-of select="@success-count"/></text>
        </statusInfo>
    </xsl:if>
    <xsl:if test="@failure-count = 0">
        <statusInfo status="SUCCESS">
            <text action="append"> Tests passed: <xsl:value-of select="@success-count"/></text>
        </statusInfo>
    </xsl:if>

</build>
</xsl:for-each>

</xsl:template>
</xsl:stylesheet>

这会给你一个看起来像这样的文件

<build number="1.0.{build.number}">
   <statusInfo status="FAILURE">
      <text action="append">Tests failed: 16, passed: 88</text>
   </statusInfo>
</build>

回答

Gallio现在具有扩展功能,可以输出TeamCity服务消息。
只需使用随附的Gallio.NAntTasks.dll并启用TeamCity扩展即可。 (在下一发行版中将不再需要)

回答

适用于Windows Vista,Windows 7的TeamCity侧边栏小工具
http://teamcity-gadget.com