Android TDD and Roboelectric
Syed Rehan Ahmed
Manager SQA | Test Automation | Test Architect | OTT | Fintech | Ride Hailing | MS Project Management | ISTQB-CTFL? Certified
I have been learning about Android development and test-driven development lately, came across the Roboelectric framework and its concept of running tests inside JVM
So, basically during development when you are practicing Red Green Refactoring you need to run tests multiple times and it would be a great hassle for the Android development since all the time emulator will start, it eventually costs you a lot of time and effort. Even it leads developers to miss scenarios and test cases.
Roboelectric helps a lot to follow TDD as it makes it much more simple and convenient, as compared to JUnit which usually be the choice of most of the developers, but it's not solely focused on android and the general flow is you need to build, deploy and launch the app which obviously takes some time, and not efficient during development. So, the Roboelectric framework refines this by running inside the JVM on the workstation in seconds which is much faster. So, because of this, the dexing, packaging, and installing-on-the emulator steps aren’t necessary, reducing test cycles from minutes to seconds, so you can iterate quickly and refactor your code with confidence.
The other alternative of Robolectric is to use mocking frameworks like Mockito for mocking out the android SDK but Roboelectric has its own advantages as it focuses on the behavior of the application instead of the implementation which makes it more effective in refactoring.
Simple test written using Roboelectric is,
@RunWith(RobolectricTestRunner.class)
public class MyActivityTest {
? @Test
? public void clickingButton_shouldChangeMessage() {
? ? MyActivity activity = Robolectric.setupActivity(MyActivity.class);
? ? activity.button.performClick();
? ? assertThat(activity.message.getText()).isEqualTo("Robolectric Rocks!");
? }
}
Learn More :
Would love to know your thoughts!
SDET Professional | Enhancing Agile Teams with Scalable Testing Frameworks and Robust Automation
2 年nice ??