unit_threaded

Advanced unit-testing.

Modules

attrs
module unit_threaded.attrs

This module defines UDAs to be used on unit tests.

io
module unit_threaded.io

IO related functions

options
module unit_threaded.options

Command-line options for running unittests.

reflection
module unit_threaded.reflection

Compile-time reflection to find unittests and properties specified via UDAs.

runner
module unit_threaded.runner

This module implements functions to run the unittests with command-line options.

runtime
module unit_threaded.runtime

This module implements a template mixin containing a program to search a list of directories for all .d files therein, then writes a D program to run all unit tests in those files using unit_threaded. The program implemented by this mixin only writes out a D file that itself must be compiled and run.

should
module unit_threaded.should

This module implements custom assertions via shouldXXX functions that throw exceptions containing information about why the assertion failed.

testcase
module unit_threaded.testcase

Implementations of TestCase child classes.

tests
module unit_threaded.tests
testsuite
module unit_threaded.testsuite

This module implements TestSuite, an aggregator for TestCase objects to run all tests.

Public Imports

unit_threaded.should
public import unit_threaded.should;
Undocumented in source.
unit_threaded.testcase
public import unit_threaded.testcase;
Undocumented in source.
unit_threaded.io
public import unit_threaded.io;
Undocumented in source.
unit_threaded.reflection
public import unit_threaded.reflection;
Undocumented in source.
unit_threaded.runner
public import unit_threaded.runner;
Undocumented in source.
unit_threaded.runtime
public import unit_threaded.runtime;
Undocumented in source.

Examples

1 
2 @name("testTimesTwo")
3 unittest
4 {
5     int timesTwo(int i) { return i * 2; }
6 
7     2.timesTwo.shouldEqual(4);
8     3.timesTwo.shouldEqual(6);
9 }
10 
11 @name("testRange")
12 unittest
13 {
14     import std.range: iota;
15     iota(3).shouldEqual([0, 1, 2]);
16     3.shouldBeIn(iota(5));
17 }
18 
19 @name("testIn")
20 unittest
21 {
22     auto ints = [1, 2, 3, 4];
23     3.shouldBeIn(ints);
24     1.shouldBeIn(ints);
25     5.shouldNotBeIn(ints);
26 }
27 
28 @name("testThrows")
29 unittest
30 {
31     void throwRangeError()
32     {
33         ubyte[] bytes;
34         bytes = bytes[1..$];
35     }
36     import core.exception: RangeError;
37     throwRangeError.shouldThrow!RangeError;
38 }
39 
40 //a test that is expected to fail
41 @name("testOops")
42 @shouldFail("Bug #1234")
43 unittest
44 {
45     3.shouldEqual(5); //won't cause the suite to fail
46 }
47 
48 //prevent data races
49 __gshared int i;
50 
51 @name("sideEffect1")
52 @serial //all @serial tests in a module run in the same thread
53 unittest
54 {
55     i++;
56     i.shouldEqual(1);
57 }
58 
59 @name("sideEffect2")
60 @serial //all @serial tests in a module run in the same thread
61 unittest
62 {
63     i++;
64     i.shouldEqual(2);
65 }

Meta

License

<a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.

Authors

Atila Neves

D's unittest blocks are a built-in feature of the language that allows for easy unit testing with no boilerplate. As a program grows it's usual to need or want more advanced testing features, which are provided by this package.

The easiest way to run tests with the functionality provided is to have a D module implementing a main function similar to this one:

import unit_threaded;

int main(string[] args) {
     return runTests!("name.of.mymodule",
                      "name.of.other.module")(args);
}

This will (by default) run all unittest blocks in the modules passed in as compile-time parameters in multiple threads. Unit tests can be named: to do so simply use the supplied name UDA. There are other supplied UDAs. Please consult the relevant documentation.

As an alternative to writing a program like the one above manually, the included gen_ut_main.d file can be run as a script and will generate such a file. This can be run as part of the build system to recreate the unit test main file. gen_ut_main.d checks to see if the file list has changed and will not regenerate the file if that's not needed.