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

Experimental new test system #1047

Merged
merged 5 commits into from
Jan 11, 2021
Merged

Experimental new test system #1047

merged 5 commits into from
Jan 11, 2021

Conversation

dalcde
Copy link
Contributor

@dalcde dalcde commented Jan 5, 2021

This introduces a new way of writing tests via the decorator @pytest.run_code. This runs the entire decorated function via selenium.run, and passes on any error.

Nothing in here is intended to be final. For example, there ought to be a better way to expose run_code to the tests. Comments welcome.

This also produces more legible errors, e.g. if I change the code to simply assert False, I get

__________________________________________________ test_bz2[firefox] __________________________________________________
Message: Error: Traceback (most recent call last):
  File "/lib/python3.8/site-packages/pyodide/_base.py", line 105, in eval_code
    eval(compile(mod, "<exec>", mode="exec"), ns, ns)
  File "<exec>", line 5, in <module>
  File "<exec>", line 3, in test_bz2
AssertionError

------------------------------------------------ Captured stdout setup ------------------------------------------------
Spawning webserver at http://127.0.0.1:49829 (see logs in /tmp/tmp26ej3u19/http-server.log)
---------------------------------------------- Captured stdout teardown -----------------------------------------------
Python exception:
Traceback (most recent call last):
  File "/lib/python3.8/site-packages/pyodide/_base.py", line 105, in eval_code
    eval(compile(mod, "<exec>", mode="exec"), ns, ns)
  File "<exec>", line 5, in <module>
  File "<exec>", line 3, in test_bz2
AssertionError

_hiwire_throw_error@http://127.0.0.1:49829/pyodide.asm.js:8:178853
@http://127.0.0.1:49829/pyodide.asm.wasm:wasm-function[378]:0x11316d
@http://127.0.0.1:49829/pyodide.asm.wasm:wasm-function[376]:0x112e70
pyodide/Module.__pyproxy_apply@http://127.0.0.1:49829/pyodide.asm.js:8:2397731
apply@http://127.0.0.1:49829/pyodide.asm.js:8:182091
languagePluginLoader</Module.runPython@http://127.0.0.1:49829/pyodide.js:333:48
@http://127.0.0.1:49829/test.html:4:34
@http://127.0.0.1:49829/test.html:6:8

@dalcde
Copy link
Contributor Author

dalcde commented Jan 5, 2021

mypy is failing due to the horrible hack to expose run_code. An even worse option would be to add it to builtins, but that still doesn't make mypy happy

@dalcde dalcde marked this pull request as draft January 5, 2021 14:11
@dalcde
Copy link
Contributor Author

dalcde commented Jan 5, 2021

New error log:

__________________________________________________ test_bz2[firefox] __________________________________________________
Error running function in pydodie

Traceback (most recent call last):
  File "/src/src/tests/test_bz2.py", line 3, in test_bz2
AssertionError
------------------------------------------------ Captured stdout setup ------------------------------------------------
Spawning webserver at http://127.0.0.1:59799 (see logs in /tmp/tmpfjec_mjy/http-server.log)
---------------------------------------------- Captured stdout teardown -----------------------------------------------
Python exception:
Traceback (most recent call last):
  File "/lib/python3.8/site-packages/pyodide/_base.py", line 105, in eval_code
    eval(compile(mod, "<exec>", mode="exec"), ns, ns)
  File "<exec>", line 12, in <module>
  File "<exec>", line 3, in test_bz2
AssertionError

_hiwire_throw_error@http://127.0.0.1:59799/pyodide.asm.js:8:178853
@http://127.0.0.1:59799/pyodide.asm.wasm:wasm-function[378]:0x11316d
@http://127.0.0.1:59799/pyodide.asm.wasm:wasm-function[376]:0x112e70
pyodide/Module.__pyproxy_apply@http://127.0.0.1:59799/pyodide.asm.js:8:2397731
apply@http://127.0.0.1:59799/pyodide.asm.js:8:182091
languagePluginLoader</Module.runPython@http://127.0.0.1:59799/pyodide.js:333:48
@http://127.0.0.1:59799/test.html:4:34
@http://127.0.0.1:59799/test.html:6:8

