assert unittest doesn't respect assertThrown

Brother Bill brotherbill at mail.com
Tue Jan 20 20:42:21 UTC 2026


In a unittest, we have assertThrown average([1], [1, 2]));
As assert statement throws an exception.
But the unittest fails, which seems odd, as an assert did thr
Have I found another bug in DMD?

```
import std.stdio : writeln;
import std.exception : enforce, assertThrown, assertNotThrown;

void main()
{
	auto result = average([], []);

	// assert is not caught in catch block
	// enforce is caught in catch block
	try
	{
		result = average([1], [1, 2]);
	}
	catch (Exception e)
	{
		writeln("Caught exception: ", e.msg);
	}	
}

int[] average(int[] a, int[] b)
{
	assert( a.length == b.length, "assert:  Input slices must have 
the same length");	// assert  will not be caught in catch block
	// enforce(a.length == b.length, "enforce: Input slices must 
have the same length");	// enforce will     be caught in catch 
block

	// Implementation goes here
	return cast(int[])[]; // Initial result, so it compiles
}

unittest
{
	/* Must throw for uneven slices */
	assertThrown(average([1], [1, 2]));

	/* Must not throw for empty slices */
	assertNotThrown(average([], []));
}
```

Running it in Linux Terminal
```
bb at fedora:~/temp/c42_p217_6a_testing_for_exceptions$ dub test 
--force    # with assert only
              No source files found in configuration 'library'. 
Falling back to default configuration for test runner.
     Starting Performing "unittest" build using /usr/bin/dmd for 
x86_64.
     Building c42_6a_testing_for_exceptions ~master: building 
configuration [application]
      Linking c42_6a_testing_for_exceptions
      Running c42_6a_testing_for_exceptions
core.exception.AssertError at source/app.d(22): assert:  Input 
slices must have the same length
----------------
??:? _d_assert_msg [0x40301c]
source/app.d:22 int[] app.average(int[], int[]) [0x400d6e]
source/app.d:32 pure @safe int[] 
app.__unittest_L29_C1().__dgliteral_L32_C22() [0x400e46]
/usr/include/dmd/phobos/std/exception.d:294 pure nothrow @safe 
void std.exception.assertThrown!(Exception, 
int[]).assertThrown(lazy int[], immutable(char)[], 
immutable(char)[], ulong) [0x4025b2]
source/app.d:32 void app.__unittest_L29_C1() [0x400daf]
??:? void app.__modtest() [0x402cbc]
??:? int 
core.runtime.runModuleUnitTests().__foreachbody_L603_C5(object.ModuleInfo*) [0x40c52e]
??:? int object.ModuleInfo.opApply(scope int 
delegate(object.ModuleInfo*)).__lambda_L2519_C13(immutable(object.ModuleInfo*)) [0x40a087]
??:? int rt.minfo.moduleinfos_apply(scope int 
delegate(immutable(object.ModuleInfo*))).__foreachbody_L585_C5(ref rt.sections_elf_shared.DSO) [0x412593]
??:? int rt.sections_elf_shared.DSO.opApply(scope int 
delegate(ref rt.sections_elf_shared.DSO)) [0x412be5]
??:? int rt.minfo.moduleinfos_apply(scope int 
delegate(immutable(object.ModuleInfo*))) [0x412521]
??:? int object.ModuleInfo.opApply(scope int 
delegate(object.ModuleInfo*)) [0x40a059]
??:? runModuleUnitTests [0x40c363]
??:? void rt.dmain2._d_run_main2(char[][], ulong, extern (C) int 
function(char[][])*).runAll() [0x403c0c]
??:? void rt.dmain2._d_run_main2(char[][], ulong, extern (C) int 
function(char[][])*).tryExec(scope void delegate()) [0x403b99]
??:? _d_run_main2 [0x403b0f]
??:? _d_run_main [0x403917]
/usr/include/dmd/druntime/import/core/internal/entrypoint.d:29 
main [0x400e99]
??:? [0x7f80e860f5b4]
??:? __libc_start_main [0x7f80e860f667]
??:? _start [0x400b64]
1/1 modules FAILED unittests
Error Program exited with code 1
```


More information about the Digitalmars-d-learn mailing list