Reputation: 3214
I've now written a few unittests using GLibs inbuilt unit tests, in Vala. That means everything is done as:
class test_some_class{
static void main(string[] args){
Test.ref(args);
Test.add_func("/some/path/test-some-functionality", test_some_functionality);
Test.run();
}
static void test_some_functionality() {
assert(true); //Or any other boolean expression such as 'a == b' as pointed out by @AlexB
}
}
There's nothing on valadoc specifying available functions to compare strings, doubles, etc.
While unittesting in general is tedious (but necessary) I find it particularly tedious here, these seems to be rather barebones unittests, missing much that is offered in JUnit and mstests (for .NET Core). I was wondering if someone can recommend a good unittesting framework written for/with GLib Tests, that offers something similar?
I have looked at valadate, but it does not seem to get a lot of attention these days, and while it may not need frequent updates, I have been quite unable to locate working samples, and the wiki does too seem to be offline. If anyone is currently using it, please speakup, and please do show some examples.
In particular it would be nice to be able to merely create the classes, declare the functions with decorations that specify them as tests that should be run.
Upvotes: 1
Views: 443
Reputation: 516
As others have pointed out, there's no great solution in the standard library unfortunately, especially since many of GLib's g_assert_foo
methods don't work or don't make sense under Vala.
For Geary, I've adopted this approach that uses a class-based framework similar to JUnit and integrates into the existing GLib test harness, but modified it to throw errors rather than use plain assert()
calls, and added support for mock objects, a wider range of assert_foo
methods, handle expected/unexpected errors and testing async methods. See the test directory in Geary's source, in particular the files test-case.vala
and mock-object.vala
.
It could certainly use a lot of cleanup, but is independent of Geary's application code, and so could easily be imported and used elsewhere. I'd be happy to split it out into a separate library if anyone is interested in using and contributing back to it.
Upvotes: 2