Commit 2ad135ef authored by Michael Droettboom's avatar Michael Droettboom

Add testing

parent eab618ec
root/
build/pyodide.js
build/pyodide.asm.*
*.bc
*.a
.pytest_cache/
__pycache__
cpython/build
cpython/downloads
cpython/installs
/root/
/build/
numpy/build
numpy/host
numpy/downloads
numpy/.patched
\ No newline at end of file
/cpython/build
/cpython/downloads
/cpython/installs
/numpy/build
/numpy/host
/numpy/downloads
/numpy/.patched
\ No newline at end of file
......@@ -44,6 +44,10 @@ build/pyodide.js: src/pyodide.js
cp $< $@
test: all
py.test test
clean:
rm -fr root
rm build/*
......
......@@ -16,3 +16,14 @@ These instructions were tested on Linux. OSX should be substantively the same.
3. Build this project.
Type `make`.
# Testing
1. Install the following dependencies into the default Python installation:
`pip install pytest selenium`
2. Install [geckodriver](https://github.com/mozilla/geckodriver/releases) somewhere
on your `PATH`.
3. `make test`
"""
Various common utilities for testing.
"""
import pathlib
import pytest
TEST_PATH = pathlib.Path(__file__).parents[0].resolve()
BUILD_PATH = TEST_PATH / '..' / 'build'
class PyodideInited:
def __call__(self, driver):
inited = driver.execute_script("return pyodide.runPython")
return inited is not None
class SeleniumWrapper:
def __init__(self):
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.wait import WebDriverWait
options = Options()
options.add_argument('-headless')
driver = Firefox(executable_path='geckodriver', firefox_options=options)
wait = WebDriverWait(driver, timeout=20)
driver.get((BUILD_PATH / "test.html").as_uri())
wait.until(PyodideInited())
self.driver = driver
@property
def logs(self):
return self.driver.execute_script("return window.logs")
def run(self, code):
return self.driver.execute_script(
'return pyodide.runPython({!r})'.format(code))
@property
def urls(self):
for handle in self.driver.window_handles:
self.driver.switch_to.window(handle)
yield self.driver.current_url
@pytest.fixture
def selenium():
return SeleniumWrapper()
def test_numpy(selenium):
selenium.run("import numpy")
x = selenium.run("numpy.zeros((32, 64))")
assert len(x) == 32
assert all(len(y) == 64 for y in x)
for y in x:
assert all(z == 0 for z in y)
import time
def test_init(selenium):
assert 'Python initialization complete' in selenium.logs
assert len(selenium.driver.window_handles) == 1
def test_webbrowser(selenium):
selenium.run("import antigravity")
time.sleep(2)
assert len(selenium.driver.window_handles) == 2
assert any('xkcd.com' in x for x in selenium.urls)
def test_print(selenium):
selenium.run("print('This should be logged')")
assert 'This should be logged' in selenium.logs
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment