Snapshot assertions¶
Compare a value against a stored snapshot, recording it on first run.
Snapshot mixin.
Take a snapshot of a python data structure, store it on disk in JSON format, and automatically compare the latest data to the stored data on every test run.
Functional testing (which snapshot testing falls under) is very much blackbox testing. When something goes wrong, it's hard to pinpoint the issue, because functional tests typically provide minimal isolation as compared to unit tests. On the plus side, snapshots typically do provide enormous leverage as a few well-placed snapshot tests can strongly verify that an application is working. Similar coverage would otherwise require dozens if not hundreds of unit tests.
On-disk Format
Snapshots are stored in a readable JSON format. For example:
assert_that({'a': 1, 'b': 2, 'c': 3}).snapshot()
Would be stored as:
{
"a": 1,
"b": 2,
"c": 3
}
The JSON formatting support most python data structures (dict, list, object, etc), but not custom binary data.
Updating
It's easy to update your snapshots...just delete them all and re-run the test suite to regenerate all snapshots.
snapshot ¶
Asserts that val is identical to the on-disk snapshot stored previously.
On the first run of a test before the snapshot file has been saved, a snapshot is created, stored to disk, and the test always passes. But on all subsequent runs, val is compared to the on-disk snapshot, and the test fails if they don't match.
Snapshot artifacts are stored in the __snapshots directory by default, and should be
committed to source control alongside any code changes.
Snapshots are identified by test filename plus line number by default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str | None
|
a custom snapshot identifier (defaults to test filename plus line number) |
None
|
path
|
str
|
the directory where snapshots are stored (defaults to |
'__snapshots'
|
Examples:
Usage:
assert_that(None).snapshot()
assert_that(True).snapshot()
assert_that(1).snapshot()
assert_that(123.4).snapshot()
assert_that('foo').snapshot()
assert_that([1, 2, 3]).snapshot()
assert_that({'a': 1, 'b': 2, 'c': 3}).snapshot()
assert_that({'a', 'b', 'c'}).snapshot()
assert_that(1 + 2j).snapshot()
assert_that(someobj).snapshot()
By default, snapshots are identified by test filename plus line number.
Alternately, you can specify a custom identifier using the id arg:
assert_that({'a': 1, 'b': 2, 'c': 3}).snapshot(id='foo-id')
By default, snapshots are stored in the __snapshots directory.
Alternately, you can specify a custom path using the path arg:
assert_that({'a': 1, 'b': 2, 'c': 3}).snapshot(path='my-custom-folder')
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does not equal to on-disk snapshot |
Source code in assertpy2/snapshot.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |