https://kotlinlang.org logo
#getting-started
Title
# getting-started
r

Ray Rahke

03/13/2024, 5:24 AM
alternatively, is it possible for me to handwrite some test code in the same file as the source file, and mark it to be excluded in final compiled product?
🧵 2
j

Joffrey

03/13/2024, 7:01 AM
Why do you want to do this? You're making lots of things much harder than they need to be by not following the conventions of the ecosystem
👍 1
r

Ray Rahke

03/14/2024, 9:49 AM
@Joffrey For this one, I was just thinking that it would make for a better dev experience in a large codebase, if unit tests are right there next to whatever code you're looking at. Similar to why people hate cpp header files vs implementation files being in mirrored directory trees
The times when you actually want to look at tests, are typically when you are looking at source code. So the bouncing to-and-from process should be a click away
if tests and source code are two sibling hierarchies mirroring each other, then if I am looking at source code in a/b/c/d/e, and see some function I want to look at a test for, I now need to go click and open 4 nested sub folders in the test/ folder.
just wanted to try this out. Have never done it personally. I know other languages like rust support it
j

Joffrey

03/14/2024, 9:53 AM
I think you will not be able to put test code in the same files as the main code, but you can definitely avoid having a mirror directory hierarchy. For instance you could have a file named
Thing.kt
and a sibling named
Thing@test.kt
, then you can use this naming convention to decide what files to pass to the compiler in different invocations
r

Ray Rahke

03/14/2024, 9:54 AM
is there a gradle thing I can look up to get started putting that together?
something similar in the docs
/ some keywords i can google
i did try earlier but didnt find much
j

Joffrey

03/14/2024, 9:56 AM
I actually don't know. If there is something, it's probably somewhere in the API of the source sets. Instead of setting whole directories, you might be able to configure file collections explicitly using glob patterns. But that's just a guess, this API might very well not exist at all
My previous message was more of the theoretical possibility. The Kotlin compiler allows this, but whether Gradle allows you to configure your project in such a way is a different question unfortunately
This is definitely useful feedback for Amper, by the way
a

andrey.dernov

03/14/2024, 10:28 AM
By the way IntelliJ IDEA I guess Android Studio as well) has Navigate → Test action. That might be helpful for your case.