Skip to content

Commit

Permalink
Add example of how to use TestPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
anchpop committed Sep 10, 2022
1 parent 0740263 commit 21fbed7
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 8 deletions.
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,16 @@ description = "Illustrates ticking `Timer` resources inside systems and handling
category = "ECS (Entity Component System)"
wasm = false

[[example]]
name = "automated_tests"
path = "tests/automated_tests.rs"

[package.metadata.example.automated_tests]
name = "Automated Tests"
description = "Illustrates how to test systems. Note that this test is in `tests/automated_tests.rs`."
category = "ECS (Entity Component System)"
wasm = false

# Games
[[example]]
name = "alien_cake_addict"
Expand Down
56 changes: 49 additions & 7 deletions crates/bevy_internal/src/default_plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,43 @@ impl PluginGroup for DefaultPlugins {
}
}

impl DefaultPlugins {
/// Usage: `DefaultPlugins::for_testing()`.
/// Like DefaultPlugins, but omits some plugins that are likely to be problematic during tests and on CI builds.
///
/// * [`RenderPlugin`](bevy_render::RenderPlugin) - with feature `bevy_render`
/// * [`SpritePlugin`](bevy_sprite::SpritePlugin) - with feature `bevy_sprite`
/// * [`PbrPlugin`](bevy_pbr::PbrPlugin) - with feature `bevy_pbr`
/// * [`UiPlugin`](bevy_ui::UiPlugin) - with feature `bevy_ui`
/// * [`TextPlugin`](bevy_text::TextPlugin) - with feature `bevy_text`
/// * [`AudioPlugin`](bevy_audio::AudioPlugin) - with feature `bevy_audio`
/// * [`GltfPlugin`](bevy_gltf::GltfPlugin) - with feature `bevy_gltf`
/// * [`WinitPlugin`](bevy_winit::WinitPlugin) - with feature `bevy_winit`
///
/// And adds:
///
/// * [`ScheduleRunnerPlugin`](bevy_app::ScheduleRunnerPlugin)
///
/// Leaving:
///
/// * [`CorePlugin`](bevy_core::CorePlugin)
/// * [`TimePlugin`](bevy_time::TimePlugin)
/// * [`TransformPlugin`](bevy_transform::TransformPlugin)
/// * [`HierarchyPlugin`](bevy_hierarchy::HierarchyPlugin)
/// * [`DiagnosticsPlugin`](bevy_diagnostic::DiagnosticsPlugin)
/// * [`InputPlugin`](bevy_input::InputPlugin)
/// * [`WindowPlugin`](bevy_window::WindowPlugin)
/// * [`AssetPlugin`](bevy_asset::AssetPlugin)
/// * [`ScenePlugin`](bevy_scene::ScenePlugin)
/// * [`GilrsPlugin`](bevy_gilrs::GilrsPlugin) - with feature `bevy_gilrs`
/// * [`ScheduleRunnerPlugin`](bevy_app::ScheduleRunnerPlugin)
///
/// You should probably not use this plugin except in automated tests.
pub fn for_testing() -> impl PluginGroup {
TestPlugins
}
}

/// Minimal plugin group that will add the following plugins:
/// * [`CorePlugin`](bevy_core::CorePlugin)
/// * [`TimePlugin`](bevy_time::TimePlugin)
Expand All @@ -97,36 +134,41 @@ impl PluginGroup for MinimalPlugins {
}
}

/// Only accessible through `DefaultPlugins::for_testing()`.
/// Plugin group intended for use in tests, that will add the following plugins:
/// * [`CorePlugin`](bevy_core::CorePlugin)
/// * [`TimePlugin`](bevy_time::TimePlugin)
/// * [`ScheduleRunnerPlugin`](bevy_app::ScheduleRunnerPlugin)
/// * [`AssetPlugin`](bevy_asset::AssetPlugin)
/// * [`ScenePlugin`](bevy_scene::ScenePlugin)
/// * [`WindowPlugin`](bevy_window::WindowPlugin)
/// * [`RenderPlugin`](bevy_render::RenderPlugin)
/// * [`GilrsPlugin`](bevy_gilrs::GilrsPlugin)
/// * [`GilrsPlugin`](bevy_gilrs::GilrsPlugin) - with feature `bevy_gilrs`
/// * [`TransformPlugin`](bevy_transform::TransformPlugin)
/// * [`HierarchyPlugin`](bevy_hierarchy::HierarchyPlugin)
/// * [`DiagnosticsPlugin`](bevy_diagnostic::DiagnosticsPlugin)
/// * [`InputPlugin`](bevy_input::InputPlugin)
///
/// See also [`DefaultPlugins`] for a more complete set of plugins
pub struct TestPlugins;
struct TestPlugins;

