Skip to content

Commit dc54edc

Browse files
committed
Add page for creation of non-Python test libraries
1 parent 0ba6765 commit dc54edc

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
sidebar_position: 0
3+
sidebar_label: Create Non-Python Libraries
4+
title: Non-Python Libraries
5+
---
6+
7+
### Rust
8+
9+
Create a dynamic system library (crate_type = "cdylib") from the following source code.
10+
11+
```rust
12+
#![allow(non_snake_case)]
13+
14+
use std::collections::HashMap;
15+
16+
use pyo3::prelude::*;
17+
18+
#[pyfunction]
19+
fn sum_as_string(a: i32, b: i32) -> PyResult<String> {
20+
Ok((a + b).to_string())
21+
}
22+
23+
#[pyfunction]
24+
fn join_strings(a: Vec<String>) -> PyResult<String> {
25+
Ok(a.join(","))
26+
}
27+
28+
#[pyfunction]
29+
fn sum_values(a: HashMap<String, i32>) -> PyResult<i32> {
30+
let mut values_sum = 0;
31+
for (_key, value) in &a {
32+
values_sum += value;
33+
}
34+
Ok(values_sum)
35+
}
36+
37+
#[pymodule]
38+
fn RustyLibrary(_py: Python, m: &PyModule) -> PyResult<()> {
39+
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
40+
m.add_function(wrap_pyfunction!(join_strings, m)?)?;
41+
m.add_function(wrap_pyfunction!(sum_values, m)?)?;
42+
Ok(())
43+
}
44+
```
45+
46+
The library can then be used as follows.
47+
48+
```robotframework
49+
*** Settings ***
50+
Library RustyLibrary
51+
52+
*** Test Cases ***
53+
Integer Argument Conversion Test
54+
${x} = Sum As String ${5} ${20}
55+
Should Be Equal ${x} 25
56+
57+
List Argument Conversion Test
58+
@{MY_LIST} = Create List spam eggs
59+
60+
${y} = Join Strings ${MY_LIST}
61+
Should Be Equal ${y} spam,eggs
62+
63+
Dictionary Argument Conversion Test
64+
&{MY_DICT} = Create Dictionary spam ${11} eggs ${22}
65+
66+
${z} = Sum Values ${MY_DICT}
67+
Should Be Equal ${z} ${33}
68+
```
69+
70+
A complete working example that includes all build files (e.g. Cargo.toml) can be found on [GitHub / mneiferbag / robot-python-test-library](https://github.com/mneiferbag/robot-rust-test-library).

0 commit comments

Comments
 (0)