Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Reaping the Benefits of TDD for Mobile App Testing & Development

Test-driven Development – TDD for short – is one of those terms that developers and leaders of development teams are often confronted with during their daily lives. We recently published a blog post about a relative of TDD, BDD (Behavior-Driven-Development), and talked about its use cases and benefits. We want to continue this journey and will now examine TDD in more detail: What is it, how does TDD for Mobile App Testing and development work, and which test frameworks are best suited for utilizing TDD for iOS and Android?

What Is Test-Driven Development?

In older, more traditional development and testing processes, testing happens at the end of the development cycle or after a requirement is completed. Yet, this can lead to a variety of problems, such as biased results, since developers often write tests to match the Code, not the other way around. Also, due to a lack of time and similar issues, development teams can easily neglect testing with this approach, especially if everything seems to work perfectly.

Test-driven development is a programming and testing practice that encourages developers to write better and more precise tests and code. For this, they write unit tests before the code of the respective functionality to ensure bug-free software and applications.

What Are Unit Tests?

In general, unit tests aren’t tied to TDD, but engineering teams from various industries take advantage of this method today. The idea behind unit tests is to ensure that components and other small pieces of functionality work independently of one other. Additionally, each test must focus on individual functionality, which makes unit tests quite small and, therefore, usually quick to write. Engineers provide some input to their code under test and expect a specific output from it. If the output is correct, the test passes; otherwise, it fails. As a result, you can also highly automate unit tests to make them more efficient.

How Does TDD Work?

When following the TDD approach, developers work through three stages to end up with clean and working code:

  1. RED: Write a unit test and execute it. Naturally, the test must fail (red) since there’s no code under test yet.
  2. GREEN: Write code that passes (green) the previously written test.
  3. REFACTOR: Remove redundancy and enhance the written code to make it clean, reusable, and presentable. While refactoring, the functionality must not be changed, so the test still passes (keep it green).

Nobody’s perfect, and often we don’t find the best or prettiest solution to a problem the first time we try. So even though developers sometimes pay less attention to the refactoring stage, it’s necessary to enhance and embellish the written code to align it with the rest of the codebase. In TDD, a developer’s job isn’t done when the code works, and there’s always room for improvement!

What Are the Advantages of Test-Driven Development?

Since engineers write tests before the code, TDD enables teams to spot issues early in development, which reduces the number of bugs in your application and saves you time and headaches later. For many, increased test coverage is one of TDD’s key benefits. With code coverage of around 80-100%, bugs have a harder time sneaking through unnoticed. For the same reason, developers can write and push code with more confidence, since the high test coverage ensures that no show-stopping bugs get into production.

Other notable advantages of TDD for mobile app testing include:

  • Increased developer productivity
  • Time-savings when fixing bugs (especially before the app’s release)
  • Ensuring a clean codebase with readable, non-redundant code
  • Reduced chances of breaking existing functionalities
  • Heightened test coverage

TDD for Android With Robolectric

Many developers use JUnit to write repeatable tests for their Java projects. However, since JUnit doesn’t solely focus on Android, let’s take a quick look at another popular unit test framework.

To follow TDD for mobile app testing on Android, you can make use of the hugely popular Robolectric framework, which is designed to make Android testing much more convenient and straightforward.

One reason why Robolectric is so popular with Android developers is that it doesn’t require a device or emulator to run on. Usually, engineers need to build, deploy, and launch the app, which can take a few minutes. However, as you can imagine, these are not optimal circumstances to perform unit tests productively during development. To improve things, Robolectric runs directly inside the JVM (Java Virtual Machine) of a developer’s workstation, which is way faster.

Here is an example of a simple test using the Robolectric framework (Source: robolectric.org):

@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!");
  }
}

The framework replaces all Android classes with so-called shadow objects, handles inflation of views, resource handling, and more so that you can perform almost all actions like on a real device.

As a result, developers can write more effective unit tests, refactor code faster, and can concentrate on creating an excellent user experience, rather than on Android’s implementation.

TDD for iOS With XCTest

In comparison, iOS developers find it a little bit easier to pick the right test framework for TDD since they can use XCTest, Apple’s testing framework, which is directly integrated into Xcode. One of the key benefits of XCTest is that the framework is always up to date with the latest iOS and Xcode versions. As a result, developers can effortlessly run automated unit tests without moving away from their favorite IDE.

XCTest is fully compatible with both Objective-C and Swift, making writing tests a straightforward task for any iOS developer. You can either use a simulator or real physical devices to execute tests on-premises or in the cloud. Since you can also use XCTest to create basic tests on the UI level, it makes sense to run tests on multiple real devices simultaneously in order to shorten test execution time. If you’d like to learn how to run XCTest test cases in our real device cloud, read our documentation page here.

Here’s an example of a simple test written with the XCTest framework (Source: developer.apple.com):

- (void) testAddition
{
   // obtain the app variables for test access
   app                  = [NSApplication sharedApplication];
   calcViewController   = (CalcViewController*)[[NSApplication sharedApplication] delegate];
   calcView             = calcViewController.view;
 
 
   [calcViewController press:[calcView viewWithTag:13]];  // +
   [calcViewController press:[calcView viewWithTag: 2]];  // 2
   [calcViewController press:[calcView viewWithTag:12]];  // =
    XCTAssertEqualObjects([calcViewController.displayField stringValue], @"10", @"Part 2 failed.");
}

Conclusion

TDD is an excellent way for your engineers to write better code in less time and with fewer errors. However, as with most agile methods, a learning phase is mandatory. Thus, it takes some time before you can reap the benefits of practicing TDD for mobile app testing and development. Rather than offering you quick and fading benefits, TDD can lead to a paradigm shift for your developers that increases productivity and app quality, the more they get used to working with it.

XCTest and Robolectric are currently the most popular unit test frameworks for iOS and Android, as they allow developers to perform tests quickly and regularly, so they can make the best use of TDD.

Of course, TDD isn’t the only development approach out there! Feel free to check out our other blog posts that help you get a flying start into streamlining your development and test processes.

  • Enhance Mobile Dev & Testing Process with BDD and Cucumber
  • TDD vs. BDD vs. ATDD – What’s the Right Choice for You?

Header Image by MetsikGarden via [source link] (copyright-free)

The post Reaping the Benefits of TDD for Mobile App Testing & Development appeared first on Bitbar.



This post first appeared on Mobile App Testing Blog, With Games And Web | Test, please read the originial post: here

Share the post

Reaping the Benefits of TDD for Mobile App Testing & Development

×

Subscribe to Mobile App Testing Blog, With Games And Web | Test

Get updates delivered right to your inbox!

Thank you for your subscription

×