3

In node.js and jest, the best practice is to put test file side by side with source files, like

|- /main
|  |- index.js
|  |- index.test.js

and then just run jest. How can we do the same in Kotlin and gradle? I 'd like to have hello.kt and something like hello.test.kt, and somehow run kotlin command line tool to run test.

I have read kotlin-example but they have files in main and test folders.

P/S: If anyone knows how to also do the same in Swift, that would be great.

2
  • Could you add your build.gradle. Commented Dec 16, 2018 at 11:02
  • See this question: stackoverflow.com/questions/15298886/… Commented Dec 16, 2018 at 11:22

1 Answer 1

1

As a best practice when working with Kotlin and Gradle you should separate your productive code from test code in different directories and the class to be tested and the test should be in the same package (to have access to package visible restricted methods during tests and easily understand the package layout). You can find this guideline in the Gradle user guide. And the same applies to Maven:

So your layout should follow:

src
├── main
│   └── kotlin
│       └── packagename
|           └── Hello.kt
└── test
    └── kotlin
        └── packagename
            └── HelloTest.kt

Following these conventions makes it easy to execute the tests with $gradle test and every expirienced Java/Kotlin/Gradle/Maven developer will easily understand your project organization.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.