I have been trying to build an OpenGL-based image processing library with GLFW, and need it to be testable. Unfortunately, I ran into this bug - GLFW needs its initialization functions to be called from the main thread, but Cargo tests are run on a background thread.
-
Just ran into this trying to use egui, guess it's still not Just Works in 2022? – Max Jun 17 '22 at 02:16
1 Answers
No, to the best of my knowledge, it's not easily possible right now. There is a --test-threads
argument you can pass to the test harness, but it sets the number of threads in addition to the main thread. So --test-threads=1
results in two threads still.
So you can't really use the default test-harness. Luckily, you can disable it in Cargo.toml
. Possibly the best solution is to create a new folder (e.g. gltests
) and place all tests that require being run in main thread in there. Then we just have to declare those tests in Cargo.toml
:
[[test]]
name = "gltests"
path = "gltests/main.rs"
harness = false
This means that cargo will try to compile gltests/main.rs
as an executable (expecting a main()
function) and execute this executable, whenever you say cargo test
. This way you won't get any of the fancy output you usually get from cargo tests. You just have to do everything yourself in main.rs
, but at least you can start tests in the main thread.

- 79,749
- 26
- 255
- 305
-
Great answer, this also helps anyone writing an automated tests of a desktop GUI application. Just like GLFW in the question, many GUI toolkits also require being initialized or used from the main thread. – user4815162342 Apr 17 '17 at 22:05