Git Product home page Git Product logo

testit's Introduction

TestIt

GitHub Workflow 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

testit's People

Contributors

eranboudjnah avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

testit's Issues

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.