I have functionality written in rust, which I am exposing to python via pyo3. I would like to test that the python functions are correctly exposed and handle python types correctly.
I already have tests in place to validate the actual functional implementation (in rust) and the end-to-end integration (in python).
How can I test the pyo3 python functions in rust?
This is covered in the pyo3 documentation in 3.4 Executing existing Python code - Want to embed Python in Rust with additional modules? and the preceding sections although the documentation does not contain a complete example.
Within
lib.rswhich contains the#[pymodule]and#[pyfunction]you can add:pyo3::append_to_inittab!(py_fizzbuzzo3);makes the python module from the same file available and must be called before initialising the python interpreterlet fizzbuzzo3 = py.import_bound("fizzbuzzo3").expect("Failed to import fizzbuzzo3");then imports the modulelet fizzbuzz = fizzbuzzo3.getattr("fizzbuzz").expect("Failed to get fizzbuzz function");assigns the python function to a variable so that:fizzbuzz.call1((1i32,))can then be used to call the function