impl PluginGroup for TestPlugins {
fn build(&mut self, group: &mut PluginGroupBuilder) {
group.add(bevy_core::CorePlugin::default());
group.add(bevy_time::TimePlugin::default());
group.add(bevy_app::ScheduleRunnerPlugin::default());
group.add(bevy_asset::AssetPlugin);
group.add(bevy_scene::ScenePlugin);
group.add(bevy_window::WindowPlugin);
group.add(bevy_render::RenderPlugin);
group.add(bevy_gilrs::GilrsPlugin);
group.add(bevy_transform::TransformPlugin);
group.add(bevy_hierarchy::HierarchyPlugin);
group.add(bevy_diagnostic::DiagnosticsPlugin);
group.add(bevy_input::InputPlugin);

#[cfg(feature = "bevy_asset")]
group.add(bevy_asset::AssetPlugin::default());

#[cfg(feature = "bevy_scene")]
group.add(bevy_scene::ScenePlugin::default());

#[cfg(feature = "bevy_gilrs")]
group.add(bevy_gilrs::GilrsPlugin::default());
}
}
1 change: 0 additions & 1 deletion crates/bevy_internal/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pub use crate::{
app::prelude::*, core::prelude::*, ecs::prelude::*, hierarchy::prelude::*, input::prelude::*,
log::prelude::*, math::prelude::*, reflect::prelude::*, time::prelude::*,
transform::prelude::*, utils::prelude::*, window::prelude::*, DefaultPlugins, MinimalPlugins,
TestPlugins,
};

pub use bevy_derive::{bevy_main, Deref, DerefMut};
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ Example | Description

Example | Description
--- | ---
[Automated Tests](../tests/automated_tests.rs) | Illustrates how to test systems. Note that this test is in `tests/automated_tests.rs`.
[Component Change Detection](../examples/ecs/component_change_detection.rs) | Change detection on components
[Custom Query Parameters](../examples/ecs/custom_query_param.rs) | Groups commonly used compound queries and query filters into a single type
[ECS Guide](../examples/ecs/ecs_guide.rs) | Full guide to Bevy's ECS
Expand Down
48 changes: 48 additions & 0 deletions tests/automated_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//! This example illustrates test systems.
fn main() {
println!("This example is special! Run it with `cargo test --example automated_tests`.");
println!(
"Or use `cargo test --example automated_tests -- --nocapture` to see the debug output."
);
}

#[cfg(test)]
mod test {
use bevy::prelude::*;

#[test]
fn simple_test() {
// Setup the app with the TestPlugins – these will run fine in tests and in CI.
// Note that many 3rd-party plugins will require DefaultPlugins, not just TestPlugins.
let mut app = App::new();
app.add_plugins(DefaultPlugins::for_testing()).add_system(increment);

// Spawn a new entity with a Counter component, and record its ID.
let counter_id = app.world.spawn().insert(Counter::default()).id();

// Simulate for a 10 frames
let num_frames = 10;
for _ in 0..num_frames {
app.update();
}

// Check that the counter was incremented 10 times.
let count = app.world.get::<Counter>(counter_id).unwrap().counter;
assert_eq!(count, num_frames);

println!("Success!");
}
// Define a system and a component that we can use in our test.
#[derive(Debug, Default, Component, Clone, Copy)]
struct Counter {
counter: u64,
}

/// Increment the counter every frame
fn increment(mut query: Query<&mut Counter>) {
for mut counter in query.iter_mut() {
counter.counter += 1;
println!("Counter: {}", counter.counter);
}
}
}

0 comments on commit 21fbed7

Please sign in to comment.