unit-threaded
My DConf2016 Lightning talk demonstrating unit-threaded.
Multi-threaded advanced unit test framework for the D programming language.
Augments D’s unittest
blocks with:
- Tests can be named and individually run
- Custom assertions for better error reporting (e.g.
1.should == 2
) - Runs in threads by default
- UDAs for customisation of tests
- Property based testing
- Mocking
Quick start with dub
Note: while getting started this way is easy, it also increases build times and may run into edge cases. See below for how to do it manually.
dub runs tests with dub test
. Unfortunately, due to the nature of
D’s compile-time reflection, to use this library a test runner file
listing all modules to reflect on must exist. Since this is a tedious
task and easily automated, unit-threaded has a dub configuration
called gen_ut_main
to do just that. To use unit-threaded with a dub
project, you can use a unittest
configuration as exemplified in this
dub.json
:
{
"name": "myproject",
"targetType": "executable",
"targetPath": "bin",
"configurations": [
{ "name": "executable" },
{
"name": "unittest",
"targetType": "executable",
"preBuildCommands": ["$DUB run --compiler=$$DC unit-threaded -c gen_ut_main -- -f bin/ut.d -d $DUB"],
"mainSourceFile": "bin/ut.d",
"excludedSourceFiles": ["src/main.d"],
"dependencies": {
"unit-threaded": "*"
}
}
]
}
With dub.sdl
:
configuration "executable" {
}
configuration "unittest" {
dependency "unit-threaded" version="*"
mainSourceFile "bin/ut.d"
excludedSourceFiles "src/main.d"
targetType "executable"
preBuildCommands "$DUB run --compiler=$$DC unit-threaded -c gen_ut_main -- -f bin/ut.d -d $DUB"
}
excludedSourceFiles
is there to not compile the file containing the
main
function to avoid linker errors. As an alternative to using
excludedSourceFiles
, the “real” main
can be versioned out:
version(unittest) {
import unit_threaded;
mixin runTestsMain!(
"module1",
"module2",
// ...
);
} else {
void main() {
//...
}
}