Skip to main content

"Pytest plugin for testing applications that use Pyodide"

Project description

pytest-pyodide

PyPI Latest Release GHA codecov

Pytest plugin for testing applications that use Pyodide

Installation

pytest-pyodide requires Python 3.10+ and can be installed with

pip install pytest-pyodide

You would also one at least one of the following runtimes,

  • Chrome and chromedriver
  • Firefox and geckodriver
  • Safari and safaridriver
  • node v14+

Usage

  1. First you would need a compatible version of Pyodide. You can download the Pyodide build artifacts from releases with,

    wget https://github.com/pyodide/pyodide/releases/download/0.21.0/pyodide-build-0.21.0.tar.bz2
    tar xjf pyodide-build-0.21.0.tar.bz2
    mv pyodide dist/
    
  2. You can then use the provided fixtures (selenium, selenium_standalone) in tests,

    def test_a(selenium):
        selenium.run("assert 1+1 == 2")   # run Python with Pyodide
    

    which you can run with

    pytest --dist-dir=./dist/
    

run_in_pyodide

Some 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 pytest_pyodide, e.g.

from pytest_pyodide 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 a packages argument to load packages before running the test. For example:

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

You can also use @run_in_pyodide with pytest.mark.parametrize, with hypothesis, etc. @run_in_pyodide MUST be the innermost decorator. Any decorators inside of @run_in_pyodide will be have no effect on the behavior of the test.

from pytest_pyodide import run_in_pyodide
@pytest.mark.parametrize("x", [1, 2, 3])
@run_in_pyodide(packages = ["regex"])
def test_type_of_int(selenium, x):
    assert type(x) is int

These arguments must be picklable. You can also use fixtures as long as the return values of the fixtures are picklable (most commonly, if they are None). As a special case, the function will see the selenium fixture as None inside the test.

It is possible to use run_in_pyodide as an inner function:

def test_inner_function(selenium):
    @run_in_pyodide
    def inner_function(selenium, x):
        assert x == 6
        return 7
    assert inner_function(selenium_mock, 6) == 7

Again both the arguments and return value must be pickleable.

Also, the function will not see closure variables at all:

def test_inner_function_closure(selenium):
    x = 6
    @run_in_pyodide
    def inner_function(selenium):
        assert x == 6
        return 7
    # Raises `NameError: 'x' is not defined`
    assert inner_function(selenium_mock) == 7

Specifying a browser

You can specify a browser runtime using --runtime (--rt) commandline option.

Possible options for --runtime are:

  • node (default)
  • firefox
  • chrome
  • safari
  • all (chrome + firefox + safari + node)
  • host (do not run browser-based tests)
pytest --runtime firefox

Running tests with Playwright (optional)

By default, the tests will be run with Selenium. It is possible to run tests with playwright instead as follows.

First install playwright browsers

python -m playwright install --with-deps

Then use the --runner argument to specify to run tests with playwright.

pytest --runner playwright

Custom test marks

Custeom test marks supported by pytest-pyodide:

pytest.mark.driver_timeout(timeout): Set script timeout in WebDriver. If the test is known to take a long time, you can extend the deadline with this marker.

pytest.mark.xfail_browsers(chrome="why chrome fails"): xfail a test in specific browsers.

Examples

See examples.

License

pytest-pyodide uses the Mozilla Public License Version 2.0.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-pyodide-0.22.1.tar.gz (116.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_pyodide-0.22.1-py3-none-any.whl (19.2 kB view details)

Uploaded Python 3

File details

Details for the file pytest-pyodide-0.22.1.tar.gz.

File metadata

  • Download URL: pytest-pyodide-0.22.1.tar.gz
  • Upload date:
  • Size: 116.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for pytest-pyodide-0.22.1.tar.gz
Algorithm Hash digest
SHA256 26e2256aed6d00cb8016bbf2f682176bc1cd97c1961499ac84fef77fb64d85cd
MD5 f1e28cf278d4cffb1d9649a7746baca3
BLAKE2b-256 c3a58c35ff7f7ab53899f45a21f965b4cc8694055216560b1693f9be9f5775e5

See more details on using hashes here.

File details

Details for the file pytest_pyodide-0.22.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_pyodide-0.22.1-py3-none-any.whl
Algorithm Hash digest
SHA256 880ac8a51da145fe3b9cf324332149895bc294619ece08e34104fddc56daed20
MD5 7805a94400a592e857cc5fbdb6cb05ba
BLAKE2b-256 c7b4d65f72aab5ffdc4adfcb31d523fef59f86c9395091bfd54115be04818d14

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page