在詹金斯上检测测试失败 [英] Detect test failures on jenkins

查看:285
本文介绍了在詹金斯上检测测试失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


从5天开始我一直在詹金斯(Jenkins)周围玩耍,但是我遇到了问题.我有一个已经用JUnit进行了单元测试的Java代码,并且正在使用Gradle Build来构建代码.我故意尝试使三个测试中的一个测试失败,并且gradle build报告失败!这是预期的.但是我将代码推送到github SampleTestProject 上,一分钟后(根据配置,在Jenkins上触发了构建).即使在本地计算机上构建测试失败,詹金斯仍将构建标记为成功!!

我要发布的代码确实很糟糕,但是可以亲身体验jenkins
主类


I have been playing around Jenkins since 5 days but I have a problem. I have a Java Code that has been unit tested with JUnit and I am using Gradle Build to build the code. I have deliberately tried to fail a test out of the three tests and gradle build reports a failure! which was expected. Yet I pushed my code onto github SampleTestProject and a build was triggered on Jenkins after a minute(as configured). Yet jenkins marks the build as successful even though the test was failed while building on a local machine!!

The Code that I am going to post is really bad yet it is ok to have a hands-on experience on jenkins
The Main class

   package com.bitwise.test;

    /**
     * Created by AniruddhaS on 2/11/2016.
     */
    public class Hello {
        public String sayHello() {
            return "Hello";
        }

        public int addArgs(int i, int i1) {
            return (i+i1);
        }

        public String sayBye() {
            return "Bye";
        }

        public int mulArgs(int i, int i1) {
            return (i*i1);
        }
    }


测试类


The Test class

package com.bitwise.test;

import junit.framework.Assert;
import org.junit.Test;
import org.junit.experimental.theories.suppliers.TestedOn;

/**
 * Created by AniruddhaS on 2/11/2016.
 */
public class HelloTest {
    @Test
    public void itShouldReturnHelloOnCallingHelloMethod(){
        //given
        Hello vector=new Hello();
        //when
        Assert.assertSame("Hello",vector.sayHello());
        //then
    }
    @Test
    public void itShouldReturnAValueAfterAdditionOfTheArgumentValues(){
        //given
        Hello adder=new Hello();
        //when
        Assert.assertEquals(3,adder.addArgs(2,1));
        //then
    }
    @Test
    public void itShouldPrintByeWhenRelevantFunctionIsCalled(){
        //given
        Hello bye=new Hello();
        //when
        Assert.assertSame("Bye",bye.sayBye());
        //then
    }
    @Test
    public void itShouldMultiply(){
        //given
        Hello bye=new Hello();
        //when


       Assert.assertEquals(6,bye.mulArgs(5,3));/*here mulArgs emits 15 but 
                                     test fails since expected value is 6*/
        //then
    }
}


build.gradle


build.gradle

group 'hello'
version '1.0'

apply plugin: 'java'

sourceCompatibility = 1.5

repositories {
    mavenCentral()
}
task test1<<
        {
    println("hello, test running")
}

test{
    testLogging{
        events 'started','passed'
        events 'started','failed'
    }
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
}


请提出如何解决以上问题!
在此先感谢:)


Kindly suggest how to sort out the above problem!
Thanks in Advance :)

推荐答案

您如何运行Gradle脚本?

How are you running the Gradle script?

您是否正在使用执行外壳"步骤来执行类似./gradlew test的操作,还是正在使用

Whether you're using an "Execute shell" step to do something like ./gradlew test, or you're using the Gradle plugin to execute your test task, Jenkins will mark the build as failed if the Gradle script fails, e.g. due to a test assertion failure.

但是您想要的是将Jenkins构建标记为不稳定而不是失败.

But what you want is for the Jenkins build to be marked as unstable rather than failed.

为此,您需要更新Gradle脚本,以免在build.gradletest块中将测试失败视为致命错误:

To do so, you need to update your Gradle script to not treat test failures as fatal in your test block of the build.gradle:

test {
  ignoreFailures = true
}

然后,您可以在Jenkins中运行Gradle脚本,并对其进行分析 JUnit结果,这将根据测试是否通过来将构建状态设置为成功不稳定.

Then you can run your Gradle script in Jenkins and let it analyse the JUnit results, which will set the build status to successful or unstable, depending on whether the tests pass or fail.

Gradle java插件将JUnit XML格式的测试结果写入build/test-results/TEST-HelloTest.xml(在您的示例中).

The Gradle java plugin writes test results in JUnit XML format to build/test-results/TEST-HelloTest.xml (in your example).

要分析结果,请转到作业配置中的构建后操作",并添加发布JUnit测试结果报告",然后在测试报告XML"字段中输入**/TEST-*.xml.

To analyse the results, go to "Post-build actions" in the job configuration, and add "Publish JUnit test result report", and enter **/TEST-*.xml in the "Test report XMLs" field.

这篇关于在詹金斯上检测测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