Test-Driven Android Development, Part 1
In this article series, we’re going to follow this road-map:
- Fundamentals of Test Driven Development(TDD)
- Designing for Test
- Introduction to Unit Testing
- Behavior-Driven Development(BDD)
- Add-ins & Plug-ins
- Testing Beyond the Unit Test
Fundamentals of TDD
- What is good Test Driven Design?
- Why do we do Test Driven Design?
We’re going to answer these questions together.
Quality is Free! In a short term of view it might say: When we write test for our application, we not adding some value, because we not adding new feature/functionality to the application that users able to see and work with.
But, the truth is the user work with a crash-free application that perform as expected.
For more information, you can read “Quality is Free by by Philip B. Crosby”.
Traditional Software Development Triangle:
It’s obvious what it says, but just to clarify it, for example if we have limited time and limited money, then we have to have limited features or we’re gonna to lose the quality.
Agile vs. Traditional:
Test First Design(Agile) is just about writing the test before writing the code. Why test first?
- Quality is too important to us.
- Define what quality code is, before writing the code.
These benefits bring us the other benefits that says:
- Stay focused on the writing high quality code.
- We know when coding is completed, because we passed the test.
- Sometimes, the one who start a module to code, is not the one who finish that module. So, if we write the test first, we make a contract between developers that says this module have to be done this way.
The Testing Pyramid:
GUI Tests:
To test elements on the UI. For example the button has a correct label or the AlertDialog is shown. This kind of test is written by Developer himself and/or QA(In case of using some external frameworks like MonkeyRunner)
Integration Tests:
To test each part of the application work together well. It usually run slower and has more codes unlike Unit Test. This kind of test is written by Developer himself, and run on emulator or real device; So, they have access to instrumentation APIs.
Note: This tests have their own AndroidManifest.xml
.
Unit Testing:
To test each individual reusable module. The definition from Google:
This type of test verifies that the target app behaves as expected when a user performs a specific action or enters a specific input in its activities.
Note: In compare to the GUI and Integration tests, Unit testing has most value.
This kind of test is written by Developer himself and run on local JVM; So, they are run so fast.
Tasks of each team in the testing process:
Business Analysts: Write User stories and use cases to define what the software would have to do.
Developers: Write the tests; So, when the tests pass, that means the software is doing what it would have to do.
QA, Developer, Automation: Run the tests.
In summary, to write a test, the developer must clearly understand the feature’s specification and requirements in which can accomplish this through use cases and user stories that comes from BA and/or PM.
Behavior-Driven Development(BDD):
A common language across teams, such as PM, BA, Developer, and Tester. It’s the way we communicate together. It’s English-like sentences known as Domain Specific Language(DSL).
With BDD, we can convert this comman language to a executable tests.
Principles:
- Full sentence for Unit test name.
- As a user I want feature so that benefits.(For more information, please take a look at here).
- Acceptance criteria: Given(some initial contexts), When(something happens), Then(some expectations).
- Define the unit test.
- Make test fail.
- Implement the feature.
- Verify the implementation pass the test.
Android Project Structure:
Unit tests goes in test
folder, and instrumentation tests goes though androidTest
folder.
What’s Next?
On the next article, we’ll go through “Designing for Test”.
Thank you for taking your precious time to reading this article. Please clap your 👏 and help others find this article too.
References:
- www.seguetech.com
- TDD by Brandan Jones