Skip to main content

Dash snapshot testing package

Project description


# dash-snapshot-testing
Use snapshot testing, inspired by Jest snapshot testing, to test [Dash][] components.

See the project [README](https://github.com/StratoDem/dash-snapshot-testing/blob/master/README.md) for more detailed information.

## Inspiration
Testing a long HTML component output for a Dash application is difficult.
It typically requires hardcoding data or setting up a dummy database.
Using snapshot tests that JSON serialize the Dash component output provide another
easy testing layer to ensure that code refactors/changes do not change the
output unexpectedly.

To learn more about snapshot testing in general, see a much more elaborate explanation from the [Facebook Jest site](https://facebook.github.io/jest/docs/en/snapshot-testing.html)

## Installation and usage
```bash
$ pip install dash-snapshot-testing
```

```python
import dash_html_components as html

from dash_snapshot_testing.snapshot_test import DashSnapshotTestCase


class MyUnitTestCase(DashSnapshotTestCase):
def test_component(self):
my_component = html.Div([html.P('wow'), html.Span('this works')], id='test-id')

self.assertSnapshotEqual(my_component, 'my-test-unique-id')
```

This outputs/checks this JSON at `__snapshots__/MyUnitTestCase-my-test-unique-id.json`:
```json
{
"type": "Div",
"props": {
"id": "test-id",
"children": [
{
"type": "P",
"props": {"children": "wow"},
"namespace": "dash_html_components"
},
{
"type": "Span",
"props": {"children": "this works"},
"namespace": "dash_html_components"
}
]
},
"namespace": "dash_html_components"
}
```

### Setting a custom `snapshots_dir` for the class
```python
class MyOtherUnitTestCase(DashSnapshotTestCase):
snapshots_dir = '__snapshots_2__'

def test_component(self):
my_component = html.Div([html.P('wow'), html.Span('another one')], id='test-id')

self.assertSnapshotEqual(my_component, 'my-test-unique-id')
```

This outputs/checks this JSON at `__snapshots_2__/MyOtherUnitTestCase-my-test-unique-id.json`:
```json
{
"type": "Div",
"props": {
"id": "test-id",
"children": [
{
"type": "P",
"props": {"children": "wow"},
"namespace": "dash_html_components"
},
{
"type": "Span",
"props": {"children": "another one"},
"namespace": "dash_html_components"
}
]
},
"namespace": "dash_html_components"
}
```

### Overwriting snapshots
To overwrite pre-existing snapshots, [like in Jest](https://facebook.github.io/jest/docs/en/snapshot-testing.html#updating-snapshots), set an environment variable as `UPDATE_DASH_SNAPSHOTS=TRUE`:
```bash
# This will run and make new snapshots
> UPDATE_DASH_SNAPSHOTS=TRUE python -m unittest my_test_module
# This will run against the previous snapshots
> python -m unittest my_test_module
```

### How this works
At its core, this `unittest.TestCase` compares a JSON-serialized Dash component
against a previously stored JSON-serialized Dash component, and checks if the `dict`
objects from `json.loads` are equivalent using `assertEqual`.

[Dash]: https://github.com/plotly/dash


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dash-snapshot-testing-1.2.2.tar.gz (4.2 kB view details)

Uploaded Source

Built Distributions

dash_snapshot_testing-1.2.2-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

dash_snapshot_testing-1.2.2-py2.py3-none-any.whl (6.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file dash-snapshot-testing-1.2.2.tar.gz.

File metadata

File hashes

Hashes for dash-snapshot-testing-1.2.2.tar.gz
Algorithm Hash digest
SHA256 16118087b57aba66053fd98c493161bc50a75ffcf9d3fa4747d5bf9e36be3585
MD5 ed5582f03eb7e2d10e4a04a255061c71
BLAKE2b-256 7af23b0b0a7c35074be081f65c771d4e6568f14d7b0999208de3aa34e06a3de9

See more details on using hashes here.

File details

Details for the file dash_snapshot_testing-1.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for dash_snapshot_testing-1.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 243fa0ee62da8a6ec5de5f8a842c50f8943bfa5dc0994080053e2352984d2be0
MD5 c69640df8fc76f0e23b10bca0779bdc4
BLAKE2b-256 769394fbcdd706489df30937e7caf0f2b089f75d69f31bba3bb0e144a4e0c934

See more details on using hashes here.

File details

Details for the file dash_snapshot_testing-1.2.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for dash_snapshot_testing-1.2.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b8b3a4e7399ac85cd15a458b920a5f98f1ea2cbc00c844a410ad122226db9005
MD5 3212a4301274df25ced438fb5dc95001
BLAKE2b-256 6aa48adbf6a17893dbfc84a6c1f821f7ebd39cd4bb2f2cf03369fab1ca6354cb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page