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
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

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.
The standard output on failure should be something like:
Expected: … But was: …
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.
Is it already available in git?
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.