• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

GAumala/KotlinSnapshot: Experimental Snapshot Testing in Kotlin

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

GAumala/KotlinSnapshot

开源软件地址(OpenSource Url):

https://github.com/GAumala/KotlinSnapshot

开源编程语言(OpenSource Language):

Java 93.8%

开源软件介绍(OpenSource 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



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap