pyodide/docs/development/testing.md

3.4 KiB

(testing)=

Testing and benchmarking

Testing

Running the Python test suite

You can use either Selenium or Playwright to run the pytest suite of tests.

Install the following dependencies into the default Python installation:

pip install pytest pytest-instafail pytest-httpserver

There are 3 test locations that are collected by pytest,

  • src/tests/: general Pyodide tests and tests running the CPython test suite
  • pyodide-build/pyodide_build/tests/: tests related to Pyodide build system (do not require selenium or playwright to run)
  • packages/*/test_*: package specific tests.

Selenium (default)

pip install selenium

Install geckodriver and chromedriver and check that they are in your PATH.

From the root directory of Pyodide, type on the command line:

pytest

Playwright

pip install playwright && python -m playwright install

From the root directory of Pyodide, type on the command line:

pytest --runner playwright

Running the JavaScript test suite

To run tests on the JavaScript Pyodide package using Mocha, run the following commands,

cd src/js
npm test

To check TypeScript type definitions run,

npx tsd

Manual interactive testing

To run tests manually:

  1. Build Pyodide, perhaps in the docker image

  2. From outside of the docker image, cd into the dist directory and run python -m http.server.

  3. Once the webserver is running, simple interactive testing can be run by visiting the URL: http://localhost:<PORT>/console.html. It's recommended to use pyodide.runPython in the browser console rather than using the repl.

Benchmarking

To run common benchmarks to understand Pyodide's performance, begin by installing the same prerequisites as for testing. Then run:

PYODIDE_PACKAGES="numpy,matplotlib" make benchmark

Linting

We lint with pre-commit.

Python is linted with flake8, black and mypy. JavaScript, markdown, yaml, and html are linted with prettier. C is linted with clang-format.

To lint the code, run:

pre-commit run -a

You can have the linter automatically run whenever you commit by running

pip install pre-commit
pre-commit install

and this can later be disabled with

pre-commit uninstall

If you don't lint your code, certain lint errors will be fixed automatically by pre-commit.ci which will push fixes to your branch. If you want to push more commits, you will either have to pull in the remote changes or force push.

Testing framework

run_in_pyodide

Many tests simply involve running a chunk of code in Pyodide and ensuring it doesn't error. In this case, one can use the run_in_pyodide decorate from pyodide_test_runner.decorator, e.g.

from pyodide_test_runner import run_in_pyodide

@run_in_pyodide
def test_add(selenium):
    assert 1 + 1 == 2

In this case, the body of the function will automatically be run in Pyodide. The decorator can also be called with arguments. The packages argument lists packages to load before running the test. For example,

from pyodide_test_runner import run_in_pyodide

@run_in_pyodide(packages = ["regex"])
def test_regex(selenium_standalone):
    import regex
    assert regex.search("o", "foo").end() == 2