pyo3/tests/common.rs

25 lines
821 B
Rust
Raw Normal View History

2018-09-03 18:50:18 +00:00
//! Useful tips for writing tests:
//! - Tests are run in parallel; There's still a race condition in test_owned with some other test
//! - You need to use flush=True to get any output from print
#[macro_export]
macro_rules! py_assert {
($py:expr, $val:ident, $assertion:expr) => {
2019-06-13 09:09:17 +00:00
pyo3::py_run!($py, $val, concat!("assert ", $assertion))
};
}
#[macro_export]
macro_rules! py_expect_exception {
($py:expr, $val:ident, $code:expr, $err:ident) => {{
use pyo3::types::IntoPyDict;
let d = [(stringify!($val), &$val)].into_py_dict($py);
let res = $py.run($code, None, Some(d));
let err = res.unwrap_err();
2018-09-21 21:32:48 +00:00
if !err.matches($py, $py.get_type::<pyo3::exceptions::$err>()) {
2020-03-06 05:01:27 +00:00
panic!("Expected {} but got {:?}", stringify!($err), err)
}
}};
}