From b7c6edb42612a55b34df92f24214020f388f36d3 Mon Sep 17 00:00:00 2001 From: Andre Popovitch Date: Fri, 9 Sep 2022 21:05:05 -0500 Subject: [PATCH] Add example of how to use TestPlugin --- Cargo.toml | 10 +++++++++ examples/README.md | 1 + tests/automated_tests.rs | 48 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 tests/automated_tests.rs diff --git a/Cargo.toml b/Cargo.toml index 226d640bcdff7d..08df30cafd446d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" +category = "ECS (Entity Component System)" +wasm = false + # Games [[example]] name = "alien_cake_addict" diff --git a/examples/README.md b/examples/README.md index 8f8b57759727a7..e5d2c8b7b340d6 100644 --- a/examples/README.md +++ b/examples/README.md @@ -187,6 +187,7 @@ Example | Description Example | Description --- | --- +[Automated Tests](../examples/ecs/automated_tests.rs) | Illustrates how to test systems [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 diff --git a/tests/automated_tests.rs b/tests/automated_tests.rs new file mode 100644 index 00000000000000..4675251677b27b --- /dev/null +++ b/tests/automated_tests.rs @@ -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(TestPlugins).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_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); + } + } +}