Skip to content

Commit c2ce5df

Browse files
author
Andrew J Westlake
committed
added support for test filter similar to default test harness
1 parent 42a71c6 commit c2ce5df

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ path = "pytests/test_asyncio.rs"
1212
harness = false
1313

1414
[dependencies]
15+
clap = "2.33"
1516
futures = "0.3"
1617
lazy_static = "1.4"
1718
once_cell = "1.5"
18-
pyo3 = { git = "https://github.com/awestlake87/pyo3", branch = "finalization-bugfix" }
19+
pyo3 = { git = "https://github.com/awestlake87/pyo3", rev = "1ac57f7a3ca663fa4a18f8ff13ffebc39e9bd9e1" }
1920
tokio = { version = "0.3", features = ["full"] }

pytests/test_asyncio.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{future::Future, thread, time::Duration};
33
use futures::stream::{self};
44
use pyo3::prelude::*;
55

6-
use pyo3_asyncio::test::{test_harness, Test};
6+
use pyo3_asyncio::test::{parse_args, test_harness, Test};
77

88
fn dump_err<'p>(py: Python<'p>) -> impl FnOnce(PyErr) + 'p {
99
move |e| {
@@ -43,14 +43,19 @@ fn test_blocking_sleep() -> PyResult<()> {
4343
}
4444

4545
fn py_main(py: Python) -> PyResult<()> {
46+
let args = parse_args("Pyo3 Asyncio Test Suite");
47+
4648
pyo3_asyncio::try_init(py)?;
4749

4850
pyo3_asyncio::run_until_complete(
4951
py,
50-
test_harness(stream::iter(vec![
51-
Test::new_async("test_async_sleep".into(), test_async_sleep(py)?),
52-
Test::new_sync("test_blocking_sleep".into(), test_blocking_sleep),
53-
])),
52+
test_harness(
53+
stream::iter(vec![
54+
Test::new_async("test_async_sleep".into(), test_async_sleep(py)?),
55+
Test::new_sync("test_blocking_sleep".into(), test_blocking_sleep),
56+
]),
57+
args,
58+
),
5459
)?;
5560

5661
Ok(())

src/test.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,32 @@
11
use std::{future::Future, pin::Pin};
22

3+
use clap::{App, Arg};
34
use futures::stream::{Stream, StreamExt};
45
use pyo3::prelude::*;
56

7+
pub struct Args {
8+
filter: Option<String>,
9+
}
10+
11+
impl Default for Args {
12+
fn default() -> Self {
13+
Self { filter: None }
14+
}
15+
}
16+
17+
pub fn parse_args(suite_name: &str) -> Args {
18+
let matches = App::new(suite_name)
19+
.arg(
20+
Arg::with_name("TESTNAME")
21+
.help("If specified, only run tests containing this string in their names"),
22+
)
23+
.get_matches();
24+
25+
Args {
26+
filter: matches.value_of("TESTNAME").map(|name| name.to_string()),
27+
}
28+
}
29+
630
pub struct Test {
731
pub name: String,
832
pub task: Pin<Box<dyn Future<Output = PyResult<()>> + Send>>,
@@ -30,11 +54,23 @@ impl Test {
3054
}
3155
}
3256

33-
pub async fn test_harness(tests: impl Stream<Item = Test>) -> PyResult<()> {
57+
pub async fn test_harness(tests: impl Stream<Item = Test>, args: Args) -> PyResult<()> {
3458
tests
35-
.for_each_concurrent(Some(4), |test| async move {
36-
test.task.await.unwrap();
37-
println!("test {} ... ok", test.name);
59+
.for_each_concurrent(Some(4), |test| {
60+
let mut ignore = false;
61+
62+
if let Some(filter) = args.filter.as_ref() {
63+
if !test.name.contains(filter) {
64+
ignore = true;
65+
}
66+
}
67+
68+
async move {
69+
if !ignore {
70+
test.task.await.unwrap();
71+
println!("test {} ... ok", test.name);
72+
}
73+
}
3874
})
3975
.await;
4076

0 commit comments

Comments
 (0)