Skip to content

Commit

Permalink
Merge pull request #2145 from davidhewitt/merge-pytests
Browse files Browse the repository at this point in the history
pytests: merge benchmark and test crates
  • Loading branch information
davidhewitt committed Feb 5, 2022
2 parents 5dd46e9 + 8b47f4f commit 4305a9a
Show file tree
Hide file tree
Showing 44 changed files with 132 additions and 201 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/bench.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,8 @@ jobs:

- name: Run benchmarks
run: |
cd pytests/pyo3-benchmarks
pip install -r requirements-dev.txt
pip install .
pytest --benchmark-json ../../output.json --benchmark-enable
pip install nox
nox -f pytests/noxfile.py -s bench -- --benchmark-json $(pwd)/output.json
- name: Store benchmark result
uses: rhysd/github-action-benchmark@v1
with:
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ harness = false
[workspace]
members = [
"pyo3-ffi",
"pyo3-build-config",
"pyo3-macros",
"pyo3-macros-backend",
"pytests/pyo3-benchmarks",
"pytests/pyo3-pytests",
"pytests",
"examples",
"xtask"
]
Expand Down
6 changes: 3 additions & 3 deletions Contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ If you want to become familiar with the codebase, see

## Getting started contributing

Please join in with any part of PyO3 which interests you. We use GitHub issues to record all bugs and ideas. Feel free to request an issue to be assigned to you if you want to work on it.
Please join in with any part of PyO3 which interests you. We use GitHub issues to record all bugs and ideas. Feel free to request an issue to be assigned to you if you want to work on it.

You can browse the API of the non-public parts of PyO3 [here](https://pyo3.rs/internal/doc/pyo3/index.html).

Expand Down Expand Up @@ -47,7 +47,7 @@ There are some specific areas of focus where help is currently needed for the do
- Issues requesting documentation improvements are tracked with the [documentation](https://github.com/PyO3/pyo3/issues?q=is%3Aissue+is%3Aopen+label%3Adocumentation) label.
- Not all APIs had docs or examples when they were made. The goal is to have documentation on all PyO3 APIs ([#306](https://github.com/PyO3/pyo3/issues/306)). If you see an API lacking a doc, please write one and open a PR!

You can build the docs (including all features) with
You can build the docs (including all features) with
```cargo +nightly pyo3_doc_scrape```

#### Doctests
Expand Down Expand Up @@ -115,7 +115,7 @@ First, there are Rust-based benchmarks located in the `benches` subdirectory. As

cargo +nightly bench

Second, there is a Python-based benchmark contained in the `pyo3-benchmarks` example. You can read more about it [here](examples/pyo3-benchmarks).
Second, there is a Python-based benchmark contained in the `pytests` subdirectory. You can read more about it [here](pytests).

## Sponsor this project

Expand Down
19 changes: 2 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,22 @@ test: lint test_py

test_py:
@for example in examples/*/noxfile.py; do echo "-- Running nox for $$example --"; nox -f $$example/noxfile.py || exit 1; echo ""; done
@for package in pytests/*/noxfile.py; do echo "-- Running nox for $$package --"; nox -f $$package/noxfile.py || exit 1; echo ""; done
echo "-- Running nox for pytests/noxfile.py --";
nox -f pytests/noxfile.py || exit 1;

fmt_py:
black . --check

fmt_rust:
cargo fmt --all -- --check
for package in pytests/*/; do cargo fmt --manifest-path $$package/Cargo.toml -- --check || exit 1; done

fmt: fmt_rust fmt_py
@true

coverage:
# cargo llvm-cov clean --workspace
# cargo llvm-cov $(COVERAGE_PACKAGES) --no-report
# cargo llvm-cov $(COVERAGE_PACKAGES) --no-report --features abi3
# cargo llvm-cov $(COVERAGE_PACKAGES) --no-report --features $(ALL_ADDITIVE_FEATURES)
# cargo llvm-cov $(COVERAGE_PACKAGES) --no-report --features abi3 $(ALL_ADDITIVE_FEATURES)
bash -c "\
set -a\
source <(cargo llvm-cov show-env)\
make test_py\
"
cargo llvm-cov $(COVERAGE_PACKAGES) --no-run --summary-only


clippy:
cargo clippy --features="$(ALL_ADDITIVE_FEATURES)" --all-targets --workspace -- -Dwarnings
cargo clippy --features="abi3 $(ALL_ADDITIVE_FEATURES)" --all-targets --workspace -- -Dwarnings
for example in examples/*/; do cargo clippy --manifest-path $$example/Cargo.toml -- -Dwarnings || exit 1; done
for package in pytests/*/; do cargo clippy --manifest-path $$package/Cargo.toml -- -Dwarnings || exit 1; done

lint: fmt clippy
@true
Expand Down
4 changes: 2 additions & 2 deletions pytests/pyo3-pytests/Cargo.toml → pytests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ description = "Python-based tests for PyO3"
edition = "2018"

[dependencies]
pyo3 = { path = "../../", features = ["extension-module"] }
pyo3 = { path = "../", features = ["extension-module"] }

[build-dependencies]
pyo3-build-config = { path = "../../pyo3-build-config" }
pyo3-build-config = { path = "../pyo3-build-config" }

[lib]
name = "pyo3_pytests"
Expand Down
File renamed without changes.
38 changes: 31 additions & 7 deletions pytests/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
# PyO3 Python tests
# pyo3-pytests

These crates are a collection of test extension modules built with PyO3. They are all tested using `nox` in PyO3's CI.
An extension module built using PyO3, used to test and benchmark PyO3 from Python.

Below is a brief description of each of these:
## Testing

| Example | Description |
| ------- | ----------- |
| `pyo3-benchmarks` | A project containing some benchmarks of PyO3 functionality called from Python. |
| `pyo3-pytests` | A project containing some tests of PyO3 functionality called from Python. |
This package is intended to be built using `maturin`. Once built, you can run the tests using `pytest`:

```shell
pip install maturin
maturin develop
pytest
```

Alternatively, install nox and run the tests inside an isolated environment:

```shell
nox
```

## Running benchmarks

You can install the module in your Python environment and then run the benchmarks with pytest:

```shell
pip install .
pytest --benchmark-enable
```

Or with nox:

```shell
nox -s bench
```
File renamed without changes.
2 changes: 1 addition & 1 deletion pytests/pyo3-benchmarks/noxfile.py → pytests/noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ def test(session):
def bench(session):
session.install("-rrequirements-dev.txt")
session.install(".")
session.run("pytest", "--benchmark-enable", *session.posargs)
session.run("pytest", "--benchmark-enable", "--benchmark-only", *session.posargs)
16 changes: 0 additions & 16 deletions pytests/pyo3-benchmarks/Cargo.toml

This file was deleted.

3 changes: 0 additions & 3 deletions pytests/pyo3-benchmarks/MANIFEST.in

This file was deleted.

18 changes: 0 additions & 18 deletions pytests/pyo3-benchmarks/README.md

This file was deleted.

4 changes: 0 additions & 4 deletions pytests/pyo3-benchmarks/requirements-dev.txt

This file was deleted.

19 changes: 0 additions & 19 deletions pytests/pyo3-pytests/README.md

This file was deleted.

9 changes: 0 additions & 9 deletions pytests/pyo3-pytests/noxfile.py

This file was deleted.

3 changes: 0 additions & 3 deletions pytests/pyo3-pytests/pyproject.toml

This file was deleted.

4 changes: 0 additions & 4 deletions pytests/pyo3-pytests/requirements-dev.txt

This file was deleted.

34 changes: 0 additions & 34 deletions pytests/pyo3-pytests/src/pyclass_iter.rs

This file was deleted.

File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions pytests/requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
hypothesis>=3.55
pytest>=6.0
pytest-benchmark>=3.4
psutil>=5.6
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 6 additions & 3 deletions pytests/pyo3-pytests/src/lib.rs → pytests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ pub mod misc;
pub mod objstore;
pub mod othermod;
pub mod path;
pub mod pyclass_iter;
pub mod pyclasses;
pub mod pyfunctions;
pub mod subclassing;

#[pymodule]
Expand All @@ -23,7 +24,8 @@ fn pyo3_pytests(py: Python, m: &PyModule) -> PyResult<()> {
m.add_wrapped(wrap_pymodule!(objstore::objstore))?;
m.add_wrapped(wrap_pymodule!(othermod::othermod))?;
m.add_wrapped(wrap_pymodule!(path::path))?;
m.add_wrapped(wrap_pymodule!(pyclass_iter::pyclass_iter))?;
m.add_wrapped(wrap_pymodule!(pyclasses::pyclasses))?;
m.add_wrapped(wrap_pymodule!(pyfunctions::pyfunctions))?;
m.add_wrapped(wrap_pymodule!(subclassing::subclassing))?;

// Inserting to sys.modules allows importing submodules nicely from Python
Expand All @@ -38,7 +40,8 @@ fn pyo3_pytests(py: Python, m: &PyModule) -> PyResult<()> {
sys_modules.set_item("pyo3_pytests.objstore", m.getattr("objstore")?)?;
sys_modules.set_item("pyo3_pytests.othermod", m.getattr("othermod")?)?;
sys_modules.set_item("pyo3_pytests.path", m.getattr("path")?)?;
sys_modules.set_item("pyo3_pytests.pyclass_iter", m.getattr("pyclass_iter")?)?;
sys_modules.set_item("pyo3_pytests.pyclasses", m.getattr("pyclasses")?)?;
sys_modules.set_item("pyo3_pytests.pyfunctions", m.getattr("pyfunctions")?)?;
sys_modules.set_item("pyo3_pytests.subclassing", m.getattr("subclassing")?)?;

Ok(())
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
43 changes: 43 additions & 0 deletions pytests/src/pyclasses.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use pyo3::iter::IterNextOutput;
use pyo3::prelude::*;

#[pyclass]
struct EmptyClass {}

#[pymethods]
impl EmptyClass {
#[new]
fn new() -> Self {
EmptyClass {}
}
}

/// This is for demonstrating how to return a value from __next__
#[pyclass]
struct PyClassIter {
count: usize,
}

#[pymethods]
impl PyClassIter {
#[new]
pub fn new() -> Self {
PyClassIter { count: 0 }
}

fn __next__(&mut self) -> IterNextOutput<usize, &'static str> {
if self.count < 5 {
self.count += 1;
IterNextOutput::Yield(self.count)
} else {
IterNextOutput::Return("Ended")
}
}
}

#[pymodule]
pub fn pyclasses(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_class::<EmptyClass>()?;
m.add_class::<PyClassIter>()?;
Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,13 @@ fn args_kwargs<'a>(
(args, kwargs)
}

#[pyclass]
struct EmptyClass {}

#[pymethods]
impl EmptyClass {
#[new]
fn new() -> Self {
EmptyClass {}
}
}

#[pymodule]
fn pyo3_benchmarks(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
pub fn pyfunctions(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(none, m)?)?;
m.add_function(wrap_pyfunction!(simple, m)?)?;
m.add_function(wrap_pyfunction!(simple_args, m)?)?;
m.add_function(wrap_pyfunction!(simple_kwargs, m)?)?;
m.add_function(wrap_pyfunction!(simple_args_kwargs, m)?)?;
m.add_function(wrap_pyfunction!(args_kwargs, m)?)?;
m.add_class::<EmptyClass>()?;
Ok(())
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 4305a9a

Please sign in to comment.