GithubHelp home page GithubHelp logo

gaumala / kotlinsnapshot Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 16.0 86 KB

Experimental Snapshot Testing in Kotlin

License: MIT License

Java 93.81% Kotlin 6.19%
android snapshot-testing unit-testing kotlin java gradle

kotlinsnapshot's Introduction

KotlinSnapshot Build Status

Snapshot testing in Kotlin. The implementation of the diff algorithm used is taken from diff-match-patch.

Install

Download with gradle from JitPack. First add the JitPack repository to your root build.gradle file:

  allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}

Then on your module's build.gradle:

dependencies {
  testImplementation 'com.github.GAumala:KotlinSnapshot:0.1'
}

Usage

Create an instance of Camera in your test file and use the method matchWithSnapshot, which takes 2 arguments: A string with the name of the snapshot and a Any object to be shot using its toString() implementation. Example:

class NetworkTest {

    private val camera = Camera()
    private val networkClient = MyNetworkClient()

    @Test
    fun shouldFetchDataFromNetwork() {
        val myData = networkClient.fetchData()
        camera.matchWithSnapshot("should fetch data from network", myData)
    }

After you run the test for the first time, a new snapshot will be written in the __snapshot__ directory of the root of your project. The written snapshot for this example would look like this:

$ cat __snapshot__/should\ fetch\ data\ from\ network.snap 
{"name":"gabriel","id":5}

On subsequent runs, the value will be compared with the snapshot stored in the filesystem if the are not equal, your test will fail. To see the detailed error you may need to run your tests with ./gradlew test --info. You should see something like this:

Snapshot Error

You can also specify the path relative to the project's root where you want the snapshot dir to be placed using Camera's constructor:

    val camera = Camera(relativePath = "src/test/kotlin/com/my/package")

Updating Snapshots

In order to update and purge snapshots from the command line, you should add a new system property called "updateSnapshots" to your build.gradle file:

test {
   // update snapshots via command line
   systemProperty "updateSnapshots", System.getProperty("u")
}

On android projects you might need to do this instead:

    testOptions {
        unitTests.all {
            // update snapshots via command line
            systemProperty "updateSnapshots", System.getProperty("u")
        }
    }

Then, when you want to update the snapshot for a specific test:

./gradlew test -Du=1 --info --tests *MyTest.someTestINeedToUpdate

Please note that in this example I used "u" but, it can be any flag, depending on how you configure your gradle file.

On android projects the test task might not support the --tests flag to filter tests, you might need to run something like testDebug instead. It is not necessary to filter tests when updating, but you should do it to avoid inconsistencies.

Purging Snapshots

As you rename snapshots, old unused snapshots may remain in your project. You can delete all existing snapshots and rebuild the ones that are actually used using the "purgeSnapshots" system property. The setup is identical to the one used for updating snapshots.

test {
   // purge snapshots via command line
   systemProperty "purgeSnapshots", System.getProperty("p")
}

Again android projects may need to handle this differently:

    testOptions {
        unitTests.all {
            // update snapshots via command line
            systemProperty "purgeSnapshots", System.getProperty("p")
        }
    }

Then, when you want to delete the snapshots:

./gradlew test -Dp=1 --info

kotlinsnapshot's People

Contributors

gaumala avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

kotlinsnapshot's Issues

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.