Skip to content

Commit a0b7390

Browse files
committed
Show that the application-under-test can be added as a plugin. Add module level documentation.
1 parent d27ee17 commit a0b7390

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

tests/how_to_test_full_app.rs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
//! This module serves as an example, and test-case, for how to automatically test full bevy apps.
2+
//!
3+
//! The way it works, is by substituting the [DefaultPlugins] with [MinimalPlugins], which
4+
//! allows bevy to run completely headless.
5+
//!
6+
//! The list of minimal plugins does not include things like window or input handling.
7+
//! This has as downside that the resources / entities associated with those systems
8+
//! (for example: `Input::<KeyCode>`) need to be manually added.
9+
//! The upside however, is that the test has complete control over these resources, meaning
10+
//! we can fake user input, fake the window being moved around, and more.
11+
//!
12+
//! The benefit of having this set of example tests be run by the CI system, is that we ensure
13+
//! these full-app tests keep working during bevy's development.
14+
115
use bevy::prelude::*;
216

317
const DEFAULT_MANA: u32 = 10;
@@ -29,6 +43,20 @@ fn spell_casting(mut player: Query<&mut Player>, keyboard_input: Res<Input<KeyCo
2943
}
3044
}
3145

46+
/// This struct would normally be defined somewhere in the game code.
47+
/// It could be the plugin that sets up the entire game (except for the [DefaultPlugins]).
48+
/// Or the game could be split into multiple smaller plugins, allowing one to write tests
49+
/// for each of the games' parts in isolation (on top of some tests for the entire game).
50+
pub struct GamePlugin;
51+
52+
impl Plugin for GamePlugin {
53+
fn build(&self, app: &mut App) {
54+
app.add_startup_system(spawn_player)
55+
.add_startup_system(window_title_system)
56+
.add_system(spell_casting);
57+
}
58+
}
59+
3260
fn create_test_app() -> App {
3361
let mut app = App::new();
3462

@@ -45,19 +73,10 @@ fn create_test_app() -> App {
4573
app
4674
}
4775

48-
fn add_game_systems(app: &mut App) {
49-
// This could be a subset of your game's systems, or the entire app.
50-
// As long as you make sure to add a fake version of inputs, windows, and any
51-
// other things that your game's systems rely on.
52-
app.add_startup_system(spawn_player)
53-
.add_startup_system(window_title_system)
54-
.add_system(spell_casting);
55-
}
56-
5776
#[test]
5877
fn test_player_spawn() {
5978
let mut app = create_test_app();
60-
add_game_systems(&mut app);
79+
app.add_plugin(GamePlugin);
6180

6281
// The `update` function needs to be called at least once for the startup
6382
// systems to run.
@@ -77,7 +96,7 @@ fn test_player_spawn() {
7796
#[test]
7897
fn test_spell_casting() {
7998
let mut app = create_test_app();
80-
add_game_systems(&mut app);
99+
app.add_plugin(GamePlugin);
81100

82101
// We simulate pressing `space` to trigger the spell casting system.
83102
app.world
@@ -102,7 +121,7 @@ fn test_spell_casting() {
102121
#[test]
103122
fn test_faking_windows() {
104123
let mut app = create_test_app();
105-
add_game_systems(&mut app);
124+
app.add_plugin(GamePlugin);
106125

107126
// The `update` function needs to be called at least once for the startup
108127
// systems to run.

0 commit comments

Comments
 (0)