5

The Karate docs have 2 examples of testing with Karate and JUnit5:

I created a project using only karate-core (0.9.9-RC2) with the second example and don't need karate-junit5. What is the difference?

I'm very impressed with what Karate is offering and I'm interested in creating a test harness that could need to pick tests out according to runtime criteria and will also need custom reporting so I'm looking at the easiest ways of hooking into the test runner. The code in karate-junit5 seems a good starting point but I just want to understand why it is not actually necessary for normally running tests.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
Pete Edwards
  • 97
  • 2
  • 4

1 Answers1

5

Great question and I can answer this. You are right, directly using the Runner class is sufficient, and this is the only way to run tests in parallel.

However, if you use the JUnit runner you get the "green bar" experience you are used to as a Java developer and we make sure the IDE UI (e.g the tree-view of tests run and failures etc) is populated. In IntelliJ you even see the logs when you click on one of the "test case" nodes if I remember right.

EDIT: note that I mean here you can see the data of each Scenario in the IDE, not the Java class of the containing JUnit test. Screenshot of IntelliJ below. So this can be convenient to quickly see what failed and the corresponding error message without having to dive into logs / HTML reports:

enter image description here

But once you get used to looking at the HTML reports that Karate outputs, in my opinion - you don't need the JUnit integration.

To summarize: the JUnit support is

a) because we started as a Cucumber extension (there is even a @KarateOptions annotation that is being deprecated)

b) because some folks like the IDE integration + experience and it is useful to run "one test at a time" in dev-mode

Your comment makes me more convinced that we can eventually deprecate the JUnit support. Would be good to get your feedback on this once you have had a chance to play around more.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • 1
    Thanks for the helpful and fast response. I'm returning to Java after a few years away so have no prior expectations. It seems that I get the IDE integration and ability to run individual tests without the junit5 package anyway which is valuable - is there something I am missing by not using karate-junit5? – Pete Edwards Jan 05 '21 at 11:59
  • 1
    Also, to dynamically control the features being run could I use JUnit5's dynamic tests? This is why I was looking at the karate-junit5 code but perhaps I can do something in the TestRunner example you provided and look at the ReportUtils example for output. Does that sound right? Thanks. – Pete Edwards Jan 05 '21 at 12:01
  • 1
    @PeteEdwards to dynamically control features, you can pass a custom list of files to the `Runner` here's an example: https://github.com/intuit/karate/blob/develop/karate-demo/src/test/java/demo/DemoTestSelected.java - I edited my answer for your question on IDE support – Peter Thomas Jan 05 '21 at 12:05
  • @PeteEdwards thanks I realized I missed the key part - that the Karate JUnit support surfaces the data about each `Scenario` in the IDE. this can be useful to quickly see what failed and the corresponding error message without having to dive into logs / HTML reports – Peter Thomas Jan 05 '21 at 12:12
  • 1
    That really helps - I see now that the integration converts each Scenario to a separate test to surface that data so a best of both worlds would be a parallel runner that supported this integration. For now I think I could keep both styles of runner - one for development and the other for the full test harness. Your input has really helped me understand the differences. Thanks. – Pete Edwards Jan 05 '21 at 13:01