Skip to content
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

TST Make pyodide-test-runner installable #2742

Merged
merged 16 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Globally register markers
  • Loading branch information
ryanking13 authored Jun 23, 2022
commit 27d53d22b92c0dc3d9048d9a5dc5d1d1be99229a
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ jobs:
name: test
command: |
npm install -g node-fetch@2
pytest -v pyodide-test-runner
cd pyodide-test-runner && pytest -v .

test-js:
<<: *defaults
Expand Down
4 changes: 4 additions & 0 deletions pyodide-test-runner/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pytest_plugins = [
"pyodide_test_runner.hook",
"pyodide_test_runner.fixture",
]
Empty file.
21 changes: 21 additions & 0 deletions pyodide-test-runner/pyodide_test_runner/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@


def pytest_configure(config):

config.addinivalue_line(
"markers",
"skip_refcount_check: Don't run refcount checks",
)

config.addinivalue_line(
"markers",
"skip_pyproxy_check: Don't run pyproxy allocation checks",
)

config.addinivalue_line(
"markers",
"driver_timeout: Set script timeout in WebDriver",
)

config.addinivalue_line(
"markers",
"xfail_browsers: xfail a test in specific browsers",
)

pytest.pyodide_dist_dir = config.getoption("--dist-dir")


Expand Down
126 changes: 33 additions & 93 deletions pyodide-test-runner/pyodide_test_runner/tests/test_decorator.py
Original file line number Diff line number Diff line change
@@ -1,83 +1,53 @@
import asyncio
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please comment why we need to refactor this file? Maybe Hood should review this.

Copy link
Member Author

@ryanking13 ryanking13 Jun 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored this to remove all import pyodide statements. I wanted to make pyodide-test-runner testable without accessing Pyodide source tree.

Most changes in test_decorator file are replacing the selenium mock class with a real selenium class which, I guess, does not harm test coverage.


import pytest
from hypothesis import given, settings
from pyodide_test_runner.decorator import run_in_pyodide
from pyodide_test_runner.hypothesis import any_strategy, std_hypothesis_settings
from pyodide_test_runner.utils import parse_driver_timeout

from pyodide import eval_code_async

@run_in_pyodide
def example_func(selenium):
pass


@run_in_pyodide(_force_assert_rewrites=True)
def example_func1(selenium):
x = 6
y = 7
assert x == y
def test_selenium1(selenium):
import pytest

with pytest.raises(AssertionError, match="assert 6 == 7"):
x = 6
y = 7
assert x == y


run_in_pyodide_alias = run_in_pyodide(_force_assert_rewrites=True)


@run_in_pyodide_alias
def example_func2(selenium):
def test_selenium2(selenium):
import pytest

x = 6
y = 7
assert x == y


run_in_pyodide_inner = run_in_pyodide()
with pytest.raises(AssertionError, match="assert 6 == 7"):
assert x == y


@run_in_pyodide(_force_assert_rewrites=True)
async def async_example_func(selenium):
async def test_selenium3(selenium):
from asyncio import sleep

import pytest

await sleep(0.01)
x = 6
await sleep(0.01)
y = 7
assert x == y


class selenium_mock:
JavascriptException = Exception
browser = "none"

@staticmethod
def load_package(*args, **kwargs):
pass

@staticmethod
def run_async(code: str):
return asyncio.new_event_loop().run_until_complete(eval_code_async(code))


def test_local1():
with pytest.raises(AssertionError, match="assert 6 == 7"):
example_func1(selenium_mock)


def test_local2():
with pytest.raises(AssertionError, match="assert 6 == 7"):
example_func2(selenium_mock)


def test_local3():
with pytest.raises(AssertionError, match="assert 6 == 7"):
async_example_func(selenium_mock)


def test_local_inner_function():
@run_in_pyodide
def inner_function(selenium, x):
assert x == 6
return 7
assert x == y

assert inner_function(selenium_mock, 6) == 7


def test_local_inner_function_closure_error():
def test_inner_function_closure_error(selenium):
x = 6

@run_in_pyodide
Expand All @@ -86,7 +56,7 @@ def inner_function(selenium):
return 7

with pytest.raises(NameError, match="'x' is not defined"):
inner_function(selenium_mock)
inner_function(selenium)


def test_inner_function(selenium):
Expand Down Expand Up @@ -126,25 +96,26 @@ def example_decorator_func(selenium):
pass


def test_local4():
example_decorator_func(selenium_mock)
assert example_decorator_func.dec_info == [
def test_selenium4(selenium_standalone):
example_decorator_func(selenium_standalone)
assert example_decorator_func.dec_info[-3:] == [
("testdec1", "a"),
("testdec2", "b"),
("testdec1", "c"),
]


class selenium_mock_fail_load_package(selenium_mock):
@staticmethod
def load_package(*args, **kwargs):
def test_local_fail_load_package(selenium_standalone):
selenium = selenium_standalone

def _load_package_error(*args, **kwargs):
raise OSError("STOP!")

selenium.load_package = _load_package_error

def test_local_fail_load_package():
exc = None
try:
example_func1(selenium_mock_fail_load_package)
example_func(selenium)
except OSError:
exc = pytest.ExceptionInfo.from_current()

Expand All @@ -161,14 +132,6 @@ def test_local_fail_load_package():
)


def test_selenium(selenium):
with pytest.raises(AssertionError, match="assert 6 == 7"):
example_func1(selenium)

with pytest.raises(AssertionError, match="assert 6 == 7"):
example_func2(selenium)


@run_in_pyodide
def test_trivial1(selenium):
x = 7
Expand Down Expand Up @@ -220,39 +183,16 @@ async def test_run_in_pyodide_async(selenium):
max_examples=25,
)
@run_in_pyodide
def test_hypothesis(selenium, obj):
def test_hypothesis(selenium_standalone, obj):
from pyodide import to_js

to_js(obj)


run_in_pyodide_inner = run_in_pyodide()
run_in_pyodide_alias2 = pytest.mark.driver_timeout(40)(run_in_pyodide_inner)


@run_in_pyodide_alias2
def test_run_in_pyodide_alias(request):
assert parse_driver_timeout(request.node) == 40


@run_in_pyodide
def test_pickle_jsexception(selenium):
import pickle

from pyodide import run_js

pickle.dumps(run_js("new Error('hi');"))


def test_raises_jsexception(selenium):
import pytest

from pyodide import JsException

@run_in_pyodide
def raise_jsexception(selenium):
from pyodide import run_js

run_js("throw new Error('hi');")

with pytest.raises(JsException, match="Error: hi"):
raise_jsexception(selenium)
4 changes: 4 additions & 0 deletions pyodide-test-runner/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ pytest11 =
where = .

[tool:pytest]
asyncio_mode = strict
addopts =
--tb=short
--dist-dir=../dist
6 changes: 1 addition & 5 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@ norecursedirs =
addopts =
--doctest-modules
--ignore="packages/matplotlib/src"
--ignore="pyodide-test-runner"
--tb=short
--dist-dir=dist
markers =
skip_refcount_check: Dont run refcount checks
skip_pyproxy_check: Dont run pyproxy allocation checks
driver_timeout: Set script timeout in WebDriver
xfail_browsers: xfail a test in specific browsers

testpaths =
src
Expand Down
22 changes: 22 additions & 0 deletions src/tests/test_pyodide.py
Original file line number Diff line number Diff line change
Expand Up @@ -1171,3 +1171,25 @@ def test_run_js(selenium):
from js import x

assert x == 77


@run_in_pyodide
def test_pickle_jsexception(selenium):
import pickle

from pyodide import run_js

pickle.dumps(run_js("new Error('hi');"))


def test_raises_jsexception(selenium):
from pyodide import JsException

@run_in_pyodide
def raise_jsexception(selenium):
from pyodide import run_js

run_js("throw new Error('hi');")

with pytest.raises(JsException, match="Error: hi"):
raise_jsexception(selenium)