Unit testing is an essential element of the application development process in which we test a specific section of the application's code to see that it's working correctly.
In D
programming language, there are two types of unit testing:
@system unittest {// Add the attribute of the function you wanna test}
@system
to verify whether the system is throwing code according to its assigned method.Let's look at the code below:
// Declare a functionint subtract(int a, int b) { return a - b; }// Call the build in unittest function@system unittest{ //Define a attribute testingassert(subtract(12,2) == 10);}void main() { }
subtract
function in which we subtract b
from a
and return the result.subtract
function by using @system unittest
.In documented testing, we first implement the test case on a particular function and then implement testing on the program by executing the program in the test case to verify that the example used for the function is correct or not.
lass deduct{int de(int x, int y) { return x - y; }// Call the build in unittest functionunittest{ // Define the test case}}unittest{//Define the test case on implemented program}void main() {}
Let's look at the code below:
class deduct{int de(int x, int y) { return x - y; }// Call the build in unittest functionunittest{ // Define the test casededuct d = new deduct;assert(d.de(12,3) == 9);}}unittest{auto t = new deduct();auto test = t.de(12,3);}void main() {}
deduct
class function.Free Resources