listing multiple unit test failures

Christopher Wright dhasenan at gmail.com
Tue Mar 11 15:46:01 PDT 2008


Spacen Jasset wrote:
>     I have started some unit tests:
> 
>     unittest
>     {
>         // Check row column indexing is correct
>         Matrix    m;
>         m[2,0] = 59;
>         assert(m.matrix_flat[2]==59, "opIndexAssign failed");
>         assert(m.matrix_flat[8]!=59, "opIndexAssign failed");
>        
>     }
> 
> If an assert fails the other assertions in a program do not fire. This 
> is inconvient since the "real bug" introduced may be caused by some 
> other assertion that has not been evaluated yet.
> 
> What I am looking for, therefore is more of a writefln of each failure, 
> together with a single assertion to abort the program if any test has 
> failed.
> 
> If there isn't such a feature then I suppose I need a "Global" class 
> which holds a bit flag to be set when an assertion is registered, and 
> then in main this class could be made to assert if the flag is set.

You're looking for DUnit, which hasn't been written yet. I'd like to 
write such a thing, but a natural, xUnit-like syntax is difficult in D.

If you want something that targets D2, that's doable; you could write 
something like:

import dunit.api;
class FooTests : TestFixture
{
    mixin(DunitTest);
    override void setup () {}
    override void teardown () {}
    void testSomething()
    {
       // ...
    }
}

But in D1, there's no way (as far as I know) to iterate over the methods 
for a type.

Well, no, there is the vtbl. But that is a REALLY ugly solution.

So you're left with some syntax like:

class FooTests : TestFixture
{
    mixin(DunitTest);
    override void setup () {}
    override void teardown () {}
    this ()
    {
       tests["test for something"] =
       {
          // blah
       }
       tests["test for something else"] =
       {
          // blah
       }
    }
}

I guess that isn't so ugly. I can hack something together tonight.


More information about the Digitalmars-d-learn mailing list