https://kotlinlang.org logo
#ktor
Title
# ktor
f

Filip Lastic

04/04/2022, 7:43 AM
Hello. I have question about integration-testing of ktor server. We have bigger ktor application which startup takes some time. For now, we use
withTestApplication {}
in our integration tests, but this causes that server has to start with each test. We started to have some performance issues, where test is executed in milliseconds, but server is starting for long time.. Do you have some experiences with starting server programatically, for example from
gradle
before running all tests? I found this solution https://stackoverflow.com/a/61246720 But not sure if it is correct approach.
a

Aleksei Tirman [JB]

04/04/2022, 9:10 AM
Do you want to use a real or fake server for testing?
f

Filip Lastic

04/04/2022, 10:14 AM
I was thinking about real server, for example using Docker container for it, but we have some test for different configurations and this would be issue with real server. So I am not sure which would be better for our use-case..
a

Aleksei Tirman [JB]

04/04/2022, 10:19 AM
You can reduce a number of server startups by creating and destroying a fake server “before” and “after” each test class.
So you’d have a test class for each configuration.
f

Filip Lastic

04/04/2022, 11:05 AM
Okay it makes sense. Maybe I can somehow create fake server also for more than single test class. There are only few use-cases which use different configurations. Do you think it would be possible?
a

Aleksei Tirman [JB]

04/04/2022, 11:08 AM
The question is how you would share a server instance among different classes?
f

Filip Lastic

04/04/2022, 11:08 AM
Maybe create something like
BaseTest
and inherit related tests from it.
a

Aleksei Tirman [JB]

04/04/2022, 11:09 AM
That's how you can start a server for each class.
I mean to have a separate server instance for each test class.
f

Filip Lastic

04/04/2022, 11:11 AM
I understand. Then maybe we can use server as global variable but probably that's not correct way
a

Aleksei Tirman [JB]

04/04/2022, 11:14 AM
The root problem is that you don't control how test classes instantiated. Maybe, that can be solved on the test framework's side.
f

Filip Lastic

04/05/2022, 7:09 AM
Maybe this solution could help https://stackoverflow.com/a/51556718
a

Aleksei Tirman [JB]

04/05/2022, 7:53 AM
Yes, this can work if you can get an instance of the server in extended test classes.
Also, you can define top-level variable in a file and refer to it in each test.
7 Views