Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit a5afef7

Browse files
committed
merge
2 parents b425e49 + eeffadc commit a5afef7

1 file changed

Lines changed: 57 additions & 14 deletions

File tree

runestone/unittest_base.py

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,42 @@
1-
import unittest
1+
# *************************************************************************
2+
# ``unittest_base.py`` - Base classes for RunestoneComponents test fixtures
3+
# *************************************************************************
4+
#
5+
# Imports
6+
# =======
7+
# These are listed in the order prescribed by `PEP 8
8+
# <http://www.python.org/dev/peps/pep-0008/#imports>`_.
9+
#
10+
# Standard library
11+
# ----------------
212
import logging
313
import os
4-
import sys
514
import platform
615
import pytest
716
import signal
817
import time
918
import subprocess
19+
import sys
20+
import unittest
21+
from urllib.request import urlopen
22+
from urllib.error import URLError
23+
24+
# Third-party imports
25+
# -------------------
1026
from selenium import webdriver
1127
from selenium.webdriver.chrome.options import Options
1228
from pyvirtualdisplay import Display
1329
logging.basicConfig(level=logging.WARN)
1430
mylogger = logging.getLogger()
1531

32+
# Local imports
33+
# -------------
34+
# None
35+
1636
# Select an unused port for serving web pages to the test suite.
17-
PORT = '8081'
37+
PORT = "8081"
38+
# Use the localhost for testing.
39+
HOST = "http://127.0.0.1:" + PORT
1840

1941

2042
# Provide access to the currently-active ModuleFixture object.
@@ -23,25 +45,32 @@
2345

2446
# Define `module fixtures <https://docs.python.org/2/library/unittest.html#setupmodule-and-teardownmodule>`_ to build the test Runestone project, run the server, then shut it down when the tests complete.
2547
class ModuleFixture(unittest.TestCase):
26-
def __init__(self,
48+
def __init__(
49+
self,
2750
# The path to the Python module in which the test resides. This provides a simple way to determine the path in which to run runestone build/serve.
2851
module_path,
2952
# True if the sphinx-build process must exit with status of 0 (success)
30-
exit_status_success=True):
53+
exit_status_success=True,
54+
):
3155

3256
super(ModuleFixture, self).__init__()
3357
self.base_path = os.path.dirname(module_path)
3458
self.exit_status_success = exit_status_success
3559
# Windows Compatability
36-
if platform.system() is 'Windows' and self.base_path is '':
37-
self.base_path = '.'
60+
if platform.system() is "Windows" and self.base_path is "":
61+
self.base_path = "."
3862

3963
def setUpModule(self):
4064
# Change to this directory for running Runestone.
4165
self.old_cwd = os.getcwd()
4266
os.chdir(self.base_path)
4367
# Compile the docs. Save the stdout and stderr for examination.
44-
p = subprocess.Popen(['runestone', 'build', '--all'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
68+
p = subprocess.Popen(
69+
["runestone", "build", "--all"],
70+
stdout=subprocess.PIPE,
71+
stderr=subprocess.PIPE,
72+
universal_newlines=True,
73+
)
4574
self.build_stdout_data, self.build_stderr_data = p.communicate()
4675
print(self.build_stdout_data + self.build_stderr_data)
4776
if self.exit_status_success:
@@ -71,17 +100,19 @@ def setUpModule(self):
71100
pytest.exit("Unknown error while trying to kill stale runestone server")
72101

73102
# Run the server. Simply calling ``runestone serve`` fails, since the process killed isn't the actual server, but probably a setuptools-created launcher.
74-
self.runestone_server = subprocess.Popen([sys.executable, '-m', 'runestone', 'serve', '--port', PORT])
103+
self.runestone_server = subprocess.Popen(
104+
[sys.executable, "-m", "runestone", "serve", "--port", PORT]
105+
)
75106

76107
# Testing time in dominated by browser startup/shutdown. So, simply run all tests in a module in a single browser instance to speed things up. See ``RunestoneTestCase.setUp`` for additional code to (mostly) clear the browser between tests.
77108
#
78109
# `PyVirtualDisplay <http://pyvirtualdisplay.readthedocs.io/en/latest/>`_ only runs on X-windows, meaning Linux. Mac seems to have `some support <https://support.apple.com/en-us/HT201341>`_. Windows is out of the question.
79-
if sys.platform.startswith('linux'):
110+
if sys.platform.startswith("linux"):
80111
self.display = Display(visible=0, size=(1280, 1024))
81112
self.display.start()
82113
else:
83114
self.display = None
84-
#self.driver = webdriver.PhantomJS() # use this for Jenkins auto testing
115+
# self.driver = webdriver.PhantomJS() # use this for Jenkins auto testing
85116
options = Options()
86117
options.add_argument("--window-size=1200,800")
87118
options.add_argument("--no-sandbox")
@@ -91,6 +122,17 @@ def setUpModule(self):
91122
global mf
92123
mf = self
93124

125+
# Wait for the webserver to come up.
126+
for tries in range(50):
127+
try:
128+
urlopen(HOST, timeout=5)
129+
except URLError:
130+
# Wait for the server to come up.
131+
time.sleep(0.1)
132+
else:
133+
# The server is up. We're done.
134+
break
135+
94136
def tearDownModule(self):
95137
# Shut down Selenium.
96138
self.driver.quit()
@@ -117,6 +159,7 @@ def tearDownModule(self):
117159
def runTest(self):
118160
pass
119161

162+
120163
# Provide a simple way to instantiante a ModuleFixture in a test module. Typical use:
121164
#
122165
# .. code:: Python
@@ -137,10 +180,10 @@ class RunestoneTestCase(unittest.TestCase):
137180
def setUp(self):
138181
# Use the shared module-wide driver.
139182
self.driver = mf.driver
140-
self.host = 'http://127.0.0.1:' + PORT
183+
self.host = HOST
141184

142185
def tearDown(self):
143186
# Clear as much as possible, to present an almost-fresh instance of a browser for the next test. (Shutting down then starting up a browswer is very slow.)
144-
self.driver.execute_script('window.localStorage.clear();')
145-
self.driver.execute_script('window.sessionStorage.clear();')
187+
self.driver.execute_script("window.localStorage.clear();")
188+
self.driver.execute_script("window.sessionStorage.clear();")
146189
self.driver.delete_all_cookies()

0 commit comments

Comments
 (0)