8 releases (breaking)

Uses old Rust 2015

0.7.0 Jun 25, 2021
0.6.1 Jul 10, 2020
0.5.0 Dec 14, 2019
0.4.0 Jun 22, 2019
0.1.0 May 1, 2018

#234 in GUI

Download history 63/week @ 2021-06-28 37/week @ 2021-07-05 78/week @ 2021-07-12 36/week @ 2021-07-19 42/week @ 2021-07-26 39/week @ 2021-08-02 42/week @ 2021-08-09 24/week @ 2021-08-16 25/week @ 2021-08-23 4/week @ 2021-08-30 17/week @ 2021-09-06 23/week @ 2021-09-13 31/week @ 2021-09-20 15/week @ 2021-09-27 19/week @ 2021-10-04 22/week @ 2021-10-11

185 downloads per month
Used in 2 crates

MIT license

330 lines

gtk-test Build Status Build status

This projects allows you to test your GTK's applications UI. It has to be used with gtk-rs crates.

How does it work?

It's quite simple actually (even though you have to perform a few more things on OSX to make it work as expected...) :

gtk::init().unwrap(); // You need to init GTK otherwise it'll just crash...

Then you build your UI as you would in normal time (using Glade or by hand). Only one thing actually changes: you must not call gtk::main!

Once you have built your UI, just call the gtk_test macros/functions to test it. Just one note about this though: sometimes, you need to let time for GTK to process some events. For example, if you clicked on a button and you have an associated action to it, it's more careful to use gtk_test::wait.

Another recommended thing is to give focus to the window in case you have to interact with it (to click on a button or to input some text...):

let w = gtk::Window::new();
// ...

General setup

When running test, you need to specify that you only want ONE thread. To do so:

cargo test -- --test-threads=1

Otherwise, GTK contexts might conflict into each others.

Specific setup for OSX

A few more things have to be done on OSX to make this work. First, you won't be able to add the #[test] attribute to your functions, it doesn't work. Instead, you have to write your test just like you would write a normal binary (so with a main function as entry point).

A short example (you can find the full version in the tests folder of this repository):

fn main() {
    let (w, l, b) = init_ui();

    assert_text!(l, "Test");
    gtk_test::wait(1000); // to be sure that GTK has updated the label's text
    assert_text!(l, "Clicked");

Then you need to add into your Cargo.toml file:

harness = false # This is the important line!
name = "basic"

It allows your test to be run as a "normal" binary.


You can find a few in the tests folder. Just copy/paste it and you're good to go (don't forget to add the missing pieces in your Cargo.toml file!).

Using it on CI?

It's actually possible (only tested for travis though). You "just" need a window manager. Here's what you have to ad in your Ci configuration file to make it work:

Install the following packages:

  • libcurl4-openssl-dev
  • libelf-dev
  • libdw-dev
  • binutils-dev
  • libsoup2.4-dev
  • libxtst-dev
  • at-spi2-core
  • openbox

Then you to execute the following commands:

> "export DISPLAY=:99.0"
> "sh -e /etc/init.d/xvfb start"
> sleep 3 # give xvfb some time to start
> openbox &

Take a look at our .travis.yml file to see how we set things up.


~385K SLoC