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
Built Distributions
File details
Details for the file dash-snapshot-testing-1.2.1.tar.gz
.
File metadata
- Download URL: dash-snapshot-testing-1.2.1.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b2ef2d85c724bc340aeb18aff52f11595ee846bf1f713ea1220049488c253ff |
|
MD5 | d0f0a3d488264e883c630fa6db80334a |
|
BLAKE2b-256 | e72a446655e3329b5d2af08dacd817cf7c8bdd7963729bf8db14d5857b6c952e |
File details
Details for the file dash_snapshot_testing-1.2.1-py3-none-any.whl
.
File metadata
- Download URL: dash_snapshot_testing-1.2.1-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e87c2139743227a5b139bc4c8ab40bf6410ae6504d746fd1c94f34cabeb6bb7a |
|
MD5 | cd48ab619d4c279ba659a88c9a4c193d |
|
BLAKE2b-256 | 03adc641a7dfa2dadc2e8b652073fe6a1f10291a68bdcb6ee360bd83037fff74 |
File details
Details for the file dash_snapshot_testing-1.2.1-py2.py3-none-any.whl
.
File metadata
- Download URL: dash_snapshot_testing-1.2.1-py2.py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 921edcacf56de096dbfbf56639ba37cc4e23462264be3ba6823c398459903956 |
|
MD5 | 28b8ddc346c7d875951c64d2f530869e |
|
BLAKE2b-256 | ae1fb1d2e836f59cd7775a2ef7bd0de742c4a35c8e771a4504ebf311b33dc63e |