Unit Testing with jbang #424
-
|
I've been pondering how to go about writing unit tests for jbang scripts/apps. There are two parts afaics, one on how the tests look like the other how to run them. The first part on how they look like I think is fairly straight forward now we have i.e. if you have script looking like: ///usr/bin/env jbang "$0" "$@" ; exit $?
class helloworld {
String greeting(String who) {
return "Hello " + who;
}
public static void main(String[] args) {
helloworld hw = new helloworld();
if(args.length==0) {
System.out.println(hw.greeting("World"));
} else {
System.out.print(hw.greeting(args[0]));
}
}
}Then the most basic test of that would be: //DEPS org.junit.jupiter:junit-jupiter-engine:5.2.0
//DEPS org.hamcrest:hamcrest-library:2.1
//SOURCES helloworld.java
import java.io.PrintWriter;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
public class TestLauncher {
@Test
void testGreeting() {
helloworld hw = new helloworld();
assertThat(hw.greeting("me"), equalTo("Hello me"));
assertThat(hw.greeting(null), equalTo("Hello"));
}
}This setup actually works (kinda) today and you can use idea with I'm wondering how/if we should add some native test support to i.e.
where then junit 5 test runner would find all identifiable tests and run them. Could also just tell users to run the above command but a) its quite long and b) it won't work on windows. Ideas/thoughts on testing with jbang? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 13 replies
-
|
My thoughts: I don't use a CI server to build my scripts continuously. So when to run the tests? What about linking the test classes in the script file itself? When you run the script the tests are executed first and the script is only executed if the tests pass. You need to add some convenience stuff like caching the results if the class under test did not change or skipping tests explicitly, but this is my first take on this. |
Beta Was this translation helpful? Give feedback.
-
|
Would also be nice to have a template with unit tests for a JBang script w/ multiple source files as an example. |
Beta Was this translation helpful? Give feedback.
-
|
Just wondering if there's a current recommendation / best practice regarding this. |
Beta Was this translation helpful? Give feedback.
It's a typo, it should be
-t junit@jbangdev