Reputation: 799
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
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'
}
Upvotes: 1
Views: 3762
Reputation: 111625
How are you running the Gradle script?
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.
But what you want is for the Jenkins build to be marked as unstable rather than failed.
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
}
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.
The Gradle java
plugin writes test results in JUnit XML format to build/test-results/TEST-HelloTest.xml
(in your example).
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.
Upvotes: 4
Reputation: 719
Well, I couldn't understand your code but you can add post-build actions in jenkins which can affect the status of the job.
eg : Text finder plugin : search keywords in the log files you specified and use that to downgrade a "successful" build to be a failure.
Log Parser Plugin : to show summary of errors and warnings
Upvotes: 4