BDD
Behavior Driven Development for the D programming language
Discussion
Home page: https://github.com/workhorsy/BDD
License
Boost Software License - Version 1.0
Examples
import BDD;
int add(int a, int b) {
return a + b;
}
unittest {
describe("math#add",
it("Should add positive numbers", delegate() {
add(5, 7).shouldEqual(12);
}),
it("Should add negative numbers", delegate() {
add(5, -7).shouldEqual(-2);
})
);
}
// Prints the results of the tests
int main() {
return BDD.printResults();
}
-
Declaration
void
shouldEqual
(T, U)(Ta
, Ub
, stringmessage
= null, stringfile
= __FILE__, size_tline
= __LINE__);Used to assert that one value is equal to another value.
Parameters
T
a
The value to test.
U
b
The value it should be equal to.
string
file
The
file
name that the assert failed in. Should be left as default.size_t
line
The
file
line
that the assert failed in. Should be left as default.Throws
If values are not equal, will throw an AssertError with expected and actual values.
Examples
// Will throw an exception like "AssertError@example.d(6): <3> expected to equal <5>." int z = 3; z.shouldEqual(5);
-
Declaration
void
shouldNotEqual
(T, U)(Ta
, Ub
, stringmessage
= null, stringfile
= __FILE__, size_tline
= __LINE__);Used to assert that one value is NOT equal to another value.
Parameters
T
a
The value to test.
U
b
The value it should NOT be equal to.
string
file
The
file
name that the assert failed in. Should be left as default.size_t
line
The
file
line
that the assert failed in. Should be left as default.Throws
If values are NOT equal, will throw an AssertError with unexpected and actual values.
Examples
// Will throw an exception like "AssertError@example.d(6): <3> expected to NOT equal <3>." int z = 3; z.shouldNotEqual(3);
-
Declaration
void
shouldBeNull
(T)(Ta
, stringmessage
= null, stringfile
= __FILE__, size_tline
= __LINE__);Used to assert that one value is equal to
null
.Parameters
T
a
The value that should equal
null
.string
file
The
file
name that the assert failed in. Should be left as default.size_t
line
The
file
line
that the assert failed in. Should be left as default.Throws
If value is NOT
null
, will throw an AssertError.Examples
// Will throw an exception like "AssertError@example.d(6): expected to be <null>." string z = "blah"; z.shouldBeNull();
-
Declaration
void
shouldNotBeNull
(T)(Ta
, stringmessage
= null, stringfile
= __FILE__, size_tline
= __LINE__);Used to assert that one value is NOT equal to
null
.Parameters
T
a
The value that should NOT equal
null
.string
file
The
file
name that the assert failed in. Should be left as default.size_t
line
The
file
line
that the assert failed in. Should be left as default.Throws
If value is
null
, will throw an AssertError.Examples
// Will throw an exception like "AssertError@example.d(6): expected to NOT be <null>." string z = null; z.shouldNotBeNull();
-
Declaration
void
shouldBeIn
(T, U)(Tvalue
, U[]valid_values
, stringfile
= __FILE__, size_tline
= __LINE__);Used to assert that one
value
is in an array of specified values.Parameters
T
value
The
value
to test.U[]
valid_values
An array of valid values.
string
file
The
file
name that the assert failed in. Should be left as default.size_t
line
The
file
line
that the assert failed in. Should be left as default.Throws
If the
value
is not in the array, will throw an AssertError with thevalue
and array values.Examples
// Will throw an exception like "AssertError@example.d(6): <Bobrick> is not in <[Tim, Al]>." "Bobrick".shouldBeIn(["Tim", "Al"]);
-
Declaration
void
shouldBeGreater
(T, U)(Ta
, Ub
, stringmessage
= null, stringfile
= __FILE__, size_tline
= __LINE__);Used to assert that one value is greater than another value.
Parameters
T
a
The value to test.
U
b
The value it should be greater than.
string
file
The
file
name that the assert failed in. Should be left as default.size_t
line
The
file
line
that the assert failed in. Should be left as default.Throws
If the value is NOT greater, will throw an AssertError with expected and actual values.
Examples
// Will throw an exception like "AssertError@example.d(6): <5> expected to be greater than <10>." 5.shouldBeGreater(10);
-
Declaration
void
shouldBeLess
(T, U)(Ta
, Ub
, stringmessage
= null, stringfile
= __FILE__, size_tline
= __LINE__);Used to assert that one value is less than another value.
Parameters
T
a
The value to test.
U
b
The value it should be less than.
string
file
The
file
name that the assert failed in. Should be left as default.size_t
line
The
file
line
that the assert failed in. Should be left as default.Throws
If the value is NOT less, will throw an AssertError with expected and actual values.
Examples
// Will throw an exception like "AssertError@example.d(6): <10> expected to be less than <5>." 10.shouldBeLess(5);
-
Declaration
void
shouldBeGreaterOrEqual
(T, U)(Ta
, Ub
, stringmessage
= null, stringfile
= __FILE__, size_tline
= __LINE__);Used to assert that one value is greater or equal than another value.
Parameters
T
a
The value to test.
U
b
The value it should be greater or equal than.
string
file
The
file
name that the assert failed in. Should be left as default.size_t
line
The
file
line
that the assert failed in. Should be left as default.Throws
If the value is NOT greater or equal, will throw an AssertError with expected and actual values.
Examples
// Will throw an exception like "AssertError@example.d(6): <5> expected to be greater or equal to <10>." 5.shouldBeGreaterOrEqual(10);
-
Declaration
void
shouldBeLessOrEqual
(T, U)(Ta
, Ub
, stringmessage
= null, stringfile
= __FILE__, size_tline
= __LINE__);Used to assert that one value is less or equal than another value.
Parameters
T
a
The value to test.
U
b
The value it should be less or equal than.
string
file
The
file
name that the assert failed in. Should be left as default.size_t
line
The
file
line
that the assert failed in. Should be left as default.Throws
If the value is NOT less or equal, will throw an AssertError with expected and actual values.
Examples
// Will throw an exception like "AssertError@example.d(6): <10> expected to be less or equal to <5>." 10.shouldBeLessOrEqual(5);
-
Declaration
void
shouldThrow
(void delegate()cb
, stringmessage
= null, stringfile
= __FILE__, size_tline
= __LINE__);Used for asserting that a delegate will throw an exception.
Parameters
void delegate()
cb
The delegate that is expected to throw the exception.
string
message
The
message
that is expected to be in the exception. Will not be tested, if it isnull
.string
file
The
file
name that the assert failed in. Should be left as default.size_t
line
The
file
line
that the assert failed in. Should be left as default.Throws
If delegate does NOT throw, will throw an AssertError.
Examples
// Makes sure it throws with the message "boom!" shouldThrow(delegate() { throw new Exception("boom!"); }, "boom!"); // Makes sure it throws, but does not check the message shouldThrow(delegate() { throw new Exception("boom!"); }); // Will throw an exception like "AssertError@test/example.d(7): Exception was not thrown. Expected: boom!" shouldThrow(delegate() { }, "boom!"); // Will throw an exception like "AssertError@test/example.d(7): Exception was not thrown. Expected one. shouldThrow(delegate() { });
-
Declaration
int
printResults
();Prints the results of all the tests. Returns 0 if all tests pass, or 1 if any fail.
Examples
BDD.printResults();
Output:
Unit Test Results: 4 total, 2 successful, 2 failed math#add - "5 + 7 = 12: <12> expected to equal <123>." broken_math.d(2) math#subtract - "5 - 7 = -2: <-2> expected to equal <456>." broken_math.d(6)
-
Declaration
void
describe
(TestPair...)(stringdescribe_message
, TestPairpairs
);The message is usually the name of the thing being tested.
Parameters
string
describe_message
The thing that is being described.
TestPair
pairs
All the 'it' delegate functions that will test the thing.
Examples
describe("example_library#thing_to_test", it("Should NOT fail", delegate() { // code here }) );
-
Declaration
TestPair
it
(stringmessage
, void delegate()func
);The
message
should describe what the test should do.Parameters
string
message
The
message
to print when the test fails.void delegate()
func
The delegate to call when running the test.
Examples
int a = 4; describe("example_library#a", it("Should equal 4", delegate() { a.shouldEqual(4); }), it("Should Not equal 5", delegate() { a.shouldNotEqual(5); }) );