【发布时间】:2016-05-23 06:01:32
【问题描述】:
自从 5 天以来,我一直在 Jenkins 周围玩耍,但我遇到了问题。我有一个已使用 JUnit 进行单元测试的 Java 代码,我正在使用 Gradle Build 来构建代码。我故意尝试使三个测试中的一个测试失败,并且 gradle build 报告失败!这是意料之中的。然而,我将我的代码推送到了 github SampleTestProject,并且在一分钟后(按照配置)在 Jenkins 上触发了构建。然而,即使在本地机器上构建测试失败,詹金斯仍将构建标记为成功!
我要发布的代码确实很糟糕,但可以在 jenkins 上进行动手体验
主类
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);
}
}
测试类
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'
}
请建议如何解决上述问题!
提前致谢 :)
【问题讨论】:
-
构建输出是否显示测试失败(Jenkins 不会注意到)或者是通过 Jenkins 运行构建时测试通过的实际问题?
标签: git jenkins gradle junit tdd