@@ -6,6 +6,10 @@ use pyo3::prelude::*;
6
6
7
7
use crate :: { dump_err, run_until_complete, with_runtime} ;
8
8
9
+ /// Args that should be provided to the test program
10
+ ///
11
+ /// These args are meant to mirror the default test harness's args.
12
+ /// > Currently only `--filter` is supported.
9
13
pub struct Args {
10
14
filter : Option < String > ,
11
15
}
@@ -16,6 +20,10 @@ impl Default for Args {
16
20
}
17
21
}
18
22
23
+ /// Parse the test args from the command line
24
+ ///
25
+ /// This should be called at the start of your test harness to give the CLI some
26
+ /// control over how our tests are run.
19
27
pub fn parse_args ( suite_name : & str ) -> Args {
20
28
let matches = App :: new ( suite_name)
21
29
. arg (
@@ -29,12 +37,14 @@ pub fn parse_args(suite_name: &str) -> Args {
29
37
}
30
38
}
31
39
40
+ /// Wrapper around a test function or future to be passed to the test harness
32
41
pub struct Test {
33
- pub name : String ,
34
- pub task : Pin < Box < dyn Future < Output = PyResult < ( ) > > + Send > > ,
42
+ name : String ,
43
+ task : Pin < Box < dyn Future < Output = PyResult < ( ) > > + Send > > ,
35
44
}
36
45
37
46
impl Test {
47
+ /// Construct a test from a future
38
48
pub fn new_async (
39
49
name : String ,
40
50
fut : impl Future < Output = PyResult < ( ) > > + Send + ' static ,
@@ -45,6 +55,7 @@ impl Test {
45
55
}
46
56
}
47
57
58
+ /// Construct a test from a blocking function (like the traditional `#[test]` attribute)
48
59
pub fn new_sync < F > ( name : String , func : F ) -> Self
49
60
where
50
61
F : FnOnce ( ) -> PyResult < ( ) > + Send + ' static ,
@@ -56,6 +67,7 @@ impl Test {
56
67
}
57
68
}
58
69
70
+ /// Run a sequence of tests while applying any necessary filtering from the `Args`
59
71
pub async fn test_harness ( tests : impl Stream < Item = Test > , args : Args ) -> PyResult < ( ) > {
60
72
tests
61
73
. for_each_concurrent ( Some ( 4 ) , |test| {
@@ -79,6 +91,11 @@ pub async fn test_harness(tests: impl Stream<Item = Test>, args: Args) -> PyResu
79
91
Ok ( ( ) )
80
92
}
81
93
94
+ /// Default main function for the test harness.
95
+ ///
96
+ /// This is meant to perform the necessary initialization for most test cases. If you want
97
+ /// additional control over the initialization (i.e. env_logger initialization), you can use this
98
+ /// function as a template.
82
99
pub fn test_main ( tests : impl Stream < Item = Test > + Send + ' static ) {
83
100
Python :: with_gil ( |py| {
84
101
with_runtime ( py, || {
0 commit comments