The line number and file name are now accurate

@dalcde
Copy link
Contributor Author

dalcde commented Jan 5, 2021

This partially addresses #1031

@hoodmane
Copy link
Member

hoodmane commented Jan 6, 2021

Thanks Dexter! This looks fantastic!
If tests are written like this, it also would fix a couple of other issues: bad editor syntax highlighting for the important part of the code (the Python part), and using lots of short calls to selenium.run and selenium.run_jsmakes it hard to run the test onconsole.html`. This way I should be able to open the browser terminal, type

pyodide.runPython(`
<ctrl+v>
`)

and run the test without having to completely rearrange it.

Maybe we should add a pyodide_py.run_js API for the javascript bits. Or we could use my @jsfunc decorator.

Copy link
Member

@rth rth left a comment

Choose a reason for hiding this comment

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

It would certainly be great to be able to do this!

conftest.py Outdated Show resolved Hide resolved
conftest.py Outdated
@@ -455,6 +458,57 @@ def service_actions():
httpd.serve_forever()


def run_in_pyodide(*args):
# This can be called as @run_in_pyodide or @run_in_pyodide("first_dep",
# "second_dep"). In the former case, run_in_pyodide acts as the decorator.
Copy link
Member

Choose a reason for hiding this comment

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

If it can only be called with the second variant and it allow to simplify that's also fine.

conftest.py Outdated Show resolved Hide resolved
@dalcde
Copy link
Contributor Author

dalcde commented Jan 9, 2021

I have squashed and force-pushed because I rewrote a huge chunk of it and moved it to a different file, so the diffs won't be very useful either way.

This introduces a new way of writing tests via the decorator
@run_in_pyodide. This runs the entire decorated function via
selenium.run, and passes on any error.
@dalcde dalcde marked this pull request as ready for review January 10, 2021 00:11
@dalcde dalcde requested a review from rth January 10, 2021 00:11
Copy link
Member

@rth rth left a comment

Choose a reason for hiding this comment

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

Thanks mostly looks good. Minor comments below.

Also,

  • I'm fairly surprised that using a decorator defining an internal function with a pytest fixture works. Could we find some reference on whether it's expected to work, or if this is relying on some internal implementation detail of pytest which can be broken at any time.
  • just curious would adding @pytest.mark.parametrize, before or after, work (or could be made to work -- though not in in this PR)?

"""
)
assert result == ["load", "loads"]
from tools.testing import run_in_pyodide
Copy link
Member

Choose a reason for hiding this comment

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

Tools is not really a place for python modules. I would rather we put it in pyodide_build. I mean it's called build, but in practice it's anything that shouldn't be included in WASM VM (build, testing, etc). So can also rename it to be more general.

Among other things mypy and pytest are not currently run on the tools folder.

tools/testing.py Outdated
catches the function the decorator is applied to. Otherewise, standalone
and packages are the actual arguments to the decorator.

See docs/testing.md for details on how to use this
Copy link
Member

Choose a reason for hiding this comment

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

Please document parameters in the docstring still and ideally add type annotations.

@dalcde
Copy link
Contributor Author

dalcde commented Jan 10, 2021

I would expect fixtures to work by querying the parameter names, e.g. via inspect. This should work with dynamically generated functions.

Copy link
Member

@rth rth left a comment

Choose a reason for hiding this comment

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

Thanks!

docs/testing.md Outdated Show resolved Hide resolved
docs/testing.md Outdated Show resolved Hide resolved
docs/testing.md Outdated Show resolved Hide resolved
@rth rth merged commit 65a9da0 into pyodide:master Jan 11, 2021
hoodmane pushed a commit to hoodmane/pyodide that referenced this pull request Jan 11, 2021
rth pushed a commit that referenced this pull request Jan 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants