-
Notifications
You must be signed in to change notification settings - Fork 682
feat: Add KoinPropTestListener for Property-Based Testing #4899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sksamuel
merged 4 commits into
kotest:master
from
belljun3395:feat/add-koin-prop-test-listener
Jun 26, 2025
Merged
feat: Add KoinPropTestListener for Property-Based Testing #4899
sksamuel
merged 4 commits into
kotest:master
from
belljun3395:feat/add-koin-prop-test-listener
Jun 26, 2025
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Signed-off-by: belljun3395 <195850@jnu.ac.kr>
859c8d7
to
19ca2fc
Compare
sksamuel
approved these changes
Jun 25, 2025
Thanks for the Pr |
@sksamuel I updated the branch due to a failure in the GitHub Actions workflow as shown in the screenshot. Is there anything else I need to do? |
Nope. Kicked it off again |
Kantis
pushed a commit
to JesusMcCloud/kotest
that referenced
this pull request
Jul 12, 2025
<!-- If this PR updates documentation, please update all relevant versions of the docs, see: https://github.com/kotest/kotest/tree/master/documentation/versioned_docs The documentation at https://github.com/kotest/kotest/tree/master/documentation/docs is the documentation for the next minor or major version _TO BE RELEASED_ --> **Overview** This PR resolves an issue that occurs when using the `Koin` dependency injection framework in a Property-Based Testing environment. This is based on the discussion in GitHub issue kotest#2171. **The Problem** When using property-based testing functions like Kotest's `checkAll` with `KoinListener`, the Koin context is shared across all iterations of the test. This is because `KoinListener` only starts and stops the Koin context at the `TestCase` level, and all iterations within a `checkAll` block are considered part of a single `TestCase`. As a result, an object declared in a Koin module during the first iteration (e.g., `declareMock { Repository(arg) }`) is reused in subsequent iterations. For example, `get<Service>()` would always receive `Repository(true)` in the second run. This violates the fundamental principle of test isolation, where each test run should be independent, thereby undermining the reliability of property-based tests. **The Solution** To address this, this PR introduces `KoinPropTestListener`, a new listener that implements the `PropTestListener` interface. The `KoinPropTestListener` ensures proper test isolation by starting the Koin context with `startKoin` immediately before **each iteration** of a property test begins, and shutting it down with `stopKoin` immediately after the iteration completes. **Before:** With `KoinListener`, isolation between property test iterations was not guaranteed. ```kotlin // Using KoinListener with property-based tests... checkAll<Boolean> { arg -> // true, false declareMock { Repository(arg) } get<Service>() // The second iteration would still get Repository(true) } ``` **After:** `KoinPropTestListener` creates a fresh Koin context for each iteration, ensuring complete test isolation. ```kotlin checkAll<Boolean>( PropTestConfig(listeners = listOf(KoinPropTestListener(myModule))) ) { a -> val currentService: MyService = get { parametersOf(a) } currentService.a shouldBe a // A new Service instance is injected every time } ``` This change allows developers to reliably use `Koin` with property-based tests, ensuring that dependencies for each test iteration are managed in a clean and isolated manner. **Related Issue** * Fixes kotest#2171 Signed-off-by: belljun3395 <195850@jnu.ac.kr> Co-authored-by: Sam <sam@sksamuel.com>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This PR resolves an issue that occurs when using the
Koin
dependency injection framework in a Property-Based Testing environment. This is based on the discussion in GitHub issue #2171.The Problem
When using property-based testing functions like Kotest's
checkAll
withKoinListener
, the Koin context is shared across all iterations of the test. This is becauseKoinListener
only starts and stops the Koin context at theTestCase
level, and all iterations within acheckAll
block are considered part of a singleTestCase
.As a result, an object declared in a Koin module during the first iteration (e.g.,
declareMock { Repository(arg) }
) is reused in subsequent iterations. For example,get<Service>()
would always receiveRepository(true)
in the second run. This violates the fundamental principle of test isolation, where each test run should be independent, thereby undermining the reliability of property-based tests.The Solution
To address this, this PR introduces
KoinPropTestListener
, a new listener that implements thePropTestListener
interface.The
KoinPropTestListener
ensures proper test isolation by starting the Koin context withstartKoin
immediately before each iteration of a property test begins, and shutting it down withstopKoin
immediately after the iteration completes.Before:
With
KoinListener
, isolation between property test iterations was not guaranteed.After:
KoinPropTestListener
creates a fresh Koin context for each iteration, ensuring complete test isolation.This change allows developers to reliably use
Koin
with property-based tests, ensuring that dependencies for each test iteration are managed in a clean and isolated manner.Related Issue