{"id":23055369,"url":"https://github.com/xemantic/xemantic-kotlin-test","last_synced_at":"2025-09-05T18:51:30.935Z","repository":{"id":267328122,"uuid":"900798183","full_name":"xemantic/xemantic-kotlin-test","owner":"xemantic","description":"The power-assert compatible assertions DSL and some other testing goodies - a Kotlin multiplatform testing library.","archived":false,"fork":false,"pushed_at":"2025-07-29T10:49:42.000Z","size":213,"stargazers_count":8,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-29T12:53:42.992Z","etag":null,"topics":["assert","assertion-framework","assertion-library","assertions","domain-specific-language","dsl","kotlin","kotlin-library","kotlin-multiplatform","multiplatform","power-assert","testing","testing-framework","testing-library","testing-tools"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xemantic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"xemantic"}},"created_at":"2024-12-09T13:46:55.000Z","updated_at":"2025-07-27T13:40:48.000Z","dependencies_parsed_at":"2024-12-09T18:37:01.916Z","dependency_job_id":"f077db0e-07dd-4dea-8bff-621f51ee2144","html_url":"https://github.com/xemantic/xemantic-kotlin-test","commit_stats":null,"previous_names":["xemantic/xemantic-kotlin-test"],"tags_count":47,"template":false,"template_full_name":null,"purl":"pkg:github/xemantic/xemantic-kotlin-test","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xemantic%2Fxemantic-kotlin-test","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xemantic%2Fxemantic-kotlin-test/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xemantic%2Fxemantic-kotlin-test/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xemantic%2Fxemantic-kotlin-test/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xemantic","download_url":"https://codeload.github.com/xemantic/xemantic-kotlin-test/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xemantic%2Fxemantic-kotlin-test/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273804760,"owners_count":25171557,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-05T02:00:09.113Z","response_time":402,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["assert","assertion-framework","assertion-library","assertions","domain-specific-language","dsl","kotlin","kotlin-library","kotlin-multiplatform","multiplatform","power-assert","testing","testing-framework","testing-library","testing-tools"],"created_at":"2024-12-16T01:11:49.181Z","updated_at":"2025-09-05T18:51:30.925Z","avatar_url":"https://github.com/xemantic.png","language":"Kotlin","funding_links":["https://github.com/sponsors/xemantic"],"categories":["测试"],"sub_categories":["语音合成"],"readme":"# xemantic-kotlin-test\nThe power-assert compatible assertions DSL and some other testing goodies - a Kotlin multiplatform testing library.\n\n[\u003cimg alt=\"Maven Central Version\" src=\"https://img.shields.io/maven-central/v/com.xemantic.kotlin/xemantic-kotlin-test\"\u003e](https://central.sonatype.com/artifact/com.xemantic.kotlin/xemantic-kotlin-test)\n[\u003cimg alt=\"GitHub Release Date\" src=\"https://img.shields.io/github/release-date/xemantic/xemantic-kotlin-test\"\u003e](https://github.com/xemantic/xemantic-kotlin-test/releases)\n[\u003cimg alt=\"license\" src=\"https://img.shields.io/github/license/xemantic/xemantic-kotlin-test?color=blue\"\u003e](https://github.com/xemantic/xemantic-kotlin-test/blob/main/LICENSE)\n\n[\u003cimg alt=\"GitHub Actions Workflow Status\" src=\"https://img.shields.io/github/actions/workflow/status/xemantic/xemantic-kotlin-test/build-main.yml\"\u003e](https://github.com/xemantic/xemantic-kotlin-test/actions/workflows/build-main.yml)\n[\u003cimg alt=\"GitHub branch check runs\" src=\"https://img.shields.io/github/check-runs/xemantic/xemantic-kotlin-test/main\"\u003e](https://github.com/xemantic/xemantic-kotlin-test/actions/workflows/build-main.yml)\n[\u003cimg alt=\"GitHub commits since latest release\" src=\"https://img.shields.io/github/commits-since/xemantic/xemantic-kotlin-test/latest\"\u003e](https://github.com/xemantic/xemantic-kotlin-test/commits/main/)\n[\u003cimg alt=\"GitHub last commit\" src=\"https://img.shields.io/github/last-commit/xemantic/xemantic-kotlin-test\"\u003e](https://github.com/xemantic/xemantic-kotlin-test/commits/main/)\n\n[\u003cimg alt=\"GitHub contributors\" src=\"https://img.shields.io/github/contributors/xemantic/xemantic-kotlin-test\"\u003e](https://github.com/xemantic/xemantic-kotlin-test/graphs/contributors)\n[\u003cimg alt=\"GitHub commit activity\" src=\"https://img.shields.io/github/commit-activity/t/xemantic/xemantic-kotlin-test\"\u003e](https://github.com/xemantic/xemantic-kotlin-test/commits/main/)\n[\u003cimg alt=\"GitHub code size in bytes\" src=\"https://img.shields.io/github/languages/code-size/xemantic/xemantic-kotlin-test\"\u003e]()\n[\u003cimg alt=\"GitHub Created At\" src=\"https://img.shields.io/github/created-at/xemantic/xemantic-kotlin-test\"\u003e](https://github.com/xemantic/xemantic-kotlin-test/commits)\n[\u003cimg alt=\"kotlin version\" src=\"https://img.shields.io/badge/dynamic/toml?url=https%3A%2F%2Fraw.githubusercontent.com%2Fxemantic%2Fxemantic-kotlin-test%2Fmain%2Fgradle%2Flibs.versions.toml\u0026query=versions.kotlin\u0026label=kotlin\"\u003e](https://kotlinlang.org/docs/releases.html)\n[\u003cimg alt=\"discord users online\" src=\"https://img.shields.io/discord/811561179280965673\"\u003e](https://discord.gg/vQktqqN2Vn)\n[![Bluesky](https://img.shields.io/badge/Bluesky-0285FF?logo=bluesky\u0026logoColor=fff)](https://bsky.app/profile/xemantic.com)\n\n## Why?\n\nI am mostly using [kotest](https://kotest.io/) library for writing test assertions in my projects.\nWhen [power-assert](https://kotlinlang.org/docs/power-assert.html) became the official Kotlin compiler plugin, I also realized that most of the kotest assertions can be replaced with something which suits my needs much better.\nInstead of writing:\n\n```kotlin\nx shouldBeGreaterThanOrEqualTo 42\n```\n\nI could write:\n\n```kotlin\nassert(x \u003e= 42)\n```\n\nAnd the power-assert will provide comprehensive error message breaking down the expression written in Kotlin into components, while displaying their values.\n\nI am quite often asserting the state of hierarchical data structures, therefore I came up with such a syntax, following the natural language, and utilizing power-assert:\n\n```kotlin\nmessage should {\n    have(id == 42)\n    have(content.size == 2)\n    content[0] should {\n        be\u003cText\u003e()\n        have(type == \"text\")\n        have(\"Hello\" in text)\n    }\n    content[1] should {\n        be\u003cImage\u003e()\n        have(type == \"image\")\n        have(width \u003e= 800)\n        have(height \u003e= 600)\n        mediaType should {\n            have(type == \"image/png\")\n        }\n    }\n}\n```\n\nNow, if the image `mediaType.type` is not `PNG`, it will show:\n\n```text\nMessage(id=42, content=[Text(text=Hello there), Image(path=image.png, width=1024, height=768, mediaType=MediaType(type=image/jpeg))])\n containing:\nImage(path=image.png, width=1024, height=768, mediaType=MediaType(type=image/jpeg))\n containing:\nMediaType(type=image/jpeg)\n should:\nhave(type == \"image/png\")\n     |    |\n     |    false\n     image/jpeg\n```\n\nIn addition, the library supports:\n\n* uniform access to project test files across non-browser platforms of a KMP project\n* access to defined set of environment variables in browser platforms of a KMP project\n\n## Usage\n\n### Setting up Gradle\n\n#### Setting up Gradle for Kotlin Multiplatform project\n\nIn your `build.gradle.kts`:\n\n```kotlin\nplugins {\n    kotlin(\"multiplatform\") version \"2.1.0\"\n    kotlin(\"plugin.power-assert\") version \"2.1.0\" // replace with the latest kotlin version\n}\n\nkotlin {\n\n    sourceSets {\n\n        commonTest {\n            depencencies {\n                implementation(\"com.xemantic.kotlin:xemantic-kotlin-test:1.10.0\")\n            }\n        }\n\n    }\n}\n\npowerAssert {\n    functions = listOf(\n        \"com.xemantic.kotlin.test.assert\",\n        \"com.xemantic.kotlin.test.have\"\n    )\n}\n```\n\n#### Setting up Gradle for Kotlin JVM project\n\nIn your `build.gradle.kts`:\n\n```kotlin\nplugins {\n    kotlin(\"jvm\") version \"2.1.0\"\n    kotlin(\"plugin.power-assert\") version \"2.1.0\" // replace with the latest kotlin version\n}\n\ndependencies {\n    testImplementation(\"com.xemantic.kotlin:xemantic-kotlin-test:1.10.0\")\n}\n\npowerAssert {\n    functions = listOf(\n        \"com.xemantic.kotlin.test.assert\",\n        \"com.xemantic.kotlin.test.have\"\n    )\n}\n```\n\n### Basic Assertions\n\n```kotlin\nassert(2 + 2 == 4)\n```\n\n\u003e [!NOTE]\n\u003e The [assert](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/assert.html) function in Kotlin stdlib is providing `assert` only for `jvm` and `native` out of all the Kotlin multiplatform targets.\n\u003e The multiplatform `assert` function can be imported from `com.xemantic.kotlin.test.assert`.\n\n### Asserting object properties\n\nThe library introduces [should](src/commonMain/kotlin/Assertions.kt) infix function, which allows you to chain assertions on an object:\n\n```kotlin\nsomeObject should {\n    // assertions go here\n}\n```\n\n### Type Assertions\n\nYou can assert the type of object using the [be](src/commonMain/kotlin/Assertions.kt) function:\n\n```kotlin\nsomeObject should {\n    be\u003cExpectedType\u003e()\n}\n```\n\n\u003e [!TIP]\n\u003e After calling `be` function with expected type, all the subsequent calls within `should {}` will have access to the properties of the expected type, like if `this`, representing `someObject`, was cast to the expected type.\n\n### Condition Assertions\n\nUse the `have` function to assert conditions:\n\n```kotlin\nsomeObject should {\n    have(someProperty == expectedValue)\n}\n```\n\n### Nested Assertions\n\nYou can nest assertions for complex objects:\n\n```kotlin\ncomplexObject should {\n    have(property1 == expectedValue1)\n    nestedObject should {\n        have(nestedProperty == expectedValue2)\n    }\n}\n```\n\n### Test Context\n\nYou can obtain access to the test context like:\n\n* Stable absolute path of the current gradle root dir, so that the test files can be used in tests of non-browser\n  platforms of the KMP project.\n* Environment variables, accessible on almost all the platforms, including access to predefined set of environment\n  variables in tests of browser platforms (e.g. API keys) of the KMP project.\n\nSee [TextContext](src/commonMain/kotlin/TestContext.kt) for details.\n\nYou have to add to `build.gradle.kts`:\n\n```kotlin\nval gradleRootDir: String = rootDir.absolutePath\nval fooValue = \"bar\"\n\ntasks.withType\u003cKotlinJvmTest\u003e().configureEach {\n    environment(\"GRADLE_ROOT_DIR\", gradleRootDir)\n    environment(\"FOO\", fooValue)\n}\n\ntasks.withType\u003cKotlinJsTest\u003e().configureEach {\n    environment(\"GRADLE_ROOT_DIR\", gradleRootDir)\n    environment(\"FOO\", fooValue)\n}\n\ntasks.withType\u003cKotlinNativeTest\u003e().configureEach {\n    environment(\"GRADLE_ROOT_DIR\", gradleRootDir)\n    environment(\"SIMCTL_CHILD_GRADLE_ROOT_DIR\", gradleRootDir)\n    environment(\"FOO\", fooValue)\n    environment(\"SIMCTL_CHILD_FOO\", fooValue)\n}\n```\n\nand specify environment variables you are interested in. The `SIMCTL_CHILD_` is used in tests running inside emulators.\n\nTo pass environment variables to browser tests, you have to create `webpack.confg.d` folder and drop this file named `env-config.js`:\n\n```js\nconst webpack = require(\"webpack\");\nconst envPlugin = new webpack.DefinePlugin({\n    'process': {\n        'env': {\n            'FOO': JSON.stringify(process.env.FOO)\n        }\n    }\n});\nconfig.plugins.push(envPlugin);\n```\n\nPick environment variables which should be provided to browser tests.\n\nThen you can write test like:\n\n```kotlin\nclass FooTest {\n\n    @Test\n    fun `should test against test data`() {\n        if (isBrowserPlatform) return // we don't have access to Gradle root dir\n        val testData = Path(gradleRootDir, \"test-data.txt\")\n        // ...\n    }\n\n    @Test\n    fun `should use predefined environment variable`() {\n        val apiKey = getEnv(\"SOME_API_KEY\")\n        // ...\n    }\n\n}\n```\n\n## Development\n\nClone this project, and then run:\n\n```shell\n./gradlew build\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxemantic%2Fxemantic-kotlin-test","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxemantic%2Fxemantic-kotlin-test","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxemantic%2Fxemantic-kotlin-test/lists"}