This module defines UDAs to be used on unit tests.
IO related functions
Command-line options for running unittests.
Compile-time reflection to find unittests and properties specified via UDAs.
This module implements functions to run the unittests with command-line options.
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.
This module implements custom assertions via shouldXXX functions that throw exceptions containing information about why the assertion failed.
Implementations of TestCase child classes.
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 }
<a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
Atila Neves
Advanced unit-testing.