Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add egui_kittest, a test harness for egui #5166

Merged
merged 47 commits into from
Oct 22, 2024
Merged

Conversation

lucasmerlin
Copy link
Collaborator

@lucasmerlin lucasmerlin commented Sep 25, 2024

This adds a testing library to egui based on kittest. Kittest is a new AccessKit-based testing library. The api is inspired by the js testing-library where the idea is also to query the dom based on accessibility attributes.
We made kittest with egui in mind but it should work with any rust gui framework with AccessKit support.

It currently has support for:

  • running the egui app, frame by frame
  • building the AccessKit tree
  • ergonomic queries via kittest
    • via e.g. get_by_name, get_by_role
  • simulating events based on the accesskit node id
  • creating arbitrary events based on Harness::input_mut
  • rendering screenshots via wgpu
  • snapshot tests with these screenshots

A simple test looks like this:

fn main() {
    let mut checked = false;
    let app = |ctx: &Context| {
        CentralPanel::default().show(ctx, |ui| {
            ui.checkbox(&mut checked, "Check me!");
        });
    };

    let mut harness = Harness::builder().with_size(egui::Vec2::new(200.0, 100.0)).build(app);
    
    let checkbox = harness.get_by_name("Check me!");
    assert_eq!(checkbox.toggled(), Some(Toggled::False));
    checkbox.click();
    
    harness.run();

    let checkbox = harness.get_by_name("Check me!");
    assert_eq!(checkbox.toggled(), Some(Toggled::True));

    // You can even render the ui and do image snapshot tests
    #[cfg(all(feature = "wgpu", feature = "snapshot"))]
    egui_kittest::image_snapshot(&egui_kittest::wgpu::TestRenderer::new().render(&harness), "readme_example");
}

Since getting wgpu to run in ci is a hassle, I'm taking another shot at creating a software renderer for egui (ideally without a huge dependency like skia) (this didn't work as well as I hoped and it turns out in CI you can just run tests on a mac runner which comes with a real GPU)

Here is a example of a failed snapshot test in ci, it will say which snapshot failed and upload an artifact with the before / after and diff images:
https://github.com/emilk/egui/actions/runs/11183049487/job/31090724606?pr=5166

Copy link

Preview available at https://egui-pr-preview.github.io/pr/5166-lucas/testing_library
Note that it might take a couple seconds for the update to show up after the preview_build workflow has completed.

@lucasmerlin lucasmerlin added tests Unit tests, regression tests, manual tests, … dev-experience labels Sep 25, 2024
@emilk
Copy link
Owner

emilk commented Sep 25, 2024

After some discussion we came to these conclusions:

  • Investigate using git lfs for storing the .png snapshot files so we don't bloat the git repository. Try to set it up in such a way that git clone + cargo test still works for contributors without lfs.
  • Try to set up a proper software wgpu backend that works on CI, same as the wgpu repository uses. This will be the most powerful and accurate, and allow us to test more than just egui (i.e. the Rerun Viewer)
  • Move the accesskit-specific part of the integration to its own repository and eventually invite collaborators from other GUI toolkits to collaborate on it

@lucasmerlin lucasmerlin requested a review from emilk October 11, 2024 10:12
Copy link
Owner

@emilk emilk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is so awesome!

The PR description is a bit outdated though

@@ -1,2 +1,3 @@
* text=auto eol=lf
Cargo.lock linguist-generated=false
**/tests/snapshots/**/*.png filter=lfs diff=lfs merge=lfs -text
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just apply lfs to all png:s everywhere?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, but I think we should do that in a separate PR. And it should also include gifs, which seem to be the biggest files in the repo. And maybe also the entirety of the media folder.

.gitignore Outdated Show resolved Hide resolved
.github/workflows/rust.yml Show resolved Hide resolved
crates/egui_demo_lib/src/demo/widget_gallery.rs Outdated Show resolved Hide resolved
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's something wrong with the framing on this one!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's because this one has constrain(false). No idea how I could fix that though without changing constrain to true 🤔

crates/egui_kittest/src/snapshot.rs Outdated Show resolved Hide resolved
});
}

let result = dify::diff::get_results(previous, current.clone(), 0.1, true, None, &None, &None);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And separately - have you tested tolerance=0?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed it to 0, but I think we should at least have someone test this on linux and windows. I can test on windows but someone else will need to test it for linux. Preferably once with the vulkan and once with the gl backend.

