在 TeamCity 中使用 MBUnit
题
我正在使用 TeamCity 持续集成服务器在 Linux 上编译 NAnt 项目。我已经能够通过命令行运行程序在单声道上运行 NAnt 来生成测试报告,但没有像 NAnt 运行程序那样使用该报告的选项。我还使用 MBUnit 作为测试框架。
如何合并测试报告并显示“测试失败:1(1 个新),通过:3049”用于构建?
更新: 看一下 MBUnitTask,它是一个 NAnt 任务,它使用发送 TeamCity 期望从 NUnit 发送的消息,因此它允许您使用 TeamCity 的所有功能进行测试。
更新: Galio 有更好的支持,因此您只需引用 Galio MBUnit 3.5 dll 而不是 MBUnit 3.5 dll,然后切换到 galio 运行程序即可使其工作。
解决方案
Gallio 现在有一个扩展 输出 TeamCity 服务消息。只需使用附带的 Gallio.NAntTasks.dll 并启用 TeamCity 扩展即可。(这 在下一个版本中不需要)
其他提示
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 stayings ,在Linux上,它在 /root Dir中)
<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 中保留,因此我们必须使用参数)
<?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 > 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>
适用于 Windows Vista、Windows 7 的 TeamCity 侧边栏小工具http://teamcity-gadget.com