https://github.com/jw3126/snaptests.jl
Minimalistic snapshot tests
https://github.com/jw3126/snaptests.jl
julia snapshot test
Last synced: 11 months ago
JSON representation
Minimalistic snapshot tests
- Host: GitHub
- URL: https://github.com/jw3126/snaptests.jl
- Owner: jw3126
- License: mit
- Created: 2023-06-03T22:53:43.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-05-06T07:59:09.000Z (about 2 years ago)
- Last Synced: 2025-07-05T18:23:45.624Z (12 months ago)
- Topics: julia, snapshot, test
- Language: Julia
- Homepage:
- Size: 13.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SnapTests
[](https://github.com/jw3126/SnapTests.jl/actions/workflows/CI.yml?query=branch%3Amain)
[](https://codecov.io/gh/jw3126/SnapTests.jl)
Minimalistic package for testing values against data stored on disk.
# Usage
```julia
using Test
using SnapTests
path = "hello1.txt"
write(path, "hello world")
@test matchsnap(path, "hello world")
@test matchsnap(path, "hi world!") # fail with a diff
@test matchsnap("does_not_exist", "hi world!") # ask for path creation
SnapTests.on_cmp_false = :ask # ask to update data on disk if test fails
SnapTests.on_cmp_false = :replace # silently replace data on disk if test fails
SnapTests.on_cmp_false = :return # default, matchsnap will just return false
# Customization
# Lets customize test so that instead of loading from disk, stuff gets looked up from a database
enterprise_db = Dict()
struct Lookup
key::Symbol
end
SnapTests.exists(l::Lookup, value) = haskey(enterprise_db, l.key)
SnapTests.load(l::Lookup, value) = enterprise_db[l.key]
SnapTests.save(l::Lookup, value) = (enterprise_db[l.key] = value)
@test matchsnap(Lookup(:key1), 1)
@test matchsnap(Lookup(:key2), 2)
@test matchsnap(Lookup(:key2), 2)
@test matchsnap(Lookup(:key2), 3) # fails
```
# Tooling
One drawback of writing values to a file instead of hard coding them in a test is
that reading the test is less self contained.
To alleviate this, there are ways for various editors to quickly peek the contents of a file.
## Neovim
Using [telescope](https://github.com/nvim-telescope/telescope.nvim) one can bind the following to some keys:
```lua
require('telescope.builtin').find_files({ default_text=vim.fn.expand('')})"
```
# Alternatives
* [ReferenceTests.jl](https://github.com/JuliaTesting/ReferenceTests.jl)
Compared to this package many more awesome features. But also heavier dependencies
and hard to customize, especially if FileIO does not cover your use case.
* [SnapshotTests.jl](https://github.com/mattwigway/SnapshotTests.jl)