unit test that show more than one failure

Dukc ajieskola at gmail.com
Tue Jun 16 10:45:14 UTC 2020


On Tuesday, 16 June 2020 at 06:19:51 UTC, Joel wrote:
> I've tired different unit test libraries, but they jump out on 
> errors instead of just adding to failed numbers.
>
> I'm thinking like this:
>
> ```
> @("dummy");
> unittset {
>   0.shouldEqual(0);
>   1.shouldEqual(2);
>   2.shouldEqual(3);
> }
> ```
>
> Test: dummy
> test passed line 10: 0 is equal to 0
> test failed line 11: 1 is not equal to 2
> test failed line 12: 2 is not equal to 3
>
> 1 passed
> 2 failed
>
> The unit tests I tried would jump out on the first failure.

I have solved this. In my project, I have two modules like this:

```
module assertions;

auto assert_(A)(A canditate)
{	assert(canditate);
}

auto assertOp(alias pred, A)(A canditate)
if (is(typeof(cast(bool) pred(canditate))))
{	assert(pred(canditate));
}

auto assertOp(alias pred, A, B)(A canditate, B check)
if (is(typeof(cast(bool) pred(canditate, check))))
{	assert(pred(canditate, check));
}

auto assertEmpty(A)(A aEl)
{	assertOp!(a => a.empty)(aEl);
}

auto assertEqual(A, B)(A aEl, B bEl)
{	assertOp!((a,b) => a == b)(aEl, bEl);
}

auto assertClose(A, B)(A aEl, B bEl)
{	import std.math;
	assertOp!((a,b) => a.isClose(b))(aEl, bEl);
}
```

```
module assertions.characterization;

auto assert_(A)(A canditate)
{	import std.stdio : writeln;
	writeln(canditate, cast(bool) canditate? " (passes)": " 
(fails)");
}

auto assertOp(alias pred, A)(A canditate)
if (is(typeof(cast(bool) pred(canditate))))
{	import std.stdio : writeln;
	writeln(canditate, cast(bool) pred(canditate)? " (passes)": " 
(fails)");
}

auto assertOp(alias pred, A, B)(A canditate, B check)
if (is(typeof(cast(bool) pred(canditate, check))))
{	import std.stdio : writeln;
	writeln(canditate, cast(bool) pred(canditate, check)? " 
(passes)": " (fails)");
}

auto assertEmpty(A)(A aEl)
{	assertOp!(a => a.empty)(aEl);
}

auto assertEqual(A, B)(A aEl, B bEl)
{	assertOp!((a,b) => a == b)(aEl, bEl);
}

auto assertClose(A, B)(A aEl, B bEl)
{	import std.math;
	assertOp!((a,b) => a.isClose(b))(aEl, bEl);
}
```

So I usually have the test like this:
```
@safe unittest
{  import assertions;
    0.assertEqual(0);
    1.assertEqual(2);
    2.assertEqual(3);
}
```

...but when it fails, I just change the import to 
`assertions.characterization`, and all test results, along with 
info whether they're correct, are printed.


More information about the Digitalmars-d-learn mailing list