2020-10-30 20:09:25 +00:00
|
|
|
(testing)=
|
2021-07-26 23:00:27 +00:00
|
|
|
|
2020-05-19 16:14:56 +00:00
|
|
|
# Testing and benchmarking
|
|
|
|
|
|
|
|
## Testing
|
|
|
|
|
|
|
|
### Requirements
|
|
|
|
|
|
|
|
Install the following dependencies into the default Python installation:
|
|
|
|
|
|
|
|
```bash
|
2020-12-06 13:08:22 +00:00
|
|
|
pip install pytest selenium pytest-instafail pytest-httpserver
|
2020-05-19 16:14:56 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
Install [geckodriver](https://github.com/mozilla/geckodriver/releases) and
|
|
|
|
[chromedriver](https://sites.google.com/a/chromium.org/chromedriver/downloads)
|
|
|
|
and check that they are in your `PATH`.
|
|
|
|
|
2021-07-06 08:48:35 +00:00
|
|
|
### Running the Python test suite
|
2020-05-19 16:14:56 +00:00
|
|
|
|
2022-04-09 20:41:10 +00:00
|
|
|
To run the pytest suite of tests, from the root directory of Pyodide, type on the command line:
|
2020-05-19 16:14:56 +00:00
|
|
|
|
|
|
|
```bash
|
2021-11-21 17:26:33 +00:00
|
|
|
pytest
|
2020-05-19 16:14:56 +00:00
|
|
|
```
|
|
|
|
|
2021-07-06 08:48:35 +00:00
|
|
|
There are 3 test locations that are collected by pytest,
|
2021-07-26 23:00:27 +00:00
|
|
|
|
2021-03-03 08:11:50 +00:00
|
|
|
- `src/tests/`: general Pyodide tests and tests running the CPython test suite
|
2021-05-03 18:51:11 +00:00
|
|
|
- `pyodide-build/pyodide_build/tests/`: tests related to Pyodide build system
|
|
|
|
(do not require selenium to run)
|
2020-11-09 12:28:02 +00:00
|
|
|
- `packages/*/test_*`: package specific tests.
|
|
|
|
|
2021-07-06 08:48:35 +00:00
|
|
|
### Running the JavaScript test suite
|
|
|
|
|
|
|
|
To run tests on the JavaScript Pyodide package using Mocha, run the following commands,
|
2021-07-26 23:00:27 +00:00
|
|
|
|
2022-04-11 23:01:40 +00:00
|
|
|
```sh
|
2021-07-06 08:48:35 +00:00
|
|
|
cd src/js
|
|
|
|
npm test
|
|
|
|
```
|
|
|
|
|
|
|
|
To check TypeScript type definitions run,
|
2021-07-26 23:00:27 +00:00
|
|
|
|
2022-04-11 23:01:40 +00:00
|
|
|
```sh
|
2021-07-06 08:48:35 +00:00
|
|
|
npx tsd
|
|
|
|
```
|
|
|
|
|
2020-05-19 16:14:56 +00:00
|
|
|
### Manual interactive testing
|
|
|
|
|
2022-04-11 23:01:40 +00:00
|
|
|
To run tests manually:
|
2020-05-19 16:14:56 +00:00
|
|
|
|
2022-04-11 23:01:40 +00:00
|
|
|
1. Build Pyodide, perhaps in the docker image
|
2020-05-19 16:14:56 +00:00
|
|
|
|
2022-04-11 23:01:40 +00:00
|
|
|
2. From outside of the docker image, `cd` into the `dist` directory and run
|
|
|
|
`python -m http.server`.
|
2020-05-19 16:14:56 +00:00
|
|
|
|
2022-04-11 23:01:40 +00:00
|
|
|
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.
|
2020-05-19 16:14:56 +00:00
|
|
|
|
|
|
|
## Benchmarking
|
|
|
|
|
|
|
|
To run common benchmarks to understand Pyodide's performance, begin by
|
|
|
|
installing the same prerequisites as for testing. Then run:
|
|
|
|
|
|
|
|
```bash
|
2022-03-02 18:11:36 +00:00
|
|
|
PYODIDE_PACKAGES="numpy,matplotlib" make benchmark
|
2020-05-19 16:14:56 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## Linting
|
|
|
|
|
2022-01-04 15:46:00 +00:00
|
|
|
Python is linted with `flake8`, `black` and `mypy`.
|
|
|
|
JavaScript is linted with `prettier`.
|
|
|
|
C is linted with `clang-format`.
|
2020-05-19 16:14:56 +00:00
|
|
|
|
|
|
|
To lint the code, run:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
make lint
|
|
|
|
```
|
2021-01-11 17:25:55 +00:00
|
|
|
|
|
|
|
## Testing framework
|
|
|
|
|
|
|
|
### run_in_pyodide
|
2021-07-26 23:00:27 +00:00
|
|
|
|
2021-03-03 08:11:50 +00:00
|
|
|
Many tests simply involve running a chunk of code in Pyodide and ensuring it
|
2021-01-11 17:25:55 +00:00
|
|
|
doesn't error. In this case, one can use the `run_in_pyodide` decorate from
|
2022-05-08 07:52:08 +00:00
|
|
|
`pyodide_test_runner.decorator`, e.g.
|
2021-01-11 17:25:55 +00:00
|
|
|
|
|
|
|
```python
|
2022-05-08 07:52:08 +00:00
|
|
|
from pyodide_test_runner import run_in_pyodide
|
2021-01-11 17:25:55 +00:00
|
|
|
|
|
|
|
@run_in_pyodide
|
|
|
|
def test_add():
|
|
|
|
assert 1 + 1 == 2
|
|
|
|
```
|
2021-07-26 23:00:27 +00:00
|
|
|
|
2021-03-20 18:56:10 +00:00
|
|
|
In this case, the body of the function will automatically be run in Pyodide.
|
2021-01-11 17:25:55 +00:00
|
|
|
The decorator can also be called with arguments. It has two configuration
|
|
|
|
options --- standalone and packages.
|
|
|
|
|
|
|
|
Setting `standalone = True` starts a standalone browser session to run the test
|
|
|
|
(the session is shared between tests by default). This is useful for testing
|
|
|
|
things like package loading.
|
|
|
|
|
|
|
|
The `packages` option lists packages to load before running the test. For
|
|
|
|
example,
|
2021-07-26 23:00:27 +00:00
|
|
|
|
2021-01-11 17:25:55 +00:00
|
|
|
```python
|
2022-05-08 07:52:08 +00:00
|
|
|
from pyodide_test_runner import run_in_pyodide
|
2021-01-11 17:25:55 +00:00
|
|
|
|
|
|
|
@run_in_pyodide(standalone = True, packages = ["regex"])
|
|
|
|
def test_regex():
|
|
|
|
import regex
|
|
|
|
assert regex.search("o", "foo").end() == 2
|
|
|
|
```
|