I’m looking for a way to generate "test data as so...
# announcements
t
I’m looking for a way to generate "test data as source code" based on real server http responses. Basically I want to serialise objects from memory as source code. eg: I have a
data class MyObject(val value: Int)
and 10 objects in memory I want to generate a
data.kt
file like :
Copy code
val obj1 = MyObject(value = 1)
val obj2 = MyObject(value = 12)
val obj3 = MyObject(value = 3)
etc
Context: Jetpack Compose Previews cannot access the file system, so you'd need test data as Kotlin source code, you cannot use a JSON file and parse it, as you would do in a normal junit test. This would be useful as a JVM CLI companion tool when you want complex test data based on a server response that would be tedious to type by hand.
b
You mean something like this, but within your JVM runtime?
You could convert your object into memory representation (via gson, jackson or kotlinx.serialization) and then traverse the tree and write the class to file with kotlinPoet
Or you can use reflection to traverse object's properties recursivelly.
t
Not really, I already have defined my data classes. I just need to generate data class instantiation on file.
b
Are you talking about some kind of annotation processor?
t
From
data class MyObject(val value: Int)
and 10 MyObject in memory I want to generate a .kt file like:
Copy code
val obj1 = MyObject(1)
val obj2 = MyObject(21)
etc
Basically I need test data in the form of actual source code, not a JSON file or anything like that
b
How will you then use the generated data?
I can't see your use case
Easiest hack though would be to write data to jsons and then deserialize them all dynamically in tests
For IDE Ui previews, you can't access the file system
This is something the IDE would actually run, so that's why I can't go the normal path of using JSON and just deserialise it in tests.
b
Still not clear on the whole expected flow, but I guess I'm out of my depth.
j
Gradle task + Kotlin serialization + KotlinPoet to generate fake KT files maybe?
And then
./gradlew generateFakes --file=/path/something.json
t
that's what I'm looking at right now, never used KotlinPoet before
j
Maybe you can run that task on sync and instead of passing every file just use filewalktree and search for al json in a specific folder
n
@Big Chungus I think I get it : Assume you have an Android App that share test files with others target (iOS, web app), like a testusers.json (a json file containing a list of users to display on a screen). @Tudor Luca wants to be able to generate a Preview from this file, unfortunatelly, when you try to read/write a file from a @Preview : WARN - er.scene.LayoutlibSceneManager - Write access not allowed during rendering so I think there one thing that should do first, it's make an issue on the tracker to ask if this blockage is something that could be toggled off (from an ide/gradle settings for example). Then you can either use kotlin poet + gradle as Javier said, but it will definitelly take some times OR you don't use the @Preview for that 🙂 keep it to just make and preview your layouts for simple case, and on your test files, you use the Compose test https://developer.android.com/jetpack/compose/testing so you won't have to check the previews to verify everything works, and if you still need to have a "picture" of the screen, I'm pretty sure it can be done with an emulator (looking for libs that allows to take screenshot during tests)
👍 1
t
@nitrog42 yup, you're right, it's gonna take time with kotlinpoet and not really worth it. honestly, I was hoping there's already a gradle plugin for this & I didn't know about haha 🙂 .
j
I am not sure about it is not worth
Autogenerating fakes is a good feature for testing, and if it allows you use in preview it should be great
it is easier manually create fakes, but you will get the same result if you automatize it, overall if you generates them from memory models, because you can just pass your network json, map to domain and generating a fake of this domain object
n
but doing so means you'll have to pass in review every screens by your eyes, usually you want your automated tests to do that for you
b
Looks like something that snapshot testing could take care of
j
@nitrog42 No, I am talking about generating the domain data classes and use for two purposes, one purposes is for fake data for UI tests. Another purpose is for Compose Preview.
At the end in your UI tests you uses fake data, so if it is autogenerated instead of creating it manually, that is time you earn
n
right, but in the ui tests, you can generate the data in your tests (you can parse a json)
j
ah, yeah, you are right
n
while in a gradle plugin... I mean you definitelly can, but honestly it will take some times
for example you can use Kotlin Poet to generate the file as you said, but for kotlin poet to correctly write the constructor method of the data class you either has to "hard code" every class you can encounter in your json, or do that in some other way (that I don't know)