本文介绍了如何限制 Cargo.toml 中的测试线程数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有共享公共资源且无法同时执行的测试.这些测试使用 cargo test 失败,但使用 RUST_TEST_THREADS=1 cargo test.

I have tests which share a common resource and can't be executed concurrently. These tests fail with cargo test, but work with RUST_TEST_THREADS=1 cargo test.

我可以修改测试以等待全局互斥锁,但如果有任何更简单的方法来强制 cargo 为我设置这个环境变量,我不想让它们变得混乱.

I can modify the tests to wait on a global mutex, but I don't want to clutter them if there is any simpler way to force cargo set this environment variable for me.

推荐答案

从 Rust 1.18 开始,没有这样的事情.事实上,没有更简单的选项可以禁用并行测试.
来源

As of Rust 1.18, there is no such thing. In fact, there is not even a simpler option to disable parallel testing.
Source

但是,cargo test -- --test-threads=1 可能对您有所帮助,这是推荐的通过 RUST_TEST_THREADS 执行您正在执行的操作的方法环境变量.请记住,这只会设置除了主线程外用于测试的线程数.

However, what might help you is cargo test -- --test-threads=1, which is the recommended way of doing what you are doing over the RUST_TEST_THREADS envvar. Keep in mind that this only sets the number of threads used for testing in addition to the main thread.

这篇关于如何限制 Cargo.toml 中的测试线程数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 11:33