jbang icon indicating copy to clipboard operation
jbang copied to clipboard

Simplify running unit tests

Open ennishol opened this issue 3 years ago • 15 comments
trafficstars

I have a jbang script.java and TestScript.java, which includes //SOURCES script.java. build.gradle however doesn't contain

test {useJUnitPlatform()}

and this has to be added to each new project or when a project is regenerated. It would be nice, if the generated build.gradle would get it automatically.

After fixing build.gradle file, I can run the test from the IDE, but it would be also nice to be able to run it from the command line. I've used jbang junit.java -f TestScript.java to run the test:

///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS org.junit.platform:junit-platform-console-standalone:1.6.2
import org.junit.platform.console.ConsoleLauncher;
import static java.lang.System.*;

public class junit {   public static void main(String... args) {        ConsoleLauncher.main(args);    }}

but it doesn't pick up the tests

ennishol avatar Dec 29 '21 08:12 ennishol

Have you tried using 'jbang -t junit@jbangdev yourscript.java'

?

It provides the scaffolding for running a junit test.

About the gradle part - that does seem to make sense to add albeit not really grokking what that does if you already have a dependency on junit in your java file ?

maxandersen avatar Dec 29 '21 10:12 maxandersen

'jbang -t junit@jbangdev yourscript.java Unknown option: '-t'

ennishol avatar Dec 29 '21 10:12 ennishol

without test {useJUnitPlatform()} in gradle.build test are not found and not executed in IDE

ennishol avatar Dec 29 '21 10:12 ennishol

Sorry - missing init.

jbang init -t junit@jbangdev yourscript.java

maxandersen avatar Dec 29 '21 10:12 maxandersen

Used jbang init -t junit@jbangdev yourscript.java

  1. Problem one:
  • on command line run the test all ok,
  • then change yourscript to return wrong value,
  • re-run the test and it does not fail: yourscript.java is not re-compiled if the test is re-executed
  1. Problem in IDE still remains
Execution failed for task ':test'.
> No tests found for given includes: [yourscriptTest](--tests filter)

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

after adding test {useJUnitPlatform()} it works

ennishol avatar Dec 29 '21 11:12 ennishol

Ah yes. We should include sources in dirty check.

Until then run with jbang --fresh to force compile.

I'll investigate why ide can't just run tests when everything is on the classpath.

maxandersen avatar Dec 29 '21 11:12 maxandersen

how are you running the tests ? in intellij I can run them just fine, without changing build.gradle.

2021-12-29_14-03-04

maxandersen avatar Dec 29 '21 13:12 maxandersen

e.g. with a click on the line public class testTest { to execute all tests in the file. or with Alt+Enter -> run as in your screenshot

ennishol avatar Dec 29 '21 13:12 ennishol

Weird. Just works for me.

maxandersen avatar Dec 29 '21 21:12 maxandersen

Ah - you probably have "run tests via gradle" enabled.

I always turn that off as it's just too slow.

That must be the difference.

maxandersen avatar Dec 29 '21 21:12 maxandersen

yes, I think these are the default settings in idea. I generated a fresh project (java 17, gradle 7.3.3) with jbang as above, and gradle settings in idea are as below. Do you change the settings every time?

image

ennishol avatar Dec 30 '21 12:12 ennishol

well its a global setting apparently as its been over a year since I changed it as it was just too slow for me :)

maxandersen avatar Dec 30 '21 13:12 maxandersen

I think we should be able to add that default if junit dependencies detected...

maxandersen avatar Dec 30 '21 13:12 maxandersen

and maybe also detect current SDK? I've generated the project with JDK 17 and as you can see in the screenshot above 16 is set

ennishol avatar Dec 30 '21 14:12 ennishol

Yeah. It's been a long standing issue since when I said it to 17 gradle starts failing. Maybe gradle 7 fixed that.

maxandersen avatar Dec 30 '21 14:12 maxandersen