Skip to content

Commit

Permalink
Add a test helper pytest fixture. (#88)
Browse files Browse the repository at this point in the history
* Add a test helper pytest fixture.

A way to have convenience helper functions for developing tests.
@sfmig prefers a fixture.
https://stackoverflow.com/a/42156088/1444054

* Suggestions from PR review.

Co-authored-by: sfmig <33267254+sfmig@users.noreply.github.com>

* Rename 'name' to be more helpful.

---------

Co-authored-by: sfmig <33267254+sfmig@users.noreply.github.com>
  • Loading branch information
samcunliffe and sfmig authored May 18, 2023
1 parent 3c67c43 commit 7852d8e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ coverage.xml
*,cover
.hypothesis/
.pytest_cache/
.test_helper_screenshots/

# mypy
.mypy_cache/
Expand Down
45 changes: 45 additions & 0 deletions tests/test_integration/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,51 @@
from pathlib import Path

import pytest
from dash.testing.composite import DashComposite
from selenium.webdriver.chrome.options import Options


class Helpers:
"""A class to group helpful things when writing tests.
Added as a pytest fixture.
Example
-------
```
def test_my_thing(helpers)
helpers.debug_screenshot()
```
"""

@staticmethod
def debug_screenshot(dash_duo: DashComposite, subdir: str = "") -> None:
"""Save a screenshot of the current state of the testing server.
Useful for debugging and developing tests.
Saves to .test_debug_screenshots or
.test_debug_screenshots/<subdir> if `subdir` is provided.
Parameters
----------
dash_duo : DashComposite
The current dash_duo fixture.
subdir: str, optional
A subdirectory name (to help you find the screenshots.)
"""
screenshots_dirname = Path(".test_helper_screenshots")
if subdir:
screenshots_dirname /= subdir
screenshots_dirname.mkdir(exist_ok=True) # mkdir -p
filename = screenshots_dirname / f"test-{dash_duo.session_id}.png"
dash_duo.driver.save_screenshot(filename.as_posix())


@pytest.fixture
def helpers():
return Helpers


def pytest_setup_options():
options = Options()
options.add_argument("--headless")
Expand Down

0 comments on commit 7852d8e

Please sign in to comment.