Practicing Unit Test and TDD part 1
Today, i’m very curious about Unit Test especially Unit Test in android. So, first what is Unit Test, according to wikipedia (http://en.wikipedia.org/wiki/Unit_testing) : unit testing is a software testing method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures are tested to determine if they are fit for use. In this article i’m using Eclipse (https://eclipse.org/) as IDE and jUnit (http://junit.org/) to do the unit test.
Example you have this method :
public int add(int number1, int number2){
return number1 + number2;
}
How you make sure that the add method give the correct result. Yes, by using unit test. This is the sample code of unit test to test add method :
@Test public void testAdd(){
assertEquals(3, add(1,2));
assertNotSame(5, add(7,25));
}
to test your method is by comparing your actual scenario expected result and actual result from the method by using assertEquals to check if the expected result and actual result is the same. And to make sure the result is not do the bad calculation you can use assertNotSame. If you want to more digging about unit test checkout this book *Pragmatic Unit Testing Java Unit (http://www.amazon.com/Pragmatic-Unit-Testing-Java-JUnit/dp/0974514012) or download read the PDF (http://www.infotest.by/documents/Pragmatic_unit_testing.pdf). And you can checkout my unit test about TicTacToe game in my github (https://github.com/linggom/TicTacToe-TDD)
Next Part is TDD,
According to wikipedia TDD is a software development process that relies on the repetition of a very short development cycle: first the developer writes an (initially failing) automated test case that defines a desired improvement or new function, then produces the minimum amount of code to pass that test, and finally refactors the new code to acceptable standards (http://en.wikipedia.org/wiki/Test-driven_development). TDD is create by Kent Beck (http://en.wikipedia.org/wiki/Kent_Beck), he is currently work in Facebook as Programmer and mentor.
In my TicTacToe Game (https://github.com/linggom/TicTacToe-TDD) that i build, i’m using TDD as software development process and have some unit test for the TicTacToe api. That is how TDD is basically work, next part i will write about how to do the TDD in simple way. See you