Skip to content

Generate DisplayName annotation only for JUnit 5 #576 #624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1599,7 +1599,23 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
} else {
setOf(annotation(testFramework.testAnnotationId))
}
displayName?.let { testFrameworkManager.addDisplayName(it) }

/* Add a short test's description depending on the test framework type:
DisplayName annotation in case of JUni5, and description argument to Test annotation in case of TestNG.
*/
if (displayName != null) {
when (testFramework) {
is Junit5 -> {
displayName.let { testFrameworkManager.addDisplayName(it) }
}
is TestNg -> {
testFrameworkManager.addTestDescription(displayName)
}
else -> {
// nothing
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to create a ticket to add a custom Java tag for display name as @DisplayName or @summary for JUnit 4

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's weird that we just forget the generated display name :( for JUnit4 but generates it.

No logging in this file at all, it's sad too

}
}
}

val result = currentExecution!!.result
if (result is UtTimeoutException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import org.utbot.framework.codegen.model.constructor.context.CgContextOwner
import org.utbot.framework.codegen.model.constructor.util.CgComponents
import org.utbot.framework.codegen.model.constructor.util.classCgClassId
import org.utbot.framework.codegen.model.constructor.util.importIfNeeded
import org.utbot.framework.codegen.model.tree.CgCommentedAnnotation
import org.utbot.framework.codegen.model.tree.CgEnumConstantAccess
import org.utbot.framework.codegen.model.tree.CgExpression
import org.utbot.framework.codegen.model.tree.CgGetJavaClass
Expand Down Expand Up @@ -174,13 +173,39 @@ internal abstract class TestFrameworkManager(val context: CgContext)
}
}

/**
* Supplements TestNG @Test annotation with a description.
* It looks like @Test(description="...")
*
* Should be used only with TestNG.
* @see <a href="https://github.com/UnitTestBot/UTBotJava/issues/576">issue-576 on GitHub</a>
*/
open fun addTestDescription(description: String?) {
if (description == null) return
val testAnnotation =
collectedMethodAnnotations.singleOrNull { it.classId == testFramework.testAnnotationId }

val descriptionArgument = CgNamedAnnotationArgument("description", stringLiteral(description))
if (testAnnotation is CgMultipleArgsAnnotation) {
testAnnotation.arguments += descriptionArgument
} else {
collectedMethodAnnotations += CgMultipleArgsAnnotation(
testFramework.testAnnotationId,
mutableListOf(descriptionArgument)
)
}
}

abstract fun disableTestMethod(reason: String)

// We add a commented JUnit5 DisplayName annotation here by default,
// because other test frameworks do not support such feature.
/**
* Adds @DisplayName annotation.
*
* Should be used only with JUnit 5.
* @see <a href="https://github.com/UnitTestBot/UTBotJava/issues/576">issue-576 on GitHub</a>
*/
open fun addDisplayName(name: String) {
val displayName = CgSingleArgAnnotation(Junit5.displayNameClassId, stringLiteral(name))
collectedMethodAnnotations += CgCommentedAnnotation(displayName)
collectedMethodAnnotations += CgSingleArgAnnotation(Junit5.displayNameClassId, stringLiteral(name))
}

protected fun ClassId.toExceptionClass(): CgExpression =
Expand Down