How do I test callbacks/delegates which are not triggered
immediately? Especially when I need to do unit tests with an
event loop?
A simple example from my codebase (SaaSy here is a custom HTTP
client to an API endpoint):
class TestSaaSy {
mixin TestMixin;
// this works
void test_encoded_auth() {
auto ev = new EventLoop;
auto saasy = new SaaSy(ev, "test", "test");
assert(saasy.encoded_auth == "dGVzdDp0ZXN0dGVzdA==",
"encoding issue");
ev.close;
ev.run;
}
// won't work. test gets finished even before the callback is
fired!
void test_get_subscription() {
auto ev = new EventLoop;
auto saasy = new SaaSy(ev, "test", "test");
saasy.getSubscription("ref363466", (bool err, string
response) {
assert(err == true);
ev.close;
});
ev.run;
}
}
On Sunday, 19 February 2012 at 15:30:33 UTC, Juan Manuel Cabo
wrote:
People of the D world.. I give you DUnit (not to be confused
with an old
tango DUnit, this one is for >= D2.057, and doesn't really
require phobos or tango (just you version the few writeln's of
the runner, and maybe
something else)).
https://github.com/jmcabo/dunit
I've been developing it for the past few weeks, and since I saw
a post of another unit testing framework just a few minutes
ago, I thought I'd rush it to github.
Soooo, here is how you define a test:
import dunit;
class Something {
mixin TestMixin;
void testOne() {
assert(1 == 1, "this works");
}
void testTwo() {
assertEquals(1, 2/2);
assertEquals("a string", "a"~" string");
}
}
.. and that's all there is to it. Put the mixin TestMixin, and
name your tests
starting with 'test'. The results output shows all of them even
if some fail, and... guess what, it tells you the name of the
unit tests that failed!! isn't this awesome!! (all thanks to
mixins, recursive template declarations, __traits, and a little
bit of CTFE)... isn't D like, so incredibly awesome or what!?!?
There is absolutely no overhead in registering the tests for
the test runner.. its all at compile time!
Your tests are inherited through derived classes, and can be
private in the unit test class (they will still run).
I made two test runners:
* One that shows the results in java style (but WITH
COLORS!! (fineprint: colors only on unix console, windows
console is colorless for now)
* Another one more verbose that shows the tree of tests as
it runs them.
It is very easy to make your own.
This is all BOOST licenced, so please tweak it away!
FINEPRINT yes shouting fineprint ;-) haha:
THIS IS NOT A unitest{} REPLACEMENT, JUST AN ITCH EVERY OOP
PEOPLE WANTED TO SCRATCH: named, easy, xUnit style unit tests..
AND NOW YOU'VE GOT THEM.