Skip to content
Generate unit testing boilerplate from kotlin files.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.idea
app
gradle/wrapper
.gitignore
LICENSE
README.md
build.gradle
gradle.properties
gradlew
gradlew.bat
settings.gradle
settings.properties
testit

README.md

TestIt

While TDD is a better approach for development, many projects don't practice it and end up with low test coverage.

This project is here to help you improve your test coverage by reducing the effort spent on writing boilerplate code, allowing you to focus on writing the actual tests.

Use TestIt to generate unit testing boilerplate from kotlin files.

Getting Started

While you can run ./gradlew run --args "filepath", it might be more convenient to set up a shortcut to the provided helper script:

Install

  1. Get HomeBrew
  2. Run brew install coreutils
  3. Create a symbolic link: sudo ln -s /path/to/testit /usr/local/bin

Note: your project would need to include mockito 2 and mockito-kotlin or MockK.

Usage

One installed, running TestIt is as simple as

testit path/to/file.kt

Output

TestIt generates a test file in the default expected path.

For example, when run against itself (testit app/src/main/java/com/mitteloupe/testit/TestIt.kt) - see source file - it generates the below file at app/src/test/java/com/mitteloupe/testit/TestItTest.kt:

package com.mitteloupe.testit

import com.mitteloupe.testit.config.PropertiesReader
import com.mitteloupe.testit.file.FileProvider
import com.mitteloupe.testit.generator.TestFilePathFormatter
import com.mitteloupe.testit.generator.TestsGeneratorFactory
import com.mitteloupe.testit.model.ClassTestCode
import com.mitteloupe.testit.parser.KotlinFileParser
import com.nhaarman.mockitokotlin2.mock
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.junit.MockitoJUnitRunner

@RunWith(MockitoJUnitRunner::class)
class TestItTest {
    private lateinit var cut: TestIt

    @Mock
    lateinit var propertiesReader: PropertiesReader

    @Mock
    lateinit var fileProvider: FileProvider

    @Mock
    lateinit var kotlinFileParser: KotlinFileParser

    @Mock
    lateinit var testFilePathFormatter: TestFilePathFormatter

    @Mock
    lateinit var testsGeneratorFactory: TestsGeneratorFactory

    @Before
    fun setUp() {
        cut = TestIt(propertiesReader, fileProvider, kotlinFileParser, testFilePathFormatter, testsGeneratorFactory)
    }

    @Test
    fun `Given _ when getTestsForFile then _`() {
        // Given
        val fileName = "fileName"

        // When
        val actualValue = cut.getTestsForFile(fileName)

        // Then
        TODO("Define assertions")
    }

    @Test
    fun `Given _ when saveTestsToFile then _`() {
        // Given
        val sourceFileName = "sourceFileName"
        val classTestCode = mock<ClassTestCode>()

        // When
        cut.saveTestsToFile(sourceFileName, classTestCode)

        // Then
        TODO("Define assertions")
    }

    @Test
    fun `Given _ when showHelp then _`() {
        // Given

        // When
        cut.showHelp()

        // Then
        TODO("Define assertions")
    }
}

Features

  • Automatically compiles a list of required imports
  • Supports multiple classes in one Kotlin file
  • Supports both mockito 2 and MockK
  • Can generate test code for abstract classes
  • Configurable

Acknowledgments

This code uses a JAR from kotlin-grammar-tools to parse Kotlin code.

Created by

Eran Boudjnah

License

MIT © Eran Boudjnah

You can’t perform that action at this time.