pyodide/docs/development/testing.md

118 lines
2.9 KiB
Markdown
Raw Normal View History

(testing)=
# Testing and benchmarking
## Testing
### Requirements
Install the following dependencies into the default Python installation:
```bash
pip install pytest selenium pytest-instafail pytest-httpserver
```
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
To run the pytest suite of tests, from the root directory of Pyodide, type on the command line:
```bash
pytest
```
2021-07-06 08:48:35 +00:00
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 to run)
- `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,
```sh
2021-07-06 08:48:35 +00:00
cd src/js
npm test
```
To check TypeScript type definitions run,
```sh
2021-07-06 08:48:35 +00:00
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:
```bash
PYODIDE_PACKAGES="numpy,matplotlib" make benchmark
```
## Linting
Python is linted with `flake8`, `black` and `mypy`.
JavaScript is linted with `prettier`.
C is linted with `clang-format`.
To lint the code, run:
```bash
make lint
```
2021-01-11 17:25:55 +00:00
## Testing framework
### run_in_pyodide
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
`pyodide_build.testing`, e.g.
2021-01-11 17:25:55 +00:00
```python
from pyodide_build.testing import run_in_pyodide
@run_in_pyodide
def test_add():
assert 1 + 1 == 2
```
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-01-11 17:25:55 +00:00
```python
from pyodide_build.testing import run_in_pyodide
@run_in_pyodide(standalone = True, packages = ["regex"])
def test_regex():
import regex
assert regex.search("o", "foo").end() == 2
```