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
+
1
15
use bevy:: prelude:: * ;
2
16
3
17
const DEFAULT_MANA : u32 = 10 ;
@@ -29,6 +43,20 @@ fn spell_casting(mut player: Query<&mut Player>, keyboard_input: Res<Input<KeyCo
29
43
}
30
44
}
31
45
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
+
32
60
fn create_test_app ( ) -> App {
33
61
let mut app = App :: new ( ) ;
34
62
@@ -45,19 +73,10 @@ fn create_test_app() -> App {
45
73
app
46
74
}
47
75
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
-
57
76
#[ test]
58
77
fn test_player_spawn ( ) {
59
78
let mut app = create_test_app ( ) ;
60
- add_game_systems ( & mut app) ;
79
+ app. add_plugin ( GamePlugin ) ;
61
80
62
81
// The `update` function needs to be called at least once for the startup
63
82
// systems to run.
@@ -77,7 +96,7 @@ fn test_player_spawn() {
77
96
#[ test]
78
97
fn test_spell_casting ( ) {
79
98
let mut app = create_test_app ( ) ;
80
- add_game_systems ( & mut app) ;
99
+ app. add_plugin ( GamePlugin ) ;
81
100
82
101
// We simulate pressing `space` to trigger the spell casting system.
83
102
app. world
@@ -102,7 +121,7 @@ fn test_spell_casting() {
102
121
#[ test]
103
122
fn test_faking_windows ( ) {
104
123
let mut app = create_test_app ( ) ;
105
- add_game_systems ( & mut app) ;
124
+ app. add_plugin ( GamePlugin ) ;
106
125
107
126
// The `update` function needs to be called at least once for the startup
108
127
// systems to run.
0 commit comments