Generate unit testing boilerplate from kotlin files.

Overview

TestIt

Build Status License

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

Once installed, running TestIt is as simple as

testit path/to/file.kt

Or, to generate parameterized tests:

testit -p 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 org.mockito.kotlin.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 overloaded functions
  • Supports both mockito 2 and MockK
  • Generates test code for abstract classes
  • Generates test code for extension functions
  • Generates test code for static functions
  • Generates parameterized tests code
  • Generates test code for exceptions
  • Configurable

Acknowledgments

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

Created by

Eran Boudjnah

License

MIT © Eran Boudjnah

You might also like...
A ksp library to automatically generate navigation functions for jetpack compose.
A ksp library to automatically generate navigation functions for jetpack compose.

Compose/Navigation/Generator ⚠️ This library is still under development and not considered stable! Content Introduction Usage Example: Single destinat

KotlinScript that generate Reel from a given image, text and audio

ReelScriot KotlinScript that generate Reel from a given image, text and audio 80f4ea39-a7da-4f21-b0ff-7a17836a1cd0.mp4 6691b51d-d7a3-4915-ae41-8bec400

Maxibon kata for Kotlin Developers. The main goal is to practice property based testing.
Maxibon kata for Kotlin Developers. The main goal is to practice property based testing.

Kata Maxibon for Kotlin. We are here to practice property based testing. We are going to use KotlinTest to write our tests. We are going to practice p

TODO API Client Kata for Kotlin Developers. The main goal is to practice integration testing using MockWebServer
TODO API Client Kata for Kotlin Developers. The main goal is to practice integration testing using MockWebServer

KataTODOApiClient for Kotlin We are here to practice integration testsing using HTTP stubbing. We are going to use MockWebServer to simulate a HTTP se

Screenshot Kata for Android Developers with Kotlin. The main goal is to practice UI Screenshot Testing.
Screenshot Kata for Android Developers with Kotlin. The main goal is to practice UI Screenshot Testing.

KataScreenshot in Kotlin We are here to practice UI testing using screenshot tests for Android. We are going to use Espresso to interact with the Appl

Super Heroes Kata for Android Developers in Kotlin. The main goal is to practice UI Testing.
Super Heroes Kata for Android Developers in Kotlin. The main goal is to practice UI Testing.

KataSuperHeroes in Kotlin We are here to practice UI Testing. We are going to use Espresso to interact with the Application UI. We are going to use Ko

Nice and simple DSL for Espresso Compose UI testing in Kotlin
Nice and simple DSL for Espresso Compose UI testing in Kotlin

Kakao Compose Nice and simple DSL for Espresso Compose in Kotlin Benefits Readability Reusability Extensible DSL How to use it Create Screen Create yo

Code for the Advanced Android Kotlin Testing Codelab 5.1-5.3
Code for the Advanced Android Kotlin Testing Codelab 5.1-5.3

TO-DO Notes - Code for 5.1-5.3 Testing Codelab Code for the Advanced Android Kotlin Testing Codelab 5.1-5.3 Introduction TO-DO Notes is an app where y

A seed and demo about how to do end-to-end testing of a Dataflow pipeline

dataflow-e2e-demo This is a demo and a seed project to show how you can end-to-end test a Dataflow pipeline. You can find more about by follwing this

Comments
  • Gradle lifecycle tasks broken, preventing Jitpack to build

    Gradle lifecycle tasks broken, preventing Jitpack to build

    Hi,

    here is the link and logs https://jitpack.io/com/github/EranBoudjnah/TestIt/master-6c4d0b55ce-1/build.log https://jitpack.io/com/github/EranBoudjnah/TestIt/app/master-6c4d0b55ce-1/build.log

    Build starting...
    Start: Mon Aug 10 17:15:46 UTC 2020 da335ec3eb71
    Git:
    6c4d0b5
    commit 6c4d0b55ce92c7b7e7a3a702639725c32fde36fe
    Author: Eran Boudjnah 
    Date:   Thu Jul 2 20:38:08 2020 +0100
    
        Added lint and code style hooks.
    
    
    Found gradle
    Gradle build script
    Found gradle version: 5.4.1.
    Using gradle wrapper
    Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 -Dhttps.protocols=TLSv1.2
    Downloading https://services.gradle.org/distributions/gradle-5.4.1-all.zip
    .
    Unzipping /home/jitpack/.gradle/wrapper/dists/gradle-5.4.1-all/3221gyojl5jsh0helicew7rwx/gradle-5.4.1-all.zip to /home/jitpack/.gradle/wrapper/dists/gradle-5.4.1-all/3221gyojl5jsh0helicew7rwx
    Set executable permissions for: /home/jitpack/.gradle/wrapper/dists/gradle-5.4.1-all/3221gyojl5jsh0helicew7rwx/gradle-5.4.1/bin/gradle
    
    ------------------------------------------------------------
    Gradle 5.4.1
    ------------------------------------------------------------
    
    Build time:   2019-04-26 08:14:42 UTC
    Revision:     261d171646b36a6a28d5a19a69676cd098a4c19d
    
    Kotlin:       1.3.21
    Groovy:       2.5.4
    Ant:          Apache Ant(TM) version 1.9.13 compiled on July 10 2018
    JVM:          11.0.2 (Oracle Corporation 11.0.2+9)
    OS:           Linux 4.14.63-xxxx-std-ipv6-64 amd64
    
    0m3.669s
    Getting tasks: ./gradlew tasks --all
    Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 -Dhttps.protocols=TLSv1.2
    Tasks: 
    
    WARNING:
    Gradle 'install' task not found. Please add the 'maven' or 'android-maven' plugin.
    See the documentation and examples: https://jitpack.io/docs/
    
    Looking for android-library
    Looking for com.android.application
    Adding maven plugin
    Running: ./gradlew clean -Pgroup=com.github.EranBoudjnah -Pversion=master-6c4d0b55ce-1 install
    Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 -Dhttps.protocols=TLSv1.2
    
    FAILURE: Build failed with an exception.
    
    * Where:
    Build file '/home/jitpack/build/build.gradle' line: 55
    
    * What went wrong:
    A problem occurred evaluating root project 'build'.
    > Failed to apply plugin [class 'org.gradle.language.base.plugins.LifecycleBasePlugin']
       > Cannot add task 'clean' as a task with that name already exists.
    
    * Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
    
    * Get more help at https://help.gradle.org
    
    BUILD FAILED in 0s
    Build tool exit code: 0
    Looking for artifacts...
    Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 -Dhttps.protocols=TLSv1.2
    Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 -Dhttps.protocols=TLSv1.2
    Looking for pom.xml in build directory and ~/.m2
    2020-08-10T17:16:19.444779057Z
    Exit code: 0
    
    ERROR: No build artifacts found
    

    clean should be, maybe, withType(Clean) or similar

    opened by soberich 0
Owner
Eran Boudjnah
Android Lead (contractor) https://optimisingmylife.com
Eran Boudjnah
Integration Testing Kotlin Multiplatform Kata for Kotlin Developers. The main goal is to practice integration testing using Ktor and Ktor Client Mock

This kata is a Kotlin multiplatform version of the kata KataTODOApiClientKotlin of Karumi. We are here to practice integration testing using HTTP stub

Jorge Sánchez Fernández 29 Oct 3, 2022
Modular Android architecture which showcase Kotlin, MVVM, Navigation, Hilt, Coroutines, Jetpack compose, Retrofit, Unit test and Kotlin Gradle DSL.

SampleCompose Modular Android architecture which showcase Kotlin, MVVM, Navigation, Hilt, Coroutines, Jetpack compose, Retrofit, Unit test and Kotlin

Mohammadali Rezaei 7 Nov 28, 2022
🎓 Learning Kotlin Coroutines for Android by example. 🚀 Sample implementations for real-world Android use cases. 🛠 Unit tests included!

Kotlin Coroutines - Use Cases on Android ?? Learning Kotlin Coroutines for Android by example. ?? Sample implementations for real-world Android use ca

Lukas Lechner 2.1k Jan 3, 2023
A Movie listing app with unit tests and a couple of UI tests

MovieListings App This app is a Movie listing app with unit tests and a couple of UI tests. The app gets a list of movies from a public API, and the d

Abayomi Akanji 1 Dec 1, 2021
🎲 Kotlin Symbol Processor to auto-generate extensive sealed classes and interfaces for Android and Kotlin.

SealedX ?? Kotlin Symbol Processor to auto-generate extensive sealed classes and interfaces for Android and Kotlin. Why SealedX? SealedX generates ext

Jaewoong Eum 236 Nov 30, 2022
A Kotlin library used to analyse discrete Markov chains, in order to generate plausible sequences

Markov Markov is a Kotlin library used to analyse discrete Markov chains, in order to generate plausible sequences. Using This project is still under

Xavier F. Gouchet 0 Nov 14, 2021
A project aiming to generate KMP declarations from several library versions

kotlin-ketchup A project aiming to generate KMP declarations from several library versions LICENSE Apache 2.0. See ./LICENSE in this repository See th

Eugene Petrenko 4 Mar 1, 2022
Used to generate the template code of GetX framework

Language: English | 中文简体 statement some fast code snippet prompt come from getx-snippets-intelliJ Description install Plugin effect Take a look at the

小呆呆666 242 Dec 30, 2022
FizzBuzzKotlin - A function fizzBuzz to generate a list of string based on the input number

FizzBuzzKotlin write a function fizzBuzz to generate a list of string based on t

gson 0 Feb 12, 2022
A project that helps us generate the test project to test the Gradle plugin.

Ktlint Gradle Provides the function to generate a Gradle project for us to test your Gradle plugin Latest plugin version: [1.0.0] Table of content How

Jack Chen 5 Jul 20, 2022