How to run Karate and Gatling with Gradle build system

99封情书 提交于 2020-01-13 18:05:21

问题


I'm trying to run a Karate test as a Gatling performance test. My entire setup works perfect when using Maven. I am being forced to use Gradle however. When trying to run under Gradle the below disaster unfolds.

Appreciate any ideas what might be causing KarateAction to crash.

MyAPI.scala

class MyAPI extends Simulation {
    val protocol = karateProtocol(
        "/myendpoint" -> Nil
    )

    val action = karateFeature("classpath:org/mycompany/karate/tests/myAPI.feature@test=myTag")

    setUp(
        scenario("my-api")
            .exec(action)
            .inject(rampUsersPerSec(1) to (5) during (5 seconds))
            .protocols(protocol)
    )
}

build.gradle

buildscript {
    ext {
        karateVersion = '0.9.2'
    }
}

apply plugin: 'scala'

configurations {
    gatling
}

dependencies {
    testCompile("com.intuit.karate:karate-apache:${karateVersion}")
    testCompile("com.intuit.karate:karate-mock-servlet:${karateVersion}")
    testCompile("com.intuit.karate:karate-junit4:${karateVersion}")
    testCompile("com.intuit.karate:karate-gatling:${karateVersion}")
    testCompile("net.masterthought:cucumber-reporting:3.8.0")

    gatling "org.scala-lang:scala-library:2.12.8"
    gatling "io.gatling:gatling-app:3.0.2"
    gatling "io.gatling.highcharts:gatling-charts-highcharts:3.0.2"
    gatling "com.intuit.karate:karate-gatling:${karateVersion}"
}

sourceSets {
    simulations {
        scala {
            srcDirs = ['src/test/java/org/mycompany/karate/perf']
        }
        resources {
            srcDirs = ['src/test/java/org/mycompany/karate/perf']
        }

        compileClasspath += configurations.gatling
    }
    test {
        resources {
            srcDir file('src/test/java')
            exclude '**/*.java'
        }
    }
}

test {
    systemProperty "karate.options", System.properties.getProperty("karate.options")
    systemProperty "karate.env", System.properties.getProperty("karate.env")
    outputs.upToDateWhen { false }
}

task gatlingRun(type: JavaExec) {
    description = 'Run Gatling Tests'
    new File("${buildDir}/reports/gatling").mkdirs()

    classpath = sourceSets.simulations.runtimeClasspath += configurations.gatling

    main = "io.gatling.app.Gatling"
    args = [
        '-s', 'org.mycompany.karate.perf.MyAPI',
        '-sf', 'src/test/java/org/mycompany/karate/perf',
        '-rf', "${buildDir}/reports/gatling"
    ]
    systemProperties System.properties
}

command line

gradle gatlingRun

command line output

> Task :compileSimulationsJava NO-SOURCE

> Task :compileSimulationsScala
Pruning sources from previous analysis, due to incompatible CompileSetup.
there were 6 feature warnings; re-run with -feature for details
one warning found

> Task :processSimulationsResources NO-SOURCE
> Task :simulationsClasses

