-
Notifications
You must be signed in to change notification settings - Fork 827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Snapshot testing #793
Snapshot testing #793
Changes from all commits
e981e3b
74764ae
2f01453
e8acba5
d619dae
13b27e1
7428a16
0cd5c0a
c9cdb97
a871e27
c82a143
8f64e36
4a4eeb5
cdd2c01
8639981
0978eb5
e3a917e
5e1c47b
29da6ba
2bed87f
0ce222d
c54c844
d2f8ad5
7e72cf1
26fb890
8e09c02
17f2e3e
da05b66
9606d43
49abcba
56b7ec7
6a63ffb
93fab61
4a46257
1a65895
644922c
bc16e16
53ac7b4
04a513f
f88a13f
8db7a07
cbe91fd
b5540ca
1486e14
ee0eed9
cc352fa
9e2500a
0c0f5cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,3 +113,6 @@ venv.bak/ | |
|
||
# mypy | ||
.mypy_cache/ | ||
|
||
# Snapshot testing report output directory | ||
tests/snapshot_tests/output |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ repos: | |
hooks: | ||
- id: black | ||
exclude: ^tests/ | ||
exclude: ^tests/snapshot_tests |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ test: | |
pytest --cov-report term-missing --cov=textual tests/ -vv | ||
unit-test: | ||
pytest --cov-report term-missing --cov=textual tests/ -vv -m "not integration_test" | ||
test-snapshot-update: | ||
pytest --cov-report term-missing --cov=textual tests/ -vv --snapshot-update | ||
Comment on lines
+5
to
+6
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this runs all tests, it'll update all snapshots. |
||
typecheck: | ||
mypy src/textual | ||
format: | ||
|
@@ -14,4 +16,3 @@ docs-build: | |
mkdocs build | ||
docs-deploy: | ||
mkdocs gh-deploy | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Snapshot Testing | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Internal docs |
||
|
||
|
||
## What is snapshot testing? | ||
|
||
Some tests that run for Textual are snapshot tests. | ||
When you first run a snapshot test, a screenshot of an app is taken and saved to disk. | ||
Next time you run it, another screenshot is taken and compared with the original one. | ||
|
||
If the screenshots don't match, it means something has changed. | ||
It's up to you to tell the test system whether that change is expected or not. | ||
|
||
This allows us to easily catch regressions in how Textual outputs to the terminal. | ||
|
||
Snapshot tests run alongside normal unit tests. | ||
|
||
## How do I write a snapshot test? | ||
|
||
1. Inject the `snap_compare` fixture into your test. | ||
2. Pass in the path to the file which contains the Textual app. | ||
|
||
```python | ||
def test_grid_layout_basic_overflow(snap_compare): | ||
assert snap_compare("docs/examples/guide/layout/grid_layout2.py") | ||
``` | ||
|
||
`snap_compare` can take additional arguments such as `press`, which allows | ||
you to simulate key presses etc. | ||
See the signature of `snap_compare` for more info. | ||
|
||
## A snapshot test failed, what do I do? | ||
|
||
When a snapshot test fails, a report will be created on your machine, and you | ||
can use this report to visually compare the output from your test with the historical output for that test. | ||
|
||
This report will be visible at the bottom of the terminal after the `pytest` session completes, | ||
or, if running in CI, it will be available as an artifact attached to the GitHub Actions summary. | ||
|
||
If you're happy that the new output of the app is correct, you can run `pytest` with the | ||
`--snapshot-update` flag. This flag will update the snapshots for any test that is executed in the run, | ||
so to update a snapshot for a single test, run only that test. | ||
|
||
With your snapshot on disk updated to match the new output, running the test again should result in a pass. |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,7 @@ nanoid = "^2.0.0" | |
dev = ["aiohttp", "click", "msgpack"] | ||
|
||
[tool.poetry.dev-dependencies] | ||
pytest = "^6.2.3" | ||
pytest = "^7.1.3" | ||
black = "^22.3.0" | ||
mypy = "^0.950" | ||
pytest-cov = "^2.12.1" | ||
|
@@ -48,13 +48,15 @@ pre-commit = "^2.13.0" | |
pytest-aiohttp = "^1.0.4" | ||
time-machine = "^2.6.0" | ||
Jinja2 = "<3.1.0" | ||
syrupy = "^3.0.0" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the snapshot testing library we're wrapping which handles associating pytest test functions with snapshots on disk. |
||
|
||
[tool.black] | ||
includes = "src" | ||
|
||
[tool.pytest.ini_options] | ||
asyncio_mode = "auto" | ||
testpaths = ["tests"] | ||
addopts = "--strict-markers" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO this should be the default - if you use an undefined marker it'll fail fast - good for catching typos. |
||
markers = [ | ||
"integration_test: marks tests as slow integration tests (deselect with '-m \"not integration_test\"')", | ||
] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This runs on both failure and success. On success, there's no snapshot report, but the step still succeeds.