Discussion:
junit listener from build.gradle
Tomek Kaczanowski
2013-02-21 21:20:31 UTC
Permalink
Hello,

I can not find way to instruct gradle to use JUnit listeners
(extending JUnit TestWatcher). What should I put in the test {}
section of my build.gradle so it uses com.mypackage.MyListener while
running JUnit tests?

I know Gradle has some test listeners of it own, but I would like to
use what JUnit offers.
--
Regards / Pozdrawiam
Tomek Kaczanowski
http://practicalunittesting.com
Adam Murdoch
2013-02-21 22:22:18 UTC
Permalink
Post by Tomek Kaczanowski
Hello,
I can not find way to instruct gradle to use JUnit listeners
(extending JUnit TestWatcher). What should I put in the test {}
section of my build.gradle so it uses com.mypackage.MyListener while
running JUnit tests?
TestWatchers are just JUnit rules, so you use them the same way as any rule in JUnit: Attach the watcher to a test class as a field with an @Rule or @ClassRule annotation.


--
Adam Murdoch
Gradle Co-founder
http://www.gradle.org
VP of Engineering, Gradleware Inc. - Gradle Training, Support, Consulting
http://www.gradleware.com
Tomek Kaczanowski
2013-02-21 23:23:57 UTC
Permalink
Hello Adam,
Post by Tomek Kaczanowski
I can not find way to instruct gradle to use JUnit listeners
(extending JUnit TestWatcher). What should I put in the test {}
section of my build.gradle so it uses com.mypackage.MyListener while
running JUnit tests?
TestWatchers are just JUnit rules, so you use them the same way as any rule
@ClassRule annotation.
Thanks, but that is not it. :) I want to specify my listener globally,
let us say for all tests. Gradle used to allow it - at least for
TestNG (http://docs.codehaus.org/display/GRADLE/Cookbook#Cookbook-addlisteners).
I would like to do the same for JUnit. Is it supported?
--
Regards / Pozdrawiam
Tomek Kaczanowski
http://practicalunittesting.com
Adam Murdoch
2013-02-21 23:37:31 UTC
Permalink
Post by Tomek Kaczanowski
Hello Adam,
Post by Tomek Kaczanowski
I can not find way to instruct gradle to use JUnit listeners
(extending JUnit TestWatcher). What should I put in the test {}
section of my build.gradle so it uses com.mypackage.MyListener while
running JUnit tests?
TestWatchers are just JUnit rules, so you use them the same way as any rule
@ClassRule annotation.
Thanks, but that is not it. :) I want to specify my listener globally,
let us say for all tests. Gradle used to allow it - at least for
TestNG (http://docs.codehaus.org/display/GRADLE/Cookbook#Cookbook-addlisteners).
I would like to do the same for JUnit. Is it supported?
No. TestNG has explicit support for adding listeners from the launcher, JUnit does not.

We could probably add something, but it wouldn't be a standard thing, it would be a Gradle specific extension to JUnit. I don't think JUnit gives us the hooks to mix a rule into every test. We'd have to invent something, perhaps generating a Suite class dynamically that wraps all the test classes or something like that.


--
Adam Murdoch
Gradle Co-founder
http://www.gradle.org
VP of Engineering, Gradleware Inc. - Gradle Training, Support, Consulting
http://www.gradleware.com
Tomek Kaczanowski
2013-02-22 07:08:25 UTC
Permalink
Hm, are you sure? Maven Surefire supports this since few versions
(http://maven.apache.org/surefire/maven-surefire-plugin/examples/junit.html
see "listeners" section).
--
Regards / Pozdrawiam
Tomek Kaczanowski
http://practicalunittesting.com
Post by Tomek Kaczanowski
Hello Adam,
I can not find way to instruct gradle to use JUnit listeners
(extending JUnit TestWatcher). What should I put in the test {}
section of my build.gradle so it uses com.mypackage.MyListener while
running JUnit tests?
TestWatchers are just JUnit rules, so you use them the same way as any rule
@ClassRule annotation.
Thanks, but that is not it. :) I want to specify my listener globally,
let us say for all tests. Gradle used to allow it - at least for
TestNG
(http://docs.codehaus.org/display/GRADLE/Cookbook#Cookbook-addlisteners).
I would like to do the same for JUnit. Is it supported?
No. TestNG has explicit support for adding listeners from the launcher, JUnit does not.
We could probably add something, but it wouldn't be a standard thing, it
would be a Gradle specific extension to JUnit. I don't think JUnit gives us
the hooks to mix a rule into every test. We'd have to invent something,
perhaps generating a Suite class dynamically that wraps all the test classes
or something like that.
--
Adam Murdoch
Gradle Co-founder
http://www.gradle.org
VP of Engineering, Gradleware Inc. - Gradle Training, Support, Consulting
http://www.gradleware.com
Loading...