Skip to content

Commit 1dc590a

Browse files
author
Xing Han Lu
committed
Move Percy initialization and methods to test_percy_snapshot
1 parent 34f94c5 commit 1dc590a

File tree

2 files changed

+49
-23
lines changed

2 files changed

+49
-23
lines changed

tests/IntegrationTests.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,21 @@
33
import logging
44
import os
55
import multiprocessing
6-
import sys
76
import time
87
import unittest
9-
import percy
108
import threading
119
import platform
1210
import flask
13-
import requests
1411

1512
from selenium import webdriver
1613
from selenium.webdriver.chrome.options import Options
1714

1815

1916
class IntegrationTests(unittest.TestCase):
20-
def percy_snapshot(self, name=''):
21-
if os.environ.get('PERCY_ENABLED', False):
22-
snapshot_name = '{} - {}'.format(name, sys.version_info)
23-
24-
self.percy_runner.snapshot(
25-
name=snapshot_name
26-
)
17+
"""
18+
Percy is not initialized here since some tests that inherits from
19+
IntegrationTests do not need to run Percy tests
20+
"""
2721

2822
@classmethod
2923
def setUpClass(cls):
@@ -36,28 +30,19 @@ def setUpClass(cls):
3630
cls.driver = webdriver.Chrome(options=options)
3731
cls.driver.set_window_size(1280, 1000)
3832

39-
if os.environ.get('PERCY_ENABLED', False):
40-
loader = percy.ResourceLoader(webdriver=cls.driver)
41-
percy_config = percy.Config(default_widths=[1280])
42-
cls.percy_runner = percy.Runner(loader=loader, config=percy_config)
43-
cls.percy_runner.initialize_build()
44-
4533
@classmethod
4634
def tearDownClass(cls):
4735
super(IntegrationTests, cls).tearDownClass()
4836

4937
cls.driver.quit()
50-
if os.environ.get('PERCY_ENABLED', False):
51-
cls.percy_runner.finalize_build()
5238

5339
def setUp(self):
5440
pass
5541

5642
def tearDown(self):
5743
time.sleep(3)
5844
if platform.system() == 'Windows':
59-
requests.get('http://localhost:8050/stop')
60-
sys.exit()
45+
self.driver.get('http://localhost:8050/stop')
6146
else:
6247
self.server_process.terminate()
6348
time.sleep(3)

tests/test_percy_snapshot.py

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import base64
22
import os
3+
import sys
34
import time
45

56
from .IntegrationTests import IntegrationTests
67
import dash
78
import dash_html_components as html
89
import dash_core_components as dcc
10+
import percy
911
from selenium.webdriver.common.by import By
1012
from selenium.webdriver.support.ui import WebDriverWait
1113
from selenium.webdriver.support import expected_conditions as EC
@@ -21,13 +23,23 @@ class Tests(IntegrationTests):
2123
Instead, we use Selenium webdrivers to automatically screenshot each of
2224
the apps being tested in test_usage.py, display them in a simple
2325
Dash app, and use Percy to take a snapshot for CVI.
26+
27+
Here, we extend the setUpClass method from IntegrationTests by adding
28+
percy runner initialization. This is because other classes that inherits
29+
from IntegrationTests do not necessarily need to initialize Percy (since
30+
all they do is save snapshots), and doing so causes Percy to render an
31+
empty build that ends up failing. Therefore, we decide to initialize and
32+
finalize the Percy runner in this class rather than inside
33+
IntegrationTests.
2434
"""
2535

26-
def test_usage(self):
36+
@staticmethod
37+
def create_app(dir_name):
2738
def encode(name):
2839
path = os.path.join(
2940
os.path.dirname(__file__),
3041
'screenshots',
42+
dir_name,
3143
name
3244
)
3345

@@ -60,8 +72,37 @@ def display_image(pathname): # pylint: disable=W0612
6072
name = pathname.replace('/', '')
6173
return html.Img(id=name, src=encode(name))
6274

63-
# Start the app
64-
self.startServer(app)
75+
return app
76+
77+
def percy_snapshot(self, name=''):
78+
if os.environ.get('PERCY_ENABLED', False):
79+
snapshot_name = '{} (Python {}.{}.{})'.format(
80+
name,
81+
sys.version_info.major,
82+
sys.version_info.minor,
83+
sys.version_info.micro,
84+
)
85+
86+
self.percy_runner.snapshot(
87+
name=snapshot_name
88+
)
89+
90+
@classmethod
91+
def setUpClass(cls):
92+
super(Tests, cls).setUpClass()
93+
94+
if os.environ.get('PERCY_ENABLED', False):
95+
loader = percy.ResourceLoader(webdriver=cls.driver)
96+
percy_config = percy.Config(default_widths=[1280])
97+
cls.percy_runner = percy.Runner(loader=loader, config=percy_config)
98+
cls.percy_runner.initialize_build()
99+
100+
@classmethod
101+
def tearDownClass(cls):
102+
super(Tests, cls).tearDownClass()
103+
104+
if os.environ.get('PERCY_ENABLED', False):
105+
cls.percy_runner.finalize_build()
65106

66107
# Find the names of all the screenshots
67108
asset_list = os.listdir(os.path.join(

0 commit comments

Comments
 (0)