Does Kotlin allow the use of backtick names for test classes?

3k Views Asked by At

Kotlin/JUnit advertise the use of backticked function names for tests. I've discovered that you can also use backticked class names...

But I can't find any reference. Is thing something in the Kotlin specification, or just a "may go away soon" unintended feature?

code example:

    @Nested
    inner class `when the entity path only specifies one location` {
        @BeforeEach
        fun setup() {
            entity = Entity(
                id = "entityid1",
                name = "entity1",
                type = "type",
                team = TeamColor.BLUE,
                currentLocation = Location(0.0, 10.0, 0.0),
                path = EntityPath(
                    startTime = "00:00:00",
                    timeLocation = listOf(
                        TimeLocation("00:00:00", Location(0.0, 10.0, 0.0)),
                    )
                ),
            )
        }

        @Test
        fun `it returns the first (only) location`() {
            val actualLocation = entityLocator.getEntityLocation(entity, calculation);
            val expectedLocation = Location(0.0, 10.0, 0.0)

            assertEquals(actualLocation, expectedLocation);
        }
    }
2

There are 2 best solutions below

0
On

Backticks are part of the general language syntax: anywhere you can write a name (whether it's a function, class, property, variable or whatever), you can put it in backticks and include almost any characters (except for newlines, or backticks themselves).

(The original use case may have been for interoperability with Java methods having the same names as Kotlin keywords, such as System.out; but it's more generally useful, as shown in the tests you quote.)

It's documented in the language grammar itself — in the definition of Identifier — so it's an intended part of the language, and unlikely to go away!

Update:

But although Kotlin is guaranteed to support backticks for all identifiers, does that mean you should choose names that need them? In most situations, it's not recommended.

The two main places where you might see them are:

  • Methods, fields, and packages from the Java standard library or from other Java libraries, that are valid in Java but not in Kotlin — usually because they're reserved words in Kotlin. (For example: System.out, and Mockito's when method.) Backticks are of course necessary for these, as there's no other way to access them from Kotlin.

  • Method names in unit tests, as demonstrated in this question. This is a matter of style. Some people like long method names with embedded spaces and punctuation, that describe what's being tested in plain English; they also like test methods that look unmistakably different from the code being tested. Other people find them ugly, awkward, and hard to read.

    As gotube says, the Kotlin coding conventions specifically allow test method names with spaces. But they don't require it; you're free to follow the usual conventions if you prefer. And they only allow it for method names, not for names of classes, objects, packages, properties, or local variables — so it's best to stick to the usual conventions for all those other identifiers, even when writing tests.

0
On

Kotlin coding convention specifies that backticks are for use in tests only:

Names for test methods

In tests (and only in tests), you can use method names with spaces enclosed in backticks.

This means

class `when the entity path only specifies one location`

is not a valid class name, but

fun `it returns the first (only) location`()

is a valid method name for a test