| 1 |
Ddoc |
|---|
| 2 |
|
|---|
| 3 |
$(SPEC_S $(TITLE), |
|---|
| 4 |
|
|---|
| 5 |
$(GRAMMAR |
|---|
| 6 |
$(GNAME UnitTest): |
|---|
| 7 |
$(B unittest) $(I FunctionBody) |
|---|
| 8 |
) |
|---|
| 9 |
|
|---|
| 10 |
$(P Unit tests are a series of test cases applied to a module to determine |
|---|
| 11 |
if it is working properly. Ideally, unit tests should be run every |
|---|
| 12 |
time a program is compiled. |
|---|
| 13 |
) |
|---|
| 14 |
|
|---|
| 15 |
$(P Unit tests are a special function defined like:) |
|---|
| 16 |
|
|---|
| 17 |
------ |
|---|
| 18 |
unittest |
|---|
| 19 |
{ |
|---|
| 20 |
...test code... |
|---|
| 21 |
} |
|---|
| 22 |
------ |
|---|
| 23 |
|
|---|
| 24 |
$(P There can be any number of unit test functions in a module, |
|---|
| 25 |
including within struct, union and class declarations. |
|---|
| 26 |
They are executed in lexical order. |
|---|
| 27 |
Stylistically, a unit test for a function should appear immediately |
|---|
| 28 |
following it. |
|---|
| 29 |
) |
|---|
| 30 |
|
|---|
| 31 |
$(P A compiler switch, such as $(LINK2 dmd-windows.html#switches, $(B -unittest)) |
|---|
| 32 |
for $(B dmd), will |
|---|
| 33 |
cause the unittest test code to be compiled and incorporated into |
|---|
| 34 |
the resulting executable. The unittest code gets run after |
|---|
| 35 |
static initialization is run and before the $(TT main()) |
|---|
| 36 |
function is called. |
|---|
| 37 |
) |
|---|
| 38 |
|
|---|
| 39 |
$(P For example, given a class Sum that is used to add two values:) |
|---|
| 40 |
|
|---|
| 41 |
------ |
|---|
| 42 |
class Sum |
|---|
| 43 |
{ |
|---|
| 44 |
int add(int x, int y) { return x + y; } |
|---|
| 45 |
|
|---|
| 46 |
unittest |
|---|
| 47 |
{ |
|---|
| 48 |
Sum sum = new Sum; |
|---|
| 49 |
assert(sum.add(3,4) == 7); |
|---|
| 50 |
assert(sum.add(-2,0) == -2); |
|---|
| 51 |
} |
|---|
| 52 |
} |
|---|
| 53 |
------ |
|---|
| 54 |
|
|---|
| 55 |
<h3>Versioning</h3> |
|---|
| 56 |
|
|---|
| 57 |
$(P The $(LINK2 version.html#PredefinedVersions, version identifier) |
|---|
| 58 |
$(B unittest) is predefined if the compilation |
|---|
| 59 |
is done with unit tests enabled. |
|---|
| 60 |
) |
|---|
| 61 |
|
|---|
| 62 |
) |
|---|
| 63 |
|
|---|
| 64 |
Macros: |
|---|
| 65 |
TITLE=Unit Tests |
|---|
| 66 |
WIKI=UnitTests |
|---|
| 67 |
GLINK=$(LINK2 #$0, $(I $0)) |
|---|
| 68 |
GNAME=<a name=$0>$(I $0)</a> |
|---|
| 69 |
DOLLAR=$ |
|---|
| 70 |
FOO= |
|---|