Junit test problem


(Firas Fafi) #1

Hello, I have a problem with creating Junit Tests, gradle build successfully but no reports are generated.

Does gradle run tests in same way as Ant? Well here is my code: First I have created a new configurations and sourceSets:

configurations {
  myproject{transitive=false}
  unitTest{transitive=false}
 }
repositories {
//some sources....
}
  dependencies {
myproject //some jars...
unitTest unitTest 'junit:junit:'+junitVersion
}
sourceSets{
  myproject{
    java.srcDirs=[
      'backend',
      'shared',
      'dashboard_web',
      'gwt_common',
      'surveys-backend',
      'widgety'
    ]
    output.classesDir=file("$buildDir/war/WEB-INF/classes")
    compileClasspath= configurations.myproject
    runtimeClasspath= output+compileClasspath
    }
 unitTest{
    java.srcDirs=[
      'backend',
      'shared',
      'dashboard_web',
      'gwt_common',
      'surveys-backend',
      'widgety',
      'test'
    ]
    output.classesDir=file("$buildDir/tests")
    compileClasspath= sourceSets.myproject.output+configurations.unitTest+configurations.myproject
    runtimeClasspath= output+compileClasspath
    }

Then I have created new task for The Junit:

task RunUnitTests(type:Test){
      testClassesDir= sourceSets.unitTest.output.classesDir
  classpath=sourceSets.unitTest.runtimeClasspath
  forkEvery = 10
  maxParallelForks = 4
      //test.testReport = false
  // Disable automatic inspections.
 // scanForTestClasses = false
      // Include test classes
      include '**/*Test.java'
  // Exclude test classes
  exclude // some classes..
       test.testReportDir = file("$buildDir/reportsDirs")
  test.testResultsDir = file("$buildDir/test-results")
}

Finally gradle compile myproject then compile unitTest and return successfully with no reports, I think it has done only the compiling and not running the Junit tests.

Thank you for your help.


(Firas Fafi) #2

Solved, well the problem was in the excluded files list. I think I have to see how define correctly filters for running Junit Tests.


(Firas Fafi) #3

I’m not so sure that this solution work’s, because the reports appeared only once and after that it’s gone and I back to the same situation.


(Firas Fafi) #4

Well I have solved the problem: Actually for including and excluding many files from running tests list task you should do like this

// include='**/*pattern1.java' also correct
  includes=[
'**/*pattern1.java'
]
  excludes=[
'**/*pattern1.java',
'**/*pattern2,java'
]
..

and not like this:

...
include **/*pattern1.java'
exclude '**/*pattern1.java',
'**/*pattern2,java'
..

Finally, the tips consist of checking the patterns and adding “=” after include and exclude same for excludes and includes .

Well I hope this well help someone in similar situation like mine.

Thank you!!


(Peter Niederwieser) #5

I don’t see how that would make a difference. However, the includes/excludes are about which class files to execute, so it should be '.class ’ or ‘.*’ instead of ‘.java’.


(Firas Fafi) #6

Yes you are right, I have just learned that and back to tell you about it, Thank you Peter


(Firas Fafi) #7

Update, the main problem is caused by the extention it should be ‘.class’ not ‘.java’. Well I made this mistake because in Ant the code look like this:

<junit showoutput="yes" printsummary="yes" logfailedtests="yes" errorProperty="test.failed" failureProperty="test.failed">
   <classpath>
    <pathelement path="${build.dir}/src" />
    <pathelement path="${target.bin}" />
    <fileset dir="${basedir}/lib">
     <include name="**/*.jar" />
    </fileset>
   </classpath>
   <formatter type="xml" />
     <!--launching neccessary tests -->
   <batchtest fork="true" todir="${report.light.dir}">
    <fileset dir="${build.dir}/src">
     <include name="**/*Test.java" />
     <exclude name="**/NotificationDaoImplTest.java" />
     <exclude name="**/*MacroLoader*Test.java" />
     <exclude name="**/*LaunchMyDSOPTest.java" />
     <exclude name="**/*MTPDBDumpParserTest.java" />
     <exclude name="**/*CorrespondenceParserForDELAMAISONTest.java" />
.....

so be careful when you move from Ant to Gradle. Finally the solution look simply like this:

task RunUnitTests(type:Test){
  testClassesDir= sourceSets.unitTest.output.classesDir
  classpath=sourceSets.unitTest.runtimeClasspath
  forkEvery = 10
  maxParallelForks = 4
   include '**/*Test.class'
  exclude '**/*otherTest.class'
  test.testReportDir = file("$buildDir/reportsDirs")
  test.testResultsDir = file("$buildDir/test-results")
}

Thank you again Peter!!