Executing a single junit test suite programmatically from a task


(Ahsan Rabbani) #1

Is there an easy way to do this? I have a situation where I want to be able to pass in a variable (test class) to a function and only have that suite executed.


(Ahsan Rabbani) #2

well i ended up just using ant within gradle. i’m not happy with this solution and it has its own problems. firstly, i would’ve liked to just use gradle’s test task. secondly, if a test fails the gradle build still succeeds. i actually did have a gradle test task which was configured to execute a single test class using an include pattern, but i had no way of passing that parameter into the task. and i don’t see any method in the project api that i can use to kick off a test from within a function.

def executeTestSuite(testClass) {
    ant.junit(fork: true, forkmode: 'once', printsummary: true) {
       classpath(path: sourceSets.test.runtimeClasspath.asPath)
                                        ant.formatter(type: 'brief')
       ant.batchtest(todir: "build/test-results") {
          ant.fileset(dir: sourceSets.test.output.classesDir) {
             include(name: "**/*${testClass}")
          }
       }
                                                                                                                                }
 }

(René Groeschke) #3

have you tried to use the ‘includes’ and ‘excludes’ properties to match the just one test class?


(Ahsan Rabbani) #4

yup, and that works except for the reason that i stated in my last post, which was that i had no way of passing the test class parameter into the task (like i do in the function in my previous post).


(Horcrux7) #5

I was not able to run this sample because ANT was not able to find its own junit classes. But the follow works for me with version 1.12

My single junit test is containing in mytests.jar. I extract the jar file because the test task accept only classes from a file directory.

copy {
            from zipTree( "${lib}/mytests.jar")
            into "${buildDir}/mytests/"
        }
        Test mytests = task(type:Test) {}
        mytests.setTestClassesDir( file( "${buildDir}/mytests/" ) )
        mytests.classpath += files( "${lib}/mytests.jar" )
        mytests.include '**/MyTest*'
         mytests.ignoreFailures = true
        mytests.executeTests()