crates/egui_kittest/src/snapshot.rs Outdated Show resolved Hide resolved
crates/egui_kittest/src/wgpu.rs Outdated Show resolved Hide resolved
crates/egui_kittest/src/texture_to_bytes.rs Outdated Show resolved Hide resolved
@lucasmerlin lucasmerlin force-pushed the lucas/testing_library branch from 88324d1 to 27414ea Compare October 12, 2024 11:53
@lucasmerlin lucasmerlin requested a review from emilk October 12, 2024 13:29
@lucasmerlin
Copy link
Collaborator Author

I've changed everything you mentioned and also added Harness::wgpu_snapshot as a convenicence.

Also, I've disabled dithering since it caused minor differences between frames. This made the pngs much smaller!

@lucasmerlin lucasmerlin merged commit 70a0113 into master Oct 22, 2024
44 checks passed
@lucasmerlin lucasmerlin deleted the lucas/testing_library branch October 22, 2024 10:39
@lucasmerlin lucasmerlin changed the title Add egui testing library Add egui_kittest, a test harness for egui Oct 22, 2024
lucasmerlin added a commit that referenced this pull request Oct 24, 2024
The check.sh script was broken in #5166, this fixes it
* Related to #5297 
* [x] I have followed the instructions in the PR template

---------

Co-authored-by: Emil Ernerfeldt <[email protected]>
lucasmerlin added a commit that referenced this pull request Oct 27, 2024
The check.sh script was broken in #5166, this fixes it
* Related to #5297 
* [x] I have followed the instructions in the PR template

---------

Co-authored-by: Emil Ernerfeldt <[email protected]>
lucasmerlin added a commit that referenced this pull request Oct 29, 2024
The check.sh script was broken in #5166, this fixes it
* Related to #5297 
* [x] I have followed the instructions in the PR template

---------

Co-authored-by: Emil Ernerfeldt <[email protected]>
hacknus pushed a commit to hacknus/egui that referenced this pull request Oct 30, 2024
- closes emilk#3491 
- closes emilk#3926

This adds a testing library to egui based on
[kittest](https://github.com/rerun-io/kittest). Kittest is a new
[AccessKit](https://github.com/AccessKit/accesskit/)-based testing
library. The api is inspired by the js
[testing-library](https://testing-library.com/) where the idea is also
to query the dom based on accessibility attributes.
We made kittest with egui in mind but it should work with any rust gui
framework with AccessKit support.

It currently has support for:
- running the egui app, frame by frame
- building the AccessKit tree
- ergonomic queries via kittest
  - via e.g. get_by_name, get_by_role
- simulating events based on the accesskit node id
- creating arbitrary events based on Harness::input_mut
- rendering screenshots via wgpu
- snapshot tests with these screenshots

A simple test looks like this: 
```rust
fn main() {
    let mut checked = false;
    let app = |ctx: &Context| {
        CentralPanel::default().show(ctx, |ui| {
            ui.checkbox(&mut checked, "Check me!");
        });
    };

    let mut harness = Harness::builder().with_size(egui::Vec2::new(200.0, 100.0)).build(app);
    
    let checkbox = harness.get_by_name("Check me!");
    assert_eq!(checkbox.toggled(), Some(Toggled::False));
    checkbox.click();
    
    harness.run();

    let checkbox = harness.get_by_name("Check me!");
    assert_eq!(checkbox.toggled(), Some(Toggled::True));

    // You can even render the ui and do image snapshot tests
    #[cfg(all(feature = "wgpu", feature = "snapshot"))]
    egui_kittest::image_snapshot(&egui_kittest::wgpu::TestRenderer::new().render(&harness), "readme_example");
}
```

~Since getting wgpu to run in ci is a hassle, I'm taking another shot at
creating a software renderer for egui (ideally without a huge dependency
like skia)~ (this didn't work as well as I hoped and it turns out in CI
you can just run tests on a mac runner which comes with a real GPU)
 
Here is a example of a failed snapshot test in ci, it will say which
snapshot failed and upload an artifact with the before / after and diff
images:

https://github.com/emilk/egui/actions/runs/11183049487/job/31090724606?pr=5166
hacknus pushed a commit to hacknus/egui that referenced this pull request Oct 30, 2024
The check.sh script was broken in emilk#5166, this fixes it
* Related to emilk#5297 
* [x] I have followed the instructions in the PR template

---------

Co-authored-by: Emil Ernerfeldt <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI Continues Integration test dev-experience tests Unit tests, regression tests, manual tests, …
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improved unit/regression test support Integration testing/event injection
2 participants