> Task :gatlingRun
16:40:27.238 [main] INFO io.gatling.core.config.GatlingConfiguration$ - Gatling will try to use 'gatling.conf' as custom config file.
16:40:27.598 [GatlingSystem-akka.actor.default-dispatcher-3] INFO akka.event.slf4j.Slf4jLogger - Slf4jLogger started
16:40:28.242 [GatlingSystem-akka.actor.default-dispatcher-3] INFO io.gatling.core.stats.writer.ConsoleDataWriter - Initializing
16:40:28.242 [GatlingSystem-akka.actor.default-dispatcher-4] INFO io.gatling.core.stats.writer.LogFileDataWriter - Initializing
16:40:28.248 [GatlingSystem-akka.actor.default-dispatcher-3] INFO io.gatling.core.stats.writer.ConsoleDataWriter - Initialized
16:40:28.253 [GatlingSystem-akka.actor.default-dispatcher-4] INFO io.gatling.core.stats.writer.LogFileDataWriter - Initialized
Simulation org.mycompany.karate.perf.MyAPI started...
16:40:28.359 [GatlingSystem-akka.actor.default-dispatcher-2] ERROR com.intuit.karate.gatling.KarateAction - 'classpath:org/mycompany/karate/tests/myAPI.feature@test=myTag' crashed on session Session(my-api,1,1558395628341,Map(),0,OK,List(),io.gatling.core.protocol.ProtocolComponentsRegistry$$Lambda$329/1759250827@7cee98de), forwarding to the next one
java.lang.NullPointerException: null
        at com.intuit.karate.Resource.<init>(Resource.java:55)
        at com.intuit.karate.core.FeatureParser.parse(FeatureParser.java:75)
        at com.intuit.karate.FileUtils.parseFeatureAndCallTag(FileUtils.java:155)
        at com.intuit.karate.Runner.callAsync(Runner.java:183)
        at com.intuit.karate.gatling.KarateAction.execute(KarateAction.scala:77)
        at io.gatling.core.action.Action.$bang(Action.scala:38)
        at io.gatling.core.action.Action.$bang$(Action.scala:38)
        at com.intuit.karate.gatling.KarateAction.io$gatling$core$action$ChainableAction$$super$$bang(KarateAction.scala:37)
        at io.gatling.core.action.ChainableAction.$bang(Action.scala:63)
        at io.gatling.core.action.ChainableAction.$bang$(Action.scala:61)
        at com.intuit.karate.gatling.KarateAction.io$gatling$core$action$ExitableAction$$super$$bang(KarateAction.scala:37)
        at io.gatling.core.action.ExitableAction.$bang(BlockExit.scala:138)
        at io.gatling.core.action.ExitableAction.$bang$(BlockExit.scala:136)
        at com.intuit.karate.gatling.KarateAction.$bang(KarateAction.scala:37)
        at io.gatling.core.controller.inject.Workload.startUser(Workload.scala:55)
        at io.gatling.core.controller.inject.Workload.injectUser(Workload.scala:64)
        at io.gatling.core.controller.inject.open.OpenWorkload.$anonfun$injectBatch$1(OpenWorkload.scala:35)
        at io.gatling.core.controller.inject.open.OpenWorkload.$anonfun$injectBatch$1$adapted(OpenWorkload.scala:35)
        at io.gatling.core.controller.inject.open.UserStream.withStream(UserStream.scala:58)
        at io.gatling.core.controller.inject.open.OpenWorkload.injectBatch(OpenWorkload.scala:35)
        at io.gatling.core.controller.inject.Injector.$anonfun$inject$1(Injector.scala:60)
        at io.gatling.core.controller.inject.Injector.$anonfun$inject$1$adapted(Injector.scala:59)
        at scala.collection.Iterator.foreach(Iterator.scala:941)
        at scala.collection.Iterator.foreach$(Iterator.scala:941)
        at scala.collection.AbstractIterator.foreach(Iterator.scala:1429)
        at scala.collection.MapLike$DefaultValuesIterable.foreach(MapLike.scala:213)
        at io.gatling.core.controller.inject.Injector.io$gatling$core$controller$inject$Injector$$inject(Injector.scala:59)
        at io.gatling.core.controller.inject.Injector$$anonfun$1.applyOrElse(Injector.scala:92)
        at io.gatling.core.controller.inject.Injector$$anonfun$1.applyOrElse(Injector.scala:82)
        at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:38)
        at akka.actor.FSM.processEvent(FSM.scala:684)
        at akka.actor.FSM.processEvent$(FSM.scala:681)
        at io.gatling.core.controller.inject.InjectorFSM.processEvent(InjectorFSM.scala:37)
        at akka.actor.FSM.akka$actor$FSM$$processMsg(FSM.scala:678)
        at akka.actor.FSM$$anonfun$receive$1.applyOrElse(FSM.scala:672)
        at akka.actor.Actor.aroundReceive(Actor.scala:517)
        at akka.actor.Actor.aroundReceive$(Actor.scala:515)
        at io.gatling.core.akka.BaseActor.aroundReceive(BaseActor.scala:24)
        at akka.actor.ActorCell.receiveMessage(ActorCell.scala:588)
        at akka.actor.ActorCell.invoke(ActorCell.scala:557)
        at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:258)
        at akka.dispatch.Mailbox.run(Mailbox.scala:225)
        at akka.dispatch.Mailbox.exec(Mailbox.scala:235)
        at akka.dispatch.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
        at akka.dispatch.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
        at akka.dispatch.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
        at akka.dispatch.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
16:40:28.359 [GatlingSystem-akka.actor.default-dispatcher-2] DEBUG io.gatling.core.action.Exit - End user #1
16:40:28.360 [GatlingSystem-akka.actor.default-dispatcher-2] DEBUG io.gatling.core.controller.inject.open.OpenWorkload - Start user #1
16:40:28.363 [GatlingSystem-akka.actor.default-dispatcher-2] DEBUG io.gatling.core.controller.inject.open.OpenWorkload - Injecting 4 users in scenario my-api, continue=true
16:40:28.364 [GatlingSystem-akka.actor.default-dispatcher-2] DEBUG io.gatling.core.controller.inject.Injector - End user #1
16:40:29.347 [GatlingSystem-akka.actor.default-dispatcher-2] DEBUG io.gatling.core.controller.inject.open.OpenWorkload - Injecting 0 users in scenario my-api, continue=true
16:40:29.373 [GatlingSystem-akka.actor.default-dispatcher-2] ERROR com.intuit.karate.gatling.KarateAction - 'classpath:org/mycompany/karate/tests/myAPI.feature@test=myTag' crashed on session Session(my-api,2,1558395629372,Map(),0,OK,List(),io.gatling.core.protocol.ProtocolComponentsRegistry$$Lambda$329/1759250827@7cee98de), forwarding to the next one

回答1:


This is just a guess.

Your build.gradle defines src/test/java as resource folder:

    test {
        resources {
            srcDir file('src/test/java')
            exclude '**/*.java'
        }
    }

The karate gatling demo defines src/test/scala as resource folder.

This is necessary, because otherwise the *.feature files next to your scala/java source files are not treated as part of the resulting artifact.

Furthermore, you are using the classpath of the simulation source set when running gatling test. Make sure, that your *.feature are included in that classpath.

As an alternative, you can put your *.feature files under src/test/resources/com/your/package, but this increases "the distance" between your feature and source code files.

Debugging Hint: Print the file tree of your build/resources folder in order to check whether or not the feature files are included in the resulting build artifact and that the path matches the path you are referencing in your runner.

Let me know if this was useful!




回答2:


I have a gradle project with scala and java, and had to create a gradle task to move resources to the right folder, in order to make them available.

task copyResources(type: Copy) {
  from ("src/test/java/") {
    include "/**/*.feature"
    include "karate-config*.js"
    include "logback*.xml"
    include "/**/*.csv"
    include "/**/*.json"
  }
  into "$buildDir/classes/java/test/"

}


来源:https://stackoverflow.com/questions/56229508/how-to-run-karate-and-gatling-with-gradle-build-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!