@@ -303,6 +303,7 @@ pub struct TestOpts {
303303 pub nocapture : bool ,
304304 pub color : ColorConfig ,
305305 pub quiet : bool ,
306+ pub test_threads : Option < usize > ,
306307}
307308
308309impl TestOpts {
@@ -317,6 +318,7 @@ impl TestOpts {
317318 nocapture : false ,
318319 color : AutoColor ,
319320 quiet : false ,
321+ test_threads : None ,
320322 }
321323 }
322324}
@@ -334,6 +336,8 @@ fn optgroups() -> Vec<getopts::OptGroup> {
334336 of stdout", "PATH" ) ,
335337 getopts:: optflag( "" , "nocapture" , "don't capture stdout/stderr of each \
336338 task, allow printing directly") ,
339+ getopts:: optopt( "" , "test-threads" , "Number of threads used for running tests \
340+ in parallel", "n_threads" ) ,
337341 getopts:: optflag( "q" , "quiet" , "Display one character per test instead of one line" ) ,
338342 getopts:: optopt( "" , "color" , "Configure coloring of output:
339343 auto = colorize if stdout is a tty and tests are run on serially (default);
@@ -349,7 +353,8 @@ The FILTER string is tested against the name of all tests, and only those
349353tests whose names contain the filter are run.
350354
351355By default, all tests are run in parallel. This can be altered with the
352- RUST_TEST_THREADS environment variable when running tests (set it to 1).
356+ --test-threads flag or the RUST_TEST_THREADS environment variable when running
357+ tests (set it to 1).
353358
354359All tests have their standard output and standard error captured by default.
355360This can be overridden with the --nocapture flag or setting RUST_TEST_NOCAPTURE
@@ -408,6 +413,18 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
408413 } ;
409414 }
410415
416+ let test_threads = match matches. opt_str ( "test-threads" ) {
417+ Some ( n_str) =>
418+ match n_str. parse :: < usize > ( ) {
419+ Ok ( n) => Some ( n) ,
420+ Err ( e) =>
421+ return Some ( Err ( format ! ( "argument for --test-threads must be a number > 0 \
422+ (error: {})", e) ) )
423+ } ,
424+ None =>
425+ None ,
426+ } ;
427+
411428 let color = match matches. opt_str ( "color" ) . as_ref ( ) . map ( |s| & * * s) {
412429 Some ( "auto" ) | None => AutoColor ,
413430 Some ( "always" ) => AlwaysColor ,
@@ -429,6 +446,7 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
429446 nocapture : nocapture,
430447 color : color,
431448 quiet : quiet,
449+ test_threads : test_threads,
432450 } ;
433451
434452 Some ( Ok ( test_opts) )
@@ -871,9 +889,10 @@ fn run_tests<F>(opts: &TestOpts, tests: Vec<TestDescAndFn>, mut callback: F) ->
871889 }
872890 } ) ;
873891
874- // It's tempting to just spawn all the tests at once, but since we have
875- // many tests that run in other processes we would be making a big mess.
876- let concurrency = get_concurrency ( ) ;
892+ let concurrency = match opts. test_threads {
893+ Some ( n) => n,
894+ None => get_concurrency ( ) ,
895+ } ;
877896
878897 let mut remaining = filtered_tests;
879898 remaining. reverse ( ) ;
0 commit comments