Skip to content

Commit 8d54757

Browse files
committed
Add option to show simple names
1 parent 36d018c commit 8d54757

File tree

5 files changed

+112
-11
lines changed

5 files changed

+112
-11
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ testlogger {
5959
showExceptions true
6060
slowThreshold 2000
6161
showSummary true
62+
showSimpleNames false
6263
showPassed true
6364
showSkipped true
6465
showFailed true
@@ -164,6 +165,17 @@ testlogger {
164165
}
165166
```
166167

168+
### Show simple names
169+
170+
If you don't like seeing long, fully-qualified class names being used for displaying the test suite names, you can choose to
171+
show only [simple names](https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getSimpleName--) by setting the below flag to true.
172+
173+
```groovy
174+
testlogger {
175+
showSimpleNames true
176+
}
177+
```
178+
167179
### Show standard streams
168180

169181
The display of standard output and error streams alongside the test logs can be controlled using the below configuration.

src/main/groovy/com/adarshr/gradle/testlogger/TestDescriptorWrapper.groovy

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,21 @@ class TestDescriptorWrapper {
2424

2525
@CompileDynamic
2626
String getClassDisplayName() {
27-
escape(testDescriptor.properties.classDisplayName ?: testDescriptor.className)
27+
def className = testDescriptor.className
28+
def classDisplayName = testDescriptor.properties.classDisplayName as String
29+
def useClassDisplayName = classDisplayName && classDisplayName != className && !className.endsWith(classDisplayName)
30+
31+
if (testLoggerExtension.showSimpleNames) {
32+
className = className.substring(className.lastIndexOf('.') + 1)
33+
className = className.substring(className.lastIndexOf('$') + 1)
34+
}
35+
36+
escape(useClassDisplayName ? classDisplayName : className)
2837
}
2938

3039
@CompileDynamic
3140
String getDisplayName() {
32-
escape(testDescriptor.properties.displayName ?: testDescriptor.name)
41+
escape(testDescriptor.properties.displayName ?: testDescriptor.name as String)
3342
}
3443

3544
String getSuiteKey() {

src/main/groovy/com/adarshr/gradle/testlogger/TestLoggerExtension.groovy

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class TestLoggerExtension {
2525
boolean showPassed = true
2626
boolean showSkipped = true
2727
boolean showFailed = true
28+
boolean showSimpleNames = false
2829

2930
private final ConsoleOutput consoleType
3031
private Set<String> configuredProperties = []
@@ -46,6 +47,7 @@ class TestLoggerExtension {
4647
this.showPassed = source.showPassed
4748
this.showSkipped = source.showSkipped
4849
this.showFailed = source.showFailed
50+
this.showSimpleNames = source.showSimpleNames
4951
this.consoleType = source.consoleType
5052
this.configuredProperties = source.configuredProperties
5153
}
@@ -118,6 +120,11 @@ class TestLoggerExtension {
118120
this.configuredProperties << 'showFailed'
119121
}
120122

123+
void setShowSimpleNames(boolean showSimpleNames) {
124+
this.showSimpleNames = showSimpleNames
125+
this.configuredProperties << 'showSimpleNames'
126+
}
127+
121128
TestLoggerExtension undecorate() {
122129
new TestLoggerExtension(this)
123130
}
@@ -155,6 +162,7 @@ class TestLoggerExtension {
155162
override(overrides, 'showPassed', Boolean)
156163
override(overrides, 'showSkipped', Boolean)
157164
override(overrides, 'showFailed', Boolean)
165+
override(overrides, 'showSimpleNames', Boolean)
158166

159167
this
160168
}

src/test-functional/groovy/com/adarshr/gradle/testlogger/functional/TestLoggerPluginSpec.groovy

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class TestLoggerPluginSpec extends AbstractFunctionalSpec {
137137
and:
138138
lines.size() == 10
139139
lines[0] == render('')
140-
lines[1] == render('[erase-ahead,bold]FirstTest[/]')
140+
lines[1] == render('[erase-ahead,bold]com.adarshr.test.FirstTest[/]')
141141
lines[2] == render('')
142142
lines[3] == render('[erase-ahead,bold] Test [bold-off]thisTestShouldBeSkipped()[yellow] SKIPPED[/]')
143143
lines[4] == render('[erase-ahead,bold] Test [bold-off]this test should fail[red] FAILED[red]')
@@ -162,16 +162,16 @@ class TestLoggerPluginSpec extends AbstractFunctionalSpec {
162162
and:
163163
lines.size() == 14
164164
lines[0] == render('')
165-
lines[1] == render('[erase-ahead,bold]NestedTestsetOne[/]')
165+
lines[1] == render('[erase-ahead,bold]com.adarshr.test.NestedTest$NestedTestsetThree[/]')
166166
lines[2] == render('')
167-
lines[3] == render('[erase-ahead,bold] Test [bold-off]secondTestOfNestedTestsetOne()[green] PASSED[/]')
168-
lines[4] == render('[erase-ahead,bold] Test [bold-off]firstTestOfNestedTestsetOne()[green] PASSED[/]')
169-
lines[5] == render('')
170-
lines[6] == render('[erase-ahead,bold]NestedTestsetThree[/]')
171-
lines[7] == render('')
172-
lines[8] == render('[erase-ahead,bold] Test [bold-off]firstTestOfNestedTestsetThree()[green] PASSED[/]')
167+
lines[3] == render('[erase-ahead,bold] Test [bold-off]firstTestOfNestedTestsetThree()[green] PASSED[/]')
168+
lines[4] == render('')
169+
lines[5] == render('[erase-ahead,bold]com.adarshr.test.NestedTest$NestedTestsetTwo[/]')
170+
lines[6] == render('')
171+
lines[7] == render('[erase-ahead,bold] Test [bold-off]secondTestOfNestedTestsetTwo()[green] PASSED[/]')
172+
lines[8] == render('[erase-ahead,bold] Test [bold-off]firstTestOfNestedTestsetTwo()[green] PASSED[/]')
173173
lines[9] == render('')
174-
lines[10] == render('[erase-ahead,bold]NestedTestsetTwo[/]')
174+
lines[10] == render('[erase-ahead,bold]com.adarshr.test.NestedTest$NestedTestsetOne[/]')
175175
lines[11] == render('')
176176
lines[12] == render('[erase-ahead,bold] Test [bold-off]secondTestOfNestedTestsetOne()[green] PASSED[/]')
177177
lines[13] == render('[erase-ahead,bold] Test [bold-off]firstTestOfNestedTestsetOne()[green] PASSED[/]')
@@ -187,6 +187,29 @@ class TestLoggerPluginSpec extends AbstractFunctionalSpec {
187187
)
188188
then:
189189
def lines = getLoggerOutput(result.output).lines
190+
and:
191+
lines.size() == 8
192+
lines[0] == render('')
193+
lines[1] == render('[erase-ahead,bold]com.adarshr.test.DeepNestedTest$NestedTestsetLevelOne[/]')
194+
lines[2] == render('')
195+
lines[3] == render('[erase-ahead,bold] Test [bold-off]nestedTestsetLevelOne()[green] PASSED[/]')
196+
lines[4] == render('')
197+
lines[5] == render('[erase-ahead,bold]Nested test set level two[/]')
198+
lines[6] == render('')
199+
lines[7] == render('[erase-ahead,bold] Test [bold-off]nestedTestsetLevelTwo()[green] PASSED[/]')
200+
and:
201+
result.task(":test").outcome == SUCCESS
202+
}
203+
204+
def "log junit5 jupiter engine deep-nested tests when showSimpleNames is true"() {
205+
when:
206+
def result = run(
207+
'sample-junit5-jupiter-deep-nested-tests',
208+
'testlogger { showSimpleNames true }',
209+
'clean test'
210+
)
211+
then:
212+
def lines = getLoggerOutput(result.output).lines
190213
and:
191214
lines.size() == 8
192215
lines[0] == render('')
@@ -226,6 +249,29 @@ class TestLoggerPluginSpec extends AbstractFunctionalSpec {
226249
result.task(":test").outcome == FAILED
227250
}
228251

252+
def "log spock tests when showSimpleNames is true"() {
253+
when:
254+
def result = run(
255+
'single-spock-test',
256+
'''
257+
testlogger {
258+
theme 'plain'
259+
showSimpleNames true
260+
}
261+
''',
262+
'clean test'
263+
)
264+
def lines = getLoggerOutput(result.output).lines
265+
then:
266+
lines.size() == 4
267+
lines[0] == render('')
268+
lines[1] == render('SingleSpec')
269+
lines[2] == render('')
270+
lines[3] == render(' Test this is a single test PASSED')
271+
and:
272+
result.task(':test').outcome == SUCCESS
273+
}
274+
229275
def "do not print empty suites when filtering tests"() {
230276
when:
231277
def result = run(

src/test/groovy/com/adarshr/gradle/testlogger/TestDescriptorWrapperSpec.groovy

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.adarshr.gradle.testlogger
22

33
import org.gradle.api.tasks.testing.TestDescriptor
44
import spock.lang.Specification
5+
import spock.lang.Unroll
56

67

78
class TestDescriptorWrapperSpec extends Specification {
@@ -25,6 +26,31 @@ class TestDescriptorWrapperSpec extends Specification {
2526
wrapper.classDisplayName == 'class display name \\[escaped\\]'
2627
}
2728

29+
@Unroll
30+
def "classDisplayName is #expected when classDisplayName property = #classDisplayName, className = #className and showSimpleNames = #showSimpleNames"() {
31+
given:
32+
testDescriptorMock.properties >> [classDisplayName: classDisplayName]
33+
testDescriptorMock.className >> className
34+
and:
35+
testLoggerExtensionMock.showSimpleNames >> showSimpleNames
36+
expect:
37+
wrapper.classDisplayName == expected
38+
where:
39+
classDisplayName | className | showSimpleNames | expected
40+
'Test' | 'com.adarshr.Test' | false | 'com.adarshr.Test'
41+
'A test' | 'com.adarshr.Test' | false | 'A test'
42+
'' | 'com.adarshr.Test' | false | 'com.adarshr.Test'
43+
null | 'com.adarshr.Test' | false | 'com.adarshr.Test'
44+
'Two' | 'com.adarshr.Test$One$Two' | false | 'com.adarshr.Test$One$Two'
45+
'com.adarshr.Test' | 'com.adarshr.Test' | false | 'com.adarshr.Test'
46+
'Test' | 'com.adarshr.Test' | true | 'Test'
47+
'A test' | 'com.adarshr.Test' | true | 'A test'
48+
'' | 'com.adarshr.Test' | true | 'Test'
49+
null | 'com.adarshr.Test' | true | 'Test'
50+
'Two' | 'com.adarshr.Test$One$Two' | true | 'Two'
51+
'com.adarshr.Test' | 'com.adarshr.Test' | true | 'Test'
52+
}
53+
2854
def "classDisplayName falls back to className if classDisplayName property is missing"() {
2955
given:
3056
testDescriptorMock.properties >> [:]

0 commit comments

Comments
 (0)