-
Notifications
You must be signed in to change notification settings - Fork 11
/
README
232 lines (181 loc) · 6.24 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
Unit testing framework ('dunit')
Allows to define unittests simply as methods which names start
with 'test'.
The only thing necessary to create a unit test class, is to
declare the mixin TestMixin inside the class. This will register
the class and its test methods for the test runner.
License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
Authors: Juan Manuel Cabo
Version: 0.6
Source: dunit.d
Last update: 2012-03-21
Copyright Juan Manuel Cabo 2012.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
---------------------------------------------------------------------------------
module ExampleTests;
import std.stdio, std.string;
import core.thread;
import dunit;
//Minimal example:
class ATestClass {
mixin TestMixin;
void testExample() {
assertEquals("bla", "b"~"la");
}
}
/**
* Look!! no test base class needed!!
*/
class AbcTest {
//This declaration here is the only thing needed to mark a class
//as a unit test class:
mixin TestMixin;
//Variable members that start with 'test' are allowed.
public int testN = 3;
public int testM = 4;
//Any method whose name starts with 'test' is run as a unit test:
//(NOTE: this is bound at compile time, there is no overhead).
public void test1() {
assert(true);
}
public void test2() {
//You can use D's assert() function:
assert(1 == 2 / 2);
//Or dunit convenience asserts (just edit dunit.d to add more):
assertEquals(1, 2/2);
//The expected and actual values will be shown in the output:
assertEquals("my string looks dazzling", "my dtring looks sazzling");
}
//Test methods with default arguments work, as long as they can
//be called without arguments, ie: as testDefaultArguments() for instance:
public void testDefaultArguments(int a=4, int b=3) {
}
//Even if the method is private to the unit test class, it is still run.
private void test5(int a=4) {
}
//This test was disabled just by adding an underscore to the name:
public void _testAnother() {
assert(false, "fails");
}
//Optional initialization and de-initialization.
// setUp() and tearDown() are called around each individual test.
// setUpClass() and tearDownClass() are called once around the whole unit test.
public void setUp() {
}
public void tearDown() {
}
public void setUpClass() {
}
public void tearDownClass() {
}
}
class DerivedTest : AbcTest {
mixin TestMixin;
//Base class tests will be run!!!!!!
//
//You can for instance override setUpClass() and change the target
//implementation of a family of classes that you are testing.
//
//For instance: Run a set of tests against all derived classes of
//the Stream class. You do this by keeping all the tests in a parent
//test class, and creating a derived TestFixture for each one,
//that all it has to do is instantiate the instance under test in the
//overriden setUpClass().
}
/**
* You can write asynchronous tests too!! Test those socket listeners of
* yours, or your active thread objects, etc.!!
*/
class AsynchronousTestExample {
mixin TestMixin;
Thread theThread;
bool threadDidItsThing;
//Prepare the test:
void setUp() {
threadDidItsThing = false;
theThread = new Thread(&threadFunction);
}
//Cleanup:
void tearDown() {
theThread.join();
theThread = null;
}
void threadFunction() {
threadDidItsThing = true;
}
void testThreadActuallyRuns() {
assertEquals(false, threadDidItsThing);
//Start the thread
theThread.start();
//Assert that within a period of time (500ms by default), the variable
//threadDidItsThing gets toggled:
assertWithTimeout({return threadDidItsThing;});
}
}
version = DUnit;
version(DUnit) {
//-All you need to run the tests, is to declare
//
// mixin DUnitMain.
//
//-You can alternatively call
//
// dunit.runTests_Progress(); for java style results
// output (SHOWS COLORS IF IN UNIX !!!)
// or
// dunit.runTests_Tree(); for a more verbose output
//
//from your main function.
mixin DUnitMain;
//void main() {dunit.runTests_Tree();}
} else {
int main (string[] args) {
writeln("production");
}
}
/*
* Alternatively, you can run your DUnit tests when passing -unittest
* to the compiler. This only needs to be declared once for the whole
* application, and will run all the tests in all modules before the
* application starts:
*/
unittest {
dunit.runTests();
}
/*
Run this file with (works in Windows/Linux):
dmd exampleTests.d dunit.d
./exampleTests
The output will be (java style):
...F....F...
There were 2 failures:
1) test2(AbcTest)[email protected](61):
Expected: 'my string looks dazzling', but was: 'my dtring looks sazzling'
2) test2(DerivedTest)[email protected](61):
Expected: 'my string looks dazzling', but was: 'my dtring looks sazzling'
FAILURES!!!
Tests run: 10, Failures: 2, Errors: 0
If you use the more verbose method dunit.runTests_Tree(), then the output is:
Unit tests:
ATestClass
OK: 0.01 ms testExample()
AbcTest
OK: 0.00 ms test1()
FAILED: test2(): [email protected](62):
Expected: 'my string looks dazzling', but was:
'my dtring looks sazzling'
OK: 0.00 ms testDefaultArguments()
OK: 0.00 ms test5()
DerivedTest
OK: 0.01 ms test1()
FAILED: test2(): [email protected](62):
Expected: 'my string looks dazzling', but was:
'my dtring looks sazzling'
OK: 0.00 ms testDefaultArguments()
OK: 0.00 ms test5()
AsynchronousTestExample
OK: 11.00 ms testThreadActuallyRuns()
HAVE FUN!
*/