Macro pyo3_testing::with_py_raises

source ·
with_py_raises!() { /* proc-macro */ }
Expand description

A proc macro to implement the equivalent of pytest’s with raises context manager.

Use like this: with_py_raises(ExpectedErrType, {code block which should raise error })

§Note:

  1. The ExpectedErrType must be in scope when calling the macro and must implement std::from::From<E> for PyErr
  2. The code inside the block must be valid rust which returns a PyResult<T>. Currently it is not possible to use the autogenerated call macros provided by #[pyo3test]pyo3test. If you would like to see that feature, please let me know via github
  3. The code will panic! if the incorrect error, or no error, is returned - this is designed for use in tests, where panicing is the acceptable and required behaviour

§Example usage:

use pyo3::exceptions::PyTypeError
#[pyo3test]
#[allow(unused_macros)]
#[pyo3import(py_adders: from adders import addone)]
fn test_raises() {
    //can't use `let result =` or `addone!()` here as they don't return a `Result`
    with_py_raises!(PyTypeError, { addone.call1(("4",)) });
}