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
voidshouldEqual(T, U)(Ta, Ub, stringmessage= null, stringfile= __FILE__, size_tline= __LINE__);Used to assert that one value is equal to another value.
Parameters
TaThe value to test.
UbThe value it should be equal to.
stringfileThe
filename that the assert failed in. Should be left as default.size_tlineThe
filelinethat 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
voidshouldNotEqual(T, U)(Ta, Ub, stringmessage= null, stringfile= __FILE__, size_tline= __LINE__);Used to assert that one value is NOT equal to another value.
Parameters
TaThe value to test.
UbThe value it should NOT be equal to.
stringfileThe
filename that the assert failed in. Should be left as default.size_tlineThe
filelinethat 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
voidshouldBeNull(T)(Ta, stringmessage= null, stringfile= __FILE__, size_tline= __LINE__);Used to assert that one value is equal to
null.Parameters
TaThe value that should equal
null.stringfileThe
filename that the assert failed in. Should be left as default.size_tlineThe
filelinethat 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
voidshouldNotBeNull(T)(Ta, stringmessage= null, stringfile= __FILE__, size_tline= __LINE__);Used to assert that one value is NOT equal to
null.Parameters
TaThe value that should NOT equal
null.stringfileThe
filename that the assert failed in. Should be left as default.size_tlineThe
filelinethat 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
voidshouldBeIn(T, U)(Tvalue, U[]valid_values, stringfile= __FILE__, size_tline= __LINE__);Used to assert that one
valueis in an array of specified values.Parameters
TvalueThe
valueto test.U[]valid_valuesAn array of valid values.
stringfileThe
filename that the assert failed in. Should be left as default.size_tlineThe
filelinethat the assert failed in. Should be left as default.Throws
If the
valueis not in the array, will throw an AssertError with thevalueand array values.Examples
// Will throw an exception like "AssertError@example.d(6): <Bobrick> is not in <[Tim, Al]>." "Bobrick".shouldBeIn(["Tim", "Al"]);
-
Declaration
voidshouldBeGreater(T, U)(Ta, Ub, stringmessage= null, stringfile= __FILE__, size_tline= __LINE__);Used to assert that one value is greater than another value.
Parameters
TaThe value to test.
UbThe value it should be greater than.
stringfileThe
filename that the assert failed in. Should be left as default.size_tlineThe
filelinethat 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
voidshouldBeLess(T, U)(Ta, Ub, stringmessage= null, stringfile= __FILE__, size_tline= __LINE__);Used to assert that one value is less than another value.
Parameters
TaThe value to test.
UbThe value it should be less than.
stringfileThe
filename that the assert failed in. Should be left as default.size_tlineThe
filelinethat 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
voidshouldBeGreaterOrEqual(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
TaThe value to test.
UbThe value it should be greater or equal than.
stringfileThe
filename that the assert failed in. Should be left as default.size_tlineThe
filelinethat 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
voidshouldBeLessOrEqual(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
TaThe value to test.
UbThe value it should be less or equal than.
stringfileThe
filename that the assert failed in. Should be left as default.size_tlineThe
filelinethat 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
voidshouldThrow(void delegate()cb, stringmessage= null, stringfile= __FILE__, size_tline= __LINE__);Used for asserting that a delegate will throw an exception.
Parameters
void delegate()cbThe delegate that is expected to throw the exception.
stringmessageThe
messagethat is expected to be in the exception. Will not be tested, if it isnull.stringfileThe
filename that the assert failed in. Should be left as default.size_tlineThe
filelinethat 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
intprintResults();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
voiddescribe(TestPair...)(stringdescribe_message, TestPairpairs);The message is usually the name of the thing being tested.
Parameters
stringdescribe_messageThe thing that is being described.
TestPairpairsAll the 'it' delegate functions that will test the thing.
Examples
describe("example_library#thing_to_test", it("Should NOT fail", delegate() { // code here }) );
-
Declaration
TestPairit(stringmessage, void delegate()func);The
messageshould describe what the test should do.Parameters
stringmessageThe
messageto print when the test fails.void delegate()funcThe 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); }) );