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 f51258c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 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"
category = "ECS (Entity Component System)"
wasm = false

# Games
[[example]]
name = "alien_cake_addict"
Expand Down
18 changes: 18 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ git checkout v0.4.0
- [Transforms](#transforms)
- [UI (User Interface)](#ui-user-interface)
- [Window](#window)
- [Tests](#tests)
- [Platform-Specific Examples](#platform-specific-examples)
- [Android](#android)
- [Setup](#setup)
- [Build & Run](#build--run)
- [Debugging](#debugging)
- [Old phones](#old-phones)
- [iOS](#ios)
- [Setup](#setup-1)
- [Build & Run](#build--run-1)
- [WASM](#wasm)
- [Setup](#setup-2)
- [Build & Run](#build--run-2)
- [Optimizing](#optimizing)
- [1. Tweak your `Cargo.toml`](#1-tweak-your-cargotoml)
- [2. Use `wasm-opt` from the binaryen package](#2-use-wasm-opt-from-the-binaryen-package)
- [Loading Assets](#loading-assets)

- [Tests](#tests)
- [Platform-Specific Examples](#platform-specific-examples)
Expand Down Expand Up @@ -187,6 +204,7 @@ Example | Description

Example | Description
--- | ---
[Automated Tests](../examples/ecs/automated_tests.rs) | Illustrates how to test systems. Note that this example is under `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(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>(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 f51258c

Please sign in to comment.