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:
- The
ExpectedErrTypemust be in scope when calling the macro and must implementstd::from::From<E> for PyErr - 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 - Add
#[allow(unused_macros)]to disable the warning that you have imported a python function but not called the associated macro. - 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;
use pyo3_testing::{pyo3test, with_py_raises};
#[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",)) });
}