Home > People Framework, Tools, Vala > Vala and unit testing

Vala and unit testing

During the past year, I have been writing most of my code in Vala, an awesome, simple and beautiful language. When I started using it, the implementation was still in its infancy, and had a pretty high number of bugs, and the only way to make sure that the code produced by the compiler was good, was to write unit tests. The first attempt was made by Johann Prieur, in the form of vunit, a mostly direct port of junit 3.x, which turned into vtest later, and also got forked into another vunit :D In the end I wrote a very simplistic unit testing framework that serves my need: test-it.

But that wasn’t really enough for me. Few days ago I cooked a small patch that would help simplifying the process of writing unit tests in Vala. The patch allows writing unit test suites like this:

[Test (path = "/vala/foo/bar")]
class FooBarTest {
	private StringBuilder buffer;

	public void set_up () {
		buffer = new StringBuilder ();
	}

	public void tear_down () {
		buffer = null;
	}

	public void test_1 () {
		buffer.append ("hello");
		assert (buffer.str == "hello");
	}

	public void test_2 () {
		assert (buffer.str == "");
	}
}

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

	var o = new FooBarTest ();

	Test.run ();
	return 0;
}

Vala, would then notice the [Test] attribute for the FooBarTest class, and produce a set of test cases that get registered with GTest when the class is instanciated. When compiled and ran the previous code produces the following output:

$ ./testing
/vala/foo/bar/1: OK
/vala/foo/bar/2: OK

The patch is still far from perfect, and personally I don’t really appreciate GTest, but if you feel like improving it, feel free to grab the code from here:

bzr branch lp:~asabil/+junk/vala-gtest-module

About these ads
Categories: People Framework, Tools, Vala
  1. gantenbein
    January 24, 2009 at 2:23 am

    Great!

    I would like to see more assert_* functions as in JUnit. E.g. for comparing floats/doubles within a delta or for checking the equality of arrays, and with the ability to specify a message to be printed on failure.

  2. gantenbein
    January 24, 2009 at 2:25 am

    The standard output on failure should be something like:

    Expected: … But was: …

  3. January 24, 2009 at 3:11 am

    I agree with you, but the current implementation uses the glib testing framework: GTest, which is imho far less than optimal, I will see what I will be able to do in the next days.

  4. March 4, 2009 at 8:35 pm

    Is it already available in git?

  5. April 8, 2009 at 11:41 am

    Ali,
    I am a big proponent of Vala (especially as it approaches 1.0 and Gnome is looking toward 3.0). I’m also a long-time follower of the Soylent project, and recently stumbled upon People (That’s your project, isn’t it?).

    I’d love to interview you about it to help spread the word.

  1. January 26, 2011 at 4:14 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: