-
-
Notifications
You must be signed in to change notification settings - Fork 125
Add unit tests, setup CircleCI and Percy #35
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e9f9d01
:snowflake: Flake 8 on usage.py
44e1a2b
IntegrationTests Class: decrease processes, increase window size, ena…
add4796
Add screenshots directory for tests
0f402e5
Update test requirements.txt
5e9867c
Remove test_render
e585146
Create test_usage.py for all usage apps
c0c79a2
Remove validate-init to run node tests
569aadd
Create test_percy_snapshot.py
98bab11
Update config.yml: Add python 3.7 tests, add supplementary tests
2c2a474
Remove unused requirements
57d31df
Clarify purpose of tests/screenshots
c94b1ac
Add explanation about test cases
372912b
Break down percy.io tests into multiple snapshots
500c647
Pylint on tests
4944030
Disable W0612
bfce626
Fix Pylint R1710
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,7 @@ | |
| # Gradle | ||
| .idea/**/gradle.xml | ||
| .idea/**/libraries | ||
| tests/screenshots/*.png | ||
|
|
||
| # misc | ||
| .DS_Store | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,3 +13,4 @@ selenium | |
| flake8 | ||
| pylint | ||
| pytest-dash>=1.0.1 | ||
| colour==0.1.5 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| This is directory is reserved for screenshots generated by Selenium's webdriver in `test_usage.py`, during the CircleCI builds. | ||
|
|
||
| Please do not add unecessary files to this directory (in fact, the only file should be this `readme.md`), and do not move this file. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import base64 | ||
| import os | ||
| import time | ||
|
|
||
| from .IntegrationTests import IntegrationTests | ||
| import dash | ||
| import dash_html_components as html | ||
| import dash_core_components as dcc | ||
| from selenium.webdriver.common.by import By | ||
| from selenium.webdriver.support.ui import WebDriverWait | ||
| from selenium.webdriver.support import expected_conditions as EC | ||
|
|
||
|
|
||
| class Tests(IntegrationTests): | ||
| """ | ||
| In order to render snapshots, Percy collects the DOM of the project and | ||
| uses a custom rendering method, different from Selenium. Therefore, it | ||
| is unable to render Canvas elements, so can't render Cytoscape charts | ||
| directly. | ||
|
|
||
| Instead, we use Selenium webdrivers to automatically screenshot each of | ||
| the apps being tested in test_usage.py, display them in a simple | ||
| Dash app, and use Percy to take a snapshot for CVI. | ||
| """ | ||
|
|
||
| def test_usage(self): | ||
| def encode(name): | ||
| path = os.path.join( | ||
| os.path.dirname(__file__), | ||
| 'screenshots', | ||
| name | ||
| ) | ||
|
|
||
| with open(path, 'rb') as image_file: | ||
| encoded_string = base64.b64encode(image_file.read()) | ||
| return "data:image/png;base64," + encoded_string.decode('ascii') | ||
|
|
||
| # Define the app | ||
| app = dash.Dash(__name__) | ||
|
Author
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. @chriddyp check out this multi-route app. Here I Assign the url path to return the image it represent. For example, to return "usage.png", you can visit localhost/usage.png. |
||
|
|
||
| app.layout = html.Div([ | ||
| # represents the URL bar, doesn't render anything | ||
| dcc.Location(id='url', refresh=False), | ||
| # content will be rendered in this element | ||
| html.Div(id='page-content') | ||
| ]) | ||
|
|
||
| @app.callback(dash.dependencies.Output('page-content', 'children'), | ||
| [dash.dependencies.Input('url', 'pathname')]) | ||
| def display_image(pathname): # pylint: disable=W0612 | ||
| """ | ||
| Assign the url path to return the image it represent. For example, | ||
| to return "usage.png", you can visit localhost/usage.png. | ||
| :param pathname: name of the screenshot, prefixed with "/" | ||
| :return: An html.Img object containing the base64 encoded image | ||
| """ | ||
| if not pathname or pathname == '/': | ||
| return None | ||
|
|
||
| name = pathname.replace('/', '') | ||
| return html.Img(id=name, src=encode(name)) | ||
|
|
||
| # Start the app | ||
| self.startServer(app) | ||
|
|
||
| # Find the names of all the screenshots | ||
| asset_list = os.listdir(os.path.join( | ||
| os.path.dirname(__file__), | ||
| 'screenshots' | ||
| )) | ||
|
|
||
| # Run Percy | ||
| for image in asset_list: | ||
| if image.endswith('png'): | ||
| self.driver.get('http://localhost:8050/{}'.format(image)) | ||
|
|
||
| WebDriverWait(self.driver, 20).until( | ||
| EC.presence_of_element_located((By.ID, image)) | ||
| ) | ||
|
|
||
| self.percy_snapshot(name=image) | ||
| time.sleep(2) | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,56 @@ | ||
| from pytest_dash.utils import ( | ||
| import_app, | ||
| wait_for_text_to_equal, | ||
| wait_for_element_by_css_selector | ||
| ) | ||
| import os | ||
| import importlib | ||
| from .IntegrationTests import IntegrationTests | ||
| from selenium.webdriver.common.by import By | ||
| from selenium.webdriver.support.ui import WebDriverWait | ||
| from selenium.webdriver.support import expected_conditions as EC | ||
|
|
||
|
|
||
| # Basic test for the component rendering. | ||
| def test_render_component(dash_threaded, selenium): | ||
| # Start a dash app contained in `usage.py` | ||
| # dash_threaded is a fixture by pytest-dash | ||
| # It will load a py file containing a Dash instance named `app` | ||
| # and start it in a thread. | ||
| app = import_app('usage') | ||
| dash_threaded(app) | ||
| class Tests(IntegrationTests): | ||
| def create_usage_test(self, filename): | ||
| app = importlib.import_module(filename).app | ||
|
|
||
| # Get the generated component input with selenium | ||
| # The html input will be a children of the #input dash component | ||
| my_component = wait_for_element_by_css_selector(selenium, '#input > input') | ||
| self.startServer(app) | ||
|
|
||
| assert 'my-value' == my_component.get_attribute('value') | ||
| WebDriverWait(self.driver, 20).until( | ||
| EC.presence_of_element_located((By.ID, "cytoscape")) | ||
| ) | ||
|
|
||
| # Clear the input | ||
| my_component.clear() | ||
| self.driver.save_screenshot(os.path.join( | ||
| os.path.dirname(__file__), | ||
| 'screenshots', | ||
| filename + '.png' | ||
| )) | ||
|
|
||
| # Send keys to the custom input. | ||
| my_component.send_keys('Hello dash') | ||
| def test_usage_advanced(self): | ||
| self.create_usage_test('usage-advanced') | ||
|
|
||
| # Wait for the text to equal, if after the timeout (default 10 seconds) | ||
| # the text is not equal it will fail the test. | ||
| wait_for_text_to_equal(selenium, '#output', 'You have entered Hello dash') | ||
| def test_usage_animated_bfs(self): | ||
| self.create_usage_test('demos.usage-animated-bfs') | ||
|
|
||
| def test_usage_breadthfirst_layout(self): | ||
| self.create_usage_test('demos.usage-breadthfirst-layout') | ||
|
|
||
| def test_usage_compound_nodes(self): | ||
| self.create_usage_test('demos.usage-compound-nodes') | ||
|
|
||
| def test_usage_events(self): | ||
| self.create_usage_test('usage-events') | ||
|
|
||
| def test_usage_elements(self): | ||
| self.create_usage_test('usage-elements') | ||
|
|
||
| def test_usage_pie_style(self): | ||
| self.create_usage_test('demos.usage-pie-style') | ||
|
|
||
| def test_usage_simple(self): | ||
| self.create_usage_test('usage') | ||
|
|
||
| def test_usage_stylesheet(self): | ||
| self.create_usage_test('usage-stylesheet') | ||
|
|
||
| def test_usage_initialisation(self): | ||
| self.create_usage_test('demos.usage-initialisation') | ||
|
|
||
| def test_usage_linkout_example(self): | ||
| self.create_usage_test('demos.usage-linkout-example') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.