Skip to content

Cannot create Bevy app in tests due to non-main thread #1720

Closed
@alice-i-cecile

Description

@alice-i-cecile

Bevy version

The release number or commit hash of the version you're using.

Operating system & version

Windows 10, VSCode, commit: 348e2a3

What you did

#[cfg(test)]
mod tests {
    use bevy::prelude::*;
    #[test]
    fn default_plugin_tests() {
        App::build().add_plugins(DefaultPlugins).run();
    }
}

What you expected to happen

I would be able to spawn Bevy apps within my tests.

What actually happened

Using cargo test fails:

 Caused by:
 process didn't exit successfully: `C:\Users\Alice\Documents\bevy-scratchpad\target\debug\deps\bevy_scratchpad-c846071201aed109.exe tests::minimal_bevy_tests --exact --nocapture` (exit code: 0xc0000005, STATUS_ACCESS_VIOLATION)

Running just the single test reveals:

thread 'tests::default_plugin_tests' panicked at 'Initializing the event loop outside of the main thread is a significant cross-platform compatibility hazard. If you really, absolutely need to create an EventLoop on a different thread, please use the `EventLoopExtWindows::new_any_thread` function.', C:\Users\Alice\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.24.0\src\platform_impl\windows\event_loop.rs:136:9

Additional information

This fails for DefaultPlugins but not MinimalPlugins.

#1714 has great guidance on how to test individual systems, but this doesn't suffice for full integration tests (and this failure is frustrating for beginners).

@mockersf suggested that I try running the test using only 1 thread, as described here. Using cargo test -- --test-threads=1 solves the issue, but this is a nuisance to remember to do every time (and I only want to run the main-thread-only tests on the main thread).

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-UsabilityA targeted quality-of-life change that makes Bevy easier to use

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions