|
1 | | -# JUnit Platform ReportEntry data capture in Gradle |
| 1 | +# Demos |
2 | 2 |
|
3 | 3 | ## Milestone 1 Demo |
4 | 4 |
|
@@ -26,7 +26,7 @@ is displayed at the class level in the metadata tab in the test report here: |
26 | 26 |
|
27 | 27 | <img width="652" height="395" alt="image" src="https://github.com/user-attachments/assets/ca234200-8e70-425d-b55b-e9bfea734066" /> |
28 | 28 |
|
29 | | -Data published during setup/cleanup: |
| 29 | +Data published during setup/cleanup: |
30 | 30 |
|
31 | 31 | ```java |
32 | 32 |
|
@@ -85,4 +85,114 @@ Received test metadata event: metadata |
85 | 85 | Received test metadata event: metadata |
86 | 86 | afterEach = value4 |
87 | 87 | BUILD SUCCESSFUL in 1s |
88 | | -``` |
| 88 | +``` |
| 89 | + |
| 90 | +## Milestone 2 & 3 Demo |
| 91 | + |
| 92 | +This project demonstrates capturing `ReportEntry` and `FileEntry` data published by JUnit Platform during test execution in Gradle. |
| 93 | + |
| 94 | +This project contains a demonstration project in `/demo-m2-3` which is a JVM library project created using Gradle `init`. |
| 95 | + |
| 96 | +Run the demo project using `./gradlew test` and the tests should pass. |
| 97 | + |
| 98 | +The following files are only created after running `./gradlew test`: |
| 99 | + |
| 100 | +Open the [test report](demo-m2-3/build/reports/tests/test/index.html), you should be able to navigate the test reports to see `FileEntry` data published by the tests appear as "attachments" and `ReportEntry` data published as "data". |
| 101 | + |
| 102 | +The [JUnit XML report](demo-m2-3/build/test-results/test/TEST-org.example.LibraryTest.xml) also includes the same data: |
| 103 | +- `ReportEntry` are captured as `<properties/>` |
| 104 | +- `FileEntry` are captured as `[[ATTACHMENT|/path/to/file]]` based on [conventions used by Jenkins, Azure pipelines and GitLab](https://kohsuke.org/2012/03/13/attaching-files-to-junit-tests/). |
| 105 | + |
| 106 | +NOTE: Make sure you open the HTML report in the browser directly as a local file and not through the IDE's built-in webserver. |
| 107 | + |
| 108 | +### Details |
| 109 | + |
| 110 | +The test class is located at [`LibraryTest.java`](demo-m2-3/src/test/java/org/example/LibraryTest.java). |
| 111 | + |
| 112 | +Data published during test construction: |
| 113 | +```java |
| 114 | +LibraryTest(TestReporter testReporter) { |
| 115 | + testReporter.publishEntry("constructor", "value1"); |
| 116 | + // Publish a file at the class level |
| 117 | + testReporter.publishFile("constructor.json", MediaType.APPLICATION_JSON, path -> { |
| 118 | + Files.writeString(path, "{ constructor: [] }"); |
| 119 | + }); |
| 120 | +} |
| 121 | +``` |
| 122 | +is displayed at the class level in the data and attachments tabs. |
| 123 | + |
| 124 | +Data published during setup/cleanup: |
| 125 | + |
| 126 | +```java |
| 127 | +@BeforeEach |
| 128 | +public void beforeEach(TestReporter testReporter) { |
| 129 | + testReporter.publishEntry("beforeEach", "value2"); |
| 130 | + // Publish a text file before the test |
| 131 | + testReporter.publishFile("beforeEach.txt", MediaType.TEXT_PLAIN, path -> { |
| 132 | + Files.writeString(path, "Hello world"); |
| 133 | + }); |
| 134 | +} |
| 135 | + |
| 136 | +@AfterEach |
| 137 | +public void afterEach(TestReporter testReporter) { |
| 138 | + testReporter.publishEntry("afterEach", "value4"); |
| 139 | + // Publish a (fake) video file after the test |
| 140 | + testReporter.publishFile("afterEach.mp4", MediaType.create("video", "mp4"), path -> { |
| 141 | + Files.writeString(path, "This is just a text file that pretends to be a video."); |
| 142 | + }); |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +and during test execution: |
| 147 | + |
| 148 | +```java |
| 149 | +@Test |
| 150 | +void someLibraryMethodReturnsTrue(TestReporter testReporter) { |
| 151 | + testReporter.publishEntry("test", "value3"); |
| 152 | + // Publish an image during the test |
| 153 | + testReporter.publishFile("test.svg", MediaType.create("image", "svg+xml"), path -> { |
| 154 | + Files.writeString(path, "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 800 600\"><rect width=\"800\" height=\"600\" fill=\"#F5F5F0\"/><rect width=\"350\" height=\"600\" fill=\"#D4292E\"/><rect x=\"350\" width=\"450\" height=\"250\" fill=\"#1356A3\"/><rect x=\"350\" y=\"250\" width=\"200\" height=\"350\" fill=\"#F7D838\"/><rect x=\"550\" y=\"250\" width=\"250\" height=\"350\" fill=\"#F5F5F0\"/><line x1=\"350\" y1=\"0\" x2=\"350\" y2=\"600\" stroke=\"#1A1A1A\" stroke-width=\"12\"/><line x1=\"350\" y1=\"250\" x2=\"800\" y2=\"250\" stroke=\"#1A1A1A\" stroke-width=\"12\"/><line x1=\"550\" y1=\"250\" x2=\"550\" y2=\"600\" stroke=\"#1A1A1A\" stroke-width=\"12\"/><rect width=\"800\" height=\"600\" fill=\"none\" stroke=\"#1A1A1A\" stroke-width=\"14\"/></svg>"); |
| 155 | + }); |
| 156 | + |
| 157 | + Library classUnderTest = new Library(); |
| 158 | + assertTrue(classUnderTest.someLibraryMethod(), "someLibraryMethod should return 'true'"); |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +is displayed at the method level in the data and attachments tabs. |
| 163 | + |
| 164 | +The report renders image and videos in the report and all other files are linked to. The order of attachments is based on the order they are published. |
| 165 | + |
| 166 | +## Milestone 2 & 3 Tooling API Demo |
| 167 | + |
| 168 | +This project demonstrates capturing `ReportEntry` and `FileEntry` data published by JUnit Platform during test execution in Gradle when running via the Tooling API. |
| 169 | + |
| 170 | +This project contains a demonstration project in `/demo-m2-3-tapi` which is a JVM application project that runs the `/demo-m2-3` build using the Tooling API, and listens for `TestMetadataEvent`s while doing so. |
| 171 | + |
| 172 | +Run the demo project using `./gradlew :demon-m2-3-tapi:run` and the build should succeed. |
| 173 | +You will see the data and attachments published by the test printed to the console using metadata events: |
| 174 | + |
| 175 | +```text |
| 176 | +> Task :demo-m2-3-tapi:run |
| 177 | +> Task :demo-m2-3:processTestResources NO-SOURCE |
| 178 | +> Task :demo-m2-3:processResources NO-SOURCE |
| 179 | +> Task :demo-m2-3:compileJava UP-TO-DATE |
| 180 | +> Task :demo-m2-3:classes UP-TO-DATE |
| 181 | +> Task :demo-m2-3:compileTestJava UP-TO-DATE |
| 182 | +> Task :demo-m2-3:testClasses UP-TO-DATE |
| 183 | +Received test metadata event: metadata |
| 184 | +File attachment (application/json): |
| 185 | + /Users/sterling/gits/sample-rbt-engine/demo-m2-3/build/junit-jupiter/org.example.LibraryTest/someLibraryMethodReturnsTrue(org.junit.jupiter.api.TestReporter)/constructor.json |
| 186 | +Received test metadata event: metadata |
| 187 | +File attachment (text/plain): |
| 188 | + /Users/sterling/gits/sample-rbt-engine/demo-m2-3/build/junit-jupiter/org.example.LibraryTest/someLibraryMethodReturnsTrue(org.junit.jupiter.api.TestReporter)/beforeEach.txt |
| 189 | +Received test metadata event: metadata |
| 190 | +File attachment (image/svg+xml): |
| 191 | + /Users/sterling/gits/sample-rbt-engine/demo-m2-3/build/junit-jupiter/org.example.LibraryTest/someLibraryMethodReturnsTrue(org.junit.jupiter.api.TestReporter)/test.svg |
| 192 | +Received test metadata event: metadata |
| 193 | +File attachment (video/mp4): |
| 194 | + /Users/sterling/gits/sample-rbt-engine/demo-m2-3/build/junit-jupiter/org.example.LibraryTest/someLibraryMethodReturnsTrue(org.junit.jupiter.api.TestReporter)/afterEach.mp4 |
| 195 | +> Task :demo-m2-3:test |
| 196 | +
|
| 197 | +BUILD SUCCESSFUL in 2s |
| 198 | +``` |
0 commit comments