Unit tests in Vala

Simple test

void add_foo_tests () {
    Test.add_func ("/vala/test", () => {
        assert ("foo" + "bar" == "foobar");
    });
}

void main (string[] args) {
    Test.init (ref args);
    add_foo_tests ();
    Test.run ();
}

Compile this with:

valac unit-test.vala

Test using GTK+

void add_foo_tests () {
    Test.add_func ("/vala/test", () => {
        var widget = new Gtk.Button ();
        assert (widget is Gtk.Button);
    });
}

void main (string[] args) {
    Gtk.init (ref args);
    Test.init (ref args);

    add_foo_tests ();

    Idle.add (() => {
        Test.run ();
        Gtk.main_quit ();
        return false;
    });

    Gtk.main ();
}

Compile this with:

valac --pkg gtk+-2.0 unit-test-gtk.vala

Meson

sources = files(
  'unit-test-gtk.vala',
)

test_unit = 'unit-test'

deps = [dependency('glib-2.0'), dependency('gtk+-3.0'), dependency('gobject-2.0')]

exe = executable(test_unit, sources: sources, dependencies : deps)
test(test_unit, exe)


Compile with meson:

meson build
cd build
meson test

Vala/Examples

Projects/Vala/TestSample (last edited 2019-07-18 11:06:40 by Gaurav Agrawal)