02 Cucumber Introduction - TDD
02 Cucumber Introduction - TDD
TDD is an iterative development process. Each iteration starts with a set of tests written for a
new piece of functionality. These tests are supposed to fail during the start of iteration as
there will be no application code corresponding to the tests. In the next phase of the
iteration, Application code is written with an intention to pass all the tests written earlier in
the iteration. Once the application code is ready tests are run.
Any failures in the test run are marked and more Application code is written/re-factored to
make these tests pass. Once application code is added/re-factored the tests are run again.
This cycle keeps on happening until all the tests pass. Once all the tests pass we can be sure
that all the features for which tests were written have been developed.
Benefits of TDD
1. Unit test proves that the code actually works
2. Can drive the design of the program
3. Refactoring allows improving the design of the code
4. Low-Level regression test suite
5. Test first reduce the cost of the bugs
Drawbacks of TDD
1. Developer can consider it as a waste of time
2. The test can be targeted on verification of classes and methods and not on what the code
really should do
3. Test become part of the maintenance overhead of a project
4. Rewrite the test when requirements change
package source;
}
We will write all our tests against this class. Here is how our Test 1 will look like. We will put
all our Adding tests in a class called AddingNumbersTests
package AppTests;
import org.junit.Assert;
import org.junit.Test;
import source.Calculator;
@Test
public void addTwoPositiveNumbers()
{
int expectedResult = 30;
int ActuaResult = myCalculator.Add(10, 20);
Assert.assertEquals("The the sum of two positive numbers is correct" ,
expectedResult, ActuaResult);
}
@Test
public void addTwoNegativeNumbers()
{
int expectedResult = -30;
int ActuaResult = myCalculator.Add(-10, -20);
Assert.assertEquals("The the sum of two negative numbers is correct" ,
expectedResult, ActuaResult);
}
}
Now the very first thing that will come to our mind is that the Calculator class doesn't have
any methods and in our tests, we have used a method named Add() on calculator class. This
will give us a compilation error. Well, that's the whole point of writing the tests first. This will
force us to add only the code that's necessary. Ignoring the compilation error, let's just move
on to the next step.
package source;
With this change, we will move to the next step that is rerun our tests. Which is nothing but
Phase 2 mentioned earlier. Let's see what is the test result that we get this time.
Results from the two tests is
java.lang.AssertionError: The the sum of two positive numbers is incorrect expected:<30> but
was:<0> at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
And
java.lang.AssertionError: The the sum of two negative numbers is incorrect expected:<-30>
but was:<0> at org.junit.Assert.fail(Assert.java:88)
Now with this test failure, we conclude that addition of two positive and negative numbers
is not happening properly. Based on the test failure we will add just enough code that these
two tests pass. As we do this we move to the next phase which is Phase 3. This phase is
already describer earlier. I will just show you how the code of our Calculator will look like
after this phase
package source;
Now we will run our tests Phase 2. The test results after this change will make all our tests
pass. Once all the tests will pass we will conclude that our Iteration has been completed.
Tests results:
Once all the tests pass it signals the end of iteration. If there are more features that need to
be implemented in your product, product will go through the same phases again but this
time with new feature set and more tests.
Summarizing it in a figure can be done like this
With this understanding of TDD we will move to BDD. Which will form the basis of
understanding Gherkin and eventually Cucumber.