From fdafaf948cf51f2c447c601282165aee77935914 Mon Sep 17 00:00:00 2001 From: Petr Komissarov Date: Thu, 16 May 2024 19:03:16 +0300 Subject: [PATCH] Add unit tests --- .github/workflows/build.yml | 38 ++++++++++++- build.gradle.kts | 10 +++- gradle.properties | 41 +++++++------- src/test/kotlin/ru/testit/management/.gitkeep | 0 .../ru/testit/management/ClientTests.kt | 25 +++++++++ .../ru/testit/management/ClipboardTests.kt | 29 ++++++++++ .../ru/testit/management/CodeSnippedTests.kt | 53 +++++++++++++++++++ .../ru/testit/management/MessagesTests.kt | 24 +++++++++ src/test/resources/junit-platform.properties | 1 + 9 files changed, 199 insertions(+), 22 deletions(-) delete mode 100644 src/test/kotlin/ru/testit/management/.gitkeep create mode 100644 src/test/kotlin/ru/testit/management/ClientTests.kt create mode 100644 src/test/kotlin/ru/testit/management/ClipboardTests.kt create mode 100644 src/test/kotlin/ru/testit/management/CodeSnippedTests.kt create mode 100644 src/test/kotlin/ru/testit/management/MessagesTests.kt diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 19bfc9c..6436336 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -9,7 +9,7 @@ on: concurrency: group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} cancel-in-progress: true - + jobs: # Prepare environment and build the plugin @@ -76,6 +76,42 @@ jobs: name: ${{ steps.artifact.outputs.filename }} path: ./build/distributions/content/*/* + # Run tests and upload a code coverage report + test: + name: Test + needs: [ build ] + runs-on: ubuntu-latest + steps: + + # Check out current repository + - name: Fetch Sources + uses: actions/checkout@v4 + + # Set up Java environment for the next steps + - name: Setup Java + uses: actions/setup-java@v4 + with: + distribution: zulu + java-version: 17 + + # Setup Gradle + - name: Setup Gradle + uses: gradle/actions/setup-gradle@v3 + with: + gradle-home-cache-cleanup: true + + # Run tests + - name: Run Tests + run: ./gradlew check -DtestCi=true + + # Collect Tests Result of failed tests + - name: Collect Tests Result + if: ${{ failure() }} + uses: actions/upload-artifact@v4 + with: + name: tests-result + path: ${{ github.workspace }}/build/reports/tests + # Run plugin structure verification along with IntelliJ Plugin Verifier verify: name: Verify plugin diff --git a/build.gradle.kts b/build.gradle.kts index d61252e..9743d14 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -43,6 +43,10 @@ kotlin { } tasks { + wrapper { + gradleVersion = properties("gradleVersion").get() + } + buildSearchableOptions { enabled = false } @@ -94,8 +98,12 @@ tasks { "java.util.logging.manager", "java.util.logging.LogManager" ) + systemProperty( + "TEST_CI", + System.getProperty("testCi") + ) reports { - html.required.set(false) + html.required.set(true) junitXml.required.set(false) } testLogging { diff --git a/gradle.properties b/gradle.properties index 04c1914..bd14be2 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,20 +1,21 @@ -pluginVersion = 1.0.5 -pluginGroup = ru.testit.management -pluginName = testit_management -pluginRepositoryUrl = https://github.com/testit-tms/testit-management/ -pluginSinceBuild = 231 -pluginUntilBuild = 241.* -platformType = IC -platformVersion = 2024.1.1 -platformPlugins = -javaEncoding = UTF-8 -javaVersion = 17 -kotlin.code.style = official -kotlin.stdlib.default.dependency = false -org.gradle.caching = true -org.gradle.configuration-cache = true -org.gradle.configureondemand = true -org.gradle.parallel = true -org.gradle.warning.mode = all -org.gradle.welcome = never -org.gradle.jvmargs = -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 -XX:+UseParallelGC \ No newline at end of file +pluginVersion=1.0.5 +pluginGroup=ru.testit.management +pluginName=testit_management +pluginRepositoryUrl=https://github.com/testit-tms/testit-management/ +gradleVersion=8.7 +pluginSinceBuild=231 +pluginUntilBuild=241.* +platformType=IC +platformVersion=2024.1.1 +platformPlugins= +javaEncoding=UTF-8 +javaVersion=17 +kotlin.code.style=official +kotlin.stdlib.default.dependency=false +org.gradle.caching=true +org.gradle.configuration-cache=true +org.gradle.configureondemand=true +org.gradle.parallel=true +org.gradle.warning.mode=all +org.gradle.welcome=never +org.gradle.jvmargs=-XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 -XX:+UseParallelGC \ No newline at end of file diff --git a/src/test/kotlin/ru/testit/management/.gitkeep b/src/test/kotlin/ru/testit/management/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/test/kotlin/ru/testit/management/ClientTests.kt b/src/test/kotlin/ru/testit/management/ClientTests.kt new file mode 100644 index 0000000..1c04b55 --- /dev/null +++ b/src/test/kotlin/ru/testit/management/ClientTests.kt @@ -0,0 +1,25 @@ +package ru.testit.management + +import org.junit.jupiter.api.DisplayName +import ru.testit.management.clients.TmsClient +import kotlin.test.DefaultAsserter.assertEquals +import kotlin.test.Test + + +@DisplayName("Client") +class ClientTests { + + @DisplayName("Validation") + @Test + fun checkMessage() { + // Arrange + val expected = + "Expected URL scheme 'http' or 'https' but no scheme was found for /api/v..." + + // Act + val actual = TmsClient.getSettingsValidationErrorMsg("", "", "") + + // Assert + assertEquals("Validation error message assertion failed!", expected, actual) + } +} \ No newline at end of file diff --git a/src/test/kotlin/ru/testit/management/ClipboardTests.kt b/src/test/kotlin/ru/testit/management/ClipboardTests.kt new file mode 100644 index 0000000..1077e70 --- /dev/null +++ b/src/test/kotlin/ru/testit/management/ClipboardTests.kt @@ -0,0 +1,29 @@ +package ru.testit.management + +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.condition.DisabledIfSystemProperty +import ru.testit.management.utils.ClipboardUtils +import java.awt.Toolkit.getDefaultToolkit +import java.awt.datatransfer.DataFlavor +import kotlin.test.DefaultAsserter.assertEquals +import kotlin.test.Test + + +@DisplayName("Clipboard") +class ClipboardTests { + + @DisplayName("Copy") + @DisabledIfSystemProperty(named = "TEST_CI", matches = "true") + @Test + fun checkCopy() { + // Arrange + val expected = "Test123" + + // Act + ClipboardUtils.copyToClipboard(expected) + val actual = getDefaultToolkit().systemClipboard.getData(DataFlavor.stringFlavor) as String + + // Assert + assertEquals("Clipboard value assertion failed!", expected, actual) + } +} \ No newline at end of file diff --git a/src/test/kotlin/ru/testit/management/CodeSnippedTests.kt b/src/test/kotlin/ru/testit/management/CodeSnippedTests.kt new file mode 100644 index 0000000..f9662bd --- /dev/null +++ b/src/test/kotlin/ru/testit/management/CodeSnippedTests.kt @@ -0,0 +1,53 @@ +package ru.testit.management + +import kotlinx.collections.immutable.persistentSetOf +import org.junit.jupiter.api.DisplayName +import ru.testit.client.model.StepModel +import ru.testit.management.utils.CodeSnippedUtils +import ru.testit.management.windows.tools.TmsNodeModel +import kotlin.test.DefaultAsserter.assertEquals +import kotlin.test.Test + + +@DisplayName("Code snippet") +class CodeSnippedTests { + + @DisplayName("Create") + @Test + fun checkCodeSnippet() { + // Arrange + val name = "Test name" + val globalID = 12345L + + val preModel = StepModel() + preModel.action = "Precondition text" + val pre = persistentSetOf(preModel) + + val stepModel = StepModel() + stepModel.action = "Step text" + val steps = persistentSetOf(stepModel) + + val postModel = StepModel() + postModel.action = "Postcondition text" + val post = persistentSetOf(postModel) + + val model = TmsNodeModel(name, globalID, pre, steps, post) + val expected = """@WorkItemIds("$globalID") +@Test +public void testName() { + // See work item [$globalID] for detailed steps description + // Pre: + // ${preModel.action} + // Steps: + // ${stepModel.action} + // Post: + // ${postModel.action} +}""" + + // Act + val actual = CodeSnippedUtils.getNewSnippet(model) + + // Assert + assertEquals("Code snipped text assertion failed!", expected, actual) + } +} \ No newline at end of file diff --git a/src/test/kotlin/ru/testit/management/MessagesTests.kt b/src/test/kotlin/ru/testit/management/MessagesTests.kt new file mode 100644 index 0000000..b246467 --- /dev/null +++ b/src/test/kotlin/ru/testit/management/MessagesTests.kt @@ -0,0 +1,24 @@ +package ru.testit.management + +import org.junit.jupiter.api.DisplayName +import ru.testit.management.utils.MessagesUtils +import kotlin.test.DefaultAsserter.assertEquals +import kotlin.test.Test + + +@DisplayName("Messages") +class MessagesTests { + + @DisplayName("Copy") + @Test + fun checkCopy() { + // Arrange + val expected = "Copy" + + // Act + val actual = MessagesUtils.get("window.tool.popup.copy.text") + + // Assert + assertEquals("Message text assertion failed!", expected, actual) + } +} \ No newline at end of file diff --git a/src/test/resources/junit-platform.properties b/src/test/resources/junit-platform.properties index 8670e43..0fff85e 100644 --- a/src/test/resources/junit-platform.properties +++ b/src/test/resources/junit-platform.properties @@ -1,3 +1,4 @@ junit.jupiter.execution.parallel.enabled=true junit.jupiter.execution.parallel.mode.default=same_thread junit.jupiter.execution.parallel.mode.classes.default=concurrent +junit.jupiter.displayname.generator.default=org.junit.jupiter.api.DisplayNameGenerator$ReplaceUnderscores