Code Monkey home page Code Monkey logo

snapshotino's Introduction

snapshotino

A lightweight, low-barrier-to-entry snapshot testing library for iOS

Why snapshot tests?

  • They're easy to write, maintain, and debug
  • They focus on the visual output of your code (which is what the user sees)
  • They encourage you to write small, modular, and testable components

What this library is capable of

  • Recording and comparing snapshots of SwiftUI.View, UIView, and UIViewController
  • Storing the expected and actual snapshots in the test results

That's it!

What it's definitely not capable of

  • Recording snapshots using different accessibility traits or devices

What it's probably not capable of

  • Any other use cases that weren't mentioned above

Author's note

If you are looking for a library that you will use on your production app, you should probably consider more powerful tools like pointfreeco/swift-snapshot-testing or uber/ios-snapshot-test-case. This library is more of a pet project than a finished product. I wanted to snapshot-test the small apps and components I build for fun without importing a full-blown 3rd party for my limited use case.

As a less interesting fact, this is also why I tried to imply the small size of the initiative in the project name, by using the -ino suffix, which appears to be a common diminutive suffix in Italian.

How to use

  • Add the library as a dependency using Swift Package Manager
  • Import snapshotino in your XCTestCase implementation
  • Use assertSnapshot to record and compare snapshots, examples below

For recording snapshots

Simply call assertSnapshot by setting record to true. The test will fail but the snapshot will be recorded under the __snapshots__ folder next to the test file.

func testUIViewController() throws {
    try assertSnapshot(of: SampleViewController(), record: true)
}

For asserting snapshots

Call assertSnapshot without setting the record attribute (it's false by default).

func testUIViewController() throws {
    try assertSnapshot(of: SampleViewController())
}

func testUIView() throws {
    if let view = Bundle.main.loadNibNamed("SampleView", owner: self, options: nil)?.first as? SampleView {
        try assertSnapshot(of: view)
    }
}

func testView() throws {
    let view = ContentView(viewModel: .init(imageName: "circle", text: "test"))
    try assertSnapshot(of: view.asSnapshottableView)
}

Note that SwiftUI.View cannot be used directly because it's not a concrete type. We use the asSnapshottableView property instead, which will return a wrapper type that can be passed to assertSnapshot.

Sample project

https://github.com/kublaios/KleinSnapshotTesting

License

This project is released under the MIT license. See LICENSE for details.

snapshotino's People

Contributors

kublaios avatar

Watchers

 avatar  avatar

snapshotino's Issues

Include the test name in the snapshot name

Currently, the snapshots are named after the unit they are testing. When there is more than one test for the same unit (which is the case often), the tests override each other's snapshot. One fix could be to include the test name in the snapshot name.

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.