如果其中一个ant测试失败,如何使Jenkins显示失败

时间:2011-07-13 15:58:22

标签: ant hudson jenkins

我有几个测试应该执行,无论彼此的成功与否,如果至少有一个测试失败,我希望Jenkins / Hudson显示红色指示灯。我的当前(为简化而简化)配置如下:

ci.sh

...
ant
...

的build.xml

...        
<target name="AllTests">
    <antcall target="TestA"/>
    <antcall target="TestB"/>
    <antcall target="TestC"/>
</target>

<target name="TestA">
    ...
    <exec executable="..." failonerror="false"/>
    ...
</target>

<target name="TestB">
    ...
    <exec executable="..." failonerror="false"/>
    ...
</target>

<target name="TestC">
    ...
    <exec executable="..." failonerror="false"/>
    ...
</target>
...

如何让所有测试都能执行,但如果三个中的至少一个失败,ant / Jenkins会失败?

6 个答案:

答案 0 :(得分:1)

我会在你的ant脚本中设置一个变量,如果测试失败,让ant脚本退出该变量。如果退出代码不是零,则Jenkins作业失败。如下所示:

#!/bin/bash
zero="0"

*** run your ant script ***

if [$? -gt $zero]; # check to see if exit code of ant script is great than zero
then
    exit(5) # non-zero exit means failure.
else
    echo Success
    exit(0)
fi

答案 1 :(得分:1)

我发现parallel任务的线程数设置为“1”作为可行的解决方法。这并不完美,但build.xml的变化很小:

<强>的build.xml

...        
<target name="AllTests">
    <parallel threadCount="1" timeout="900000">
        <antcall target="TestA"/>
        <antcall target="TestB"/>
        <antcall target="TestC"/>
    </parallel>
</target>

<target name="TestA">
    ...
    <exec executable="..." failonerror="false"/>
    ...
</target>

<target name="TestB">
    ...
    <exec executable="..." failonerror="false"/>
    ...
</target>

<target name="TestC">
    ...
    <exec executable="..." failonerror="false"/>
    ...
</target>
...

答案 2 :(得分:0)

咦?默认情况下,AFAIK,ant不会因测试失败或测试错误而停止。然后可以告诉Hudson / Jenkins收集JUnit测试报告并根据测试结果更改构建状态...(Jenkins至少有一个允许您设置水印的插件)

请参阅http://ant.apache.org/manual/Tasks/junit.html - 默认情况下,haltonfailure和haltonerror已关闭。

答案 3 :(得分:0)

这是顺序ant解决方案,但它会更改build.xml一点。它使用临时文件来标记一个测试失败。

<?xml version="1.0" encoding="UTF-8"?>
<project name="project" default="AllTests">
    <property name="test-failed.file.path" location="test.failed" />

    <macrodef name="checkTestResult">
        <sequential>
            <condition property="allOK">
                <equals arg1="${test.result}" arg2="0" />
            </condition>
            <antcall target="-create-test-failed-file" />
        </sequential>
    </macrodef>

    <target name="-create-test-failed-file" unless="${allOK}">
        <touch file="${test-failed.file.path}" />
    </target>

    <target name="AllTests">
        <delete file="${test-failed.file.path}" />

        <antcall target="TestA" />
        <antcall target="TestB" />
        <antcall target="TestC" />

        <available file="${test-failed.file.path}" property="oneTestFailed" />
        <delete file="${test-failed.file.path}" />
        <fail if="${oneTestFailed}" message="At least one test failed" />
    </target>

    <target name="TestA">
        <exec executable="hostname"
              failonerror="false"
              resultproperty="test.result" />
        <checkTestResult />
    </target>

    <target name="TestB">
        <!-- This one fails -->
        <exec executable="hostname"
              failonerror="false"
              resultproperty="test.result">
            <arg value="--NoSuchOption" />
        </exec>
        <checkTestResult />
    </target>

    <target name="TestC">
        <exec executable="hostname"
              failonerror="false"
              resultproperty="test.result" />
        <checkTestResult />
    </target>
</project>

答案 4 :(得分:0)

您可以安装Log Parser Plugin以使控制台日志中的某些模式的构建失败。

因此所有脚本都可以运行,但是当日志部分(可以配置为打印)中出现错误/警告时,构建可能会在最后失败。

可以在解析规则文件中配置规则,例如:

# match line starting with 'error ', case-insensitive
error /(?i)^error /

# list of warnings here...
warning /[Ww]arning/
warning /WARNING/

如果您的3个脚本和Jenkins在第1个脚本上停止构建,您可以add #!/bin/sh -x to the Execute shell不停止第一个错误,并使用上面提到的 Log Parser 插件最后失败了。

另请参阅:How/When does Execute Shell mark a build as failure in Jenkins?

答案 5 :(得分:0)

只需添加我的观点:如果您想手动使ANT任务失败并希望您的jenkins工作失败。

然后你可以使用这样的东西

<fail status="-1" message="" />
根据Ant任务返回的错误代码,jenkins作业失败。

相关问题