SlideServe
Menu
Préférences
© SlideServe · v2026

Testing Your App: Importance and Methods

craigc avatar
28 diapositives · 328 Views · Nov 07, 2019

Learn why testing is crucial for your app, types of testing, including unit testing, and how to run local unit tests in Android Studio using JUnit. Ensure your app functions flawlessly with effective testing strategies.

Testing Your App: Importance and Methods - slide 1
01 / 28Testing Your App: Importance and Methods
1 / 17

Télécharger la présentation

Testing Your App: Importance and Methods
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Android Developer Fundamentals Testing and Debugging, and Backwards Compatibility Lesson 3 This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Android Developer Fundamentals

  2. 3.2 Testing Your App This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License Testing your app Android Developer Fundamentals

  3. Contents • Why testing is worth your time • Unit testing Note: User interface testing (instrumented testing) is covered in a later chapter

  4. Testing Rocks

  5. Cost to Fix Why should you test your app? Catch bugs early! $1000 • Find and fix issues early • Less costly • Takes less effort • Costs to fix bugs increases with time $100 $10 Release Code Specification Design QA $1 Discovery Time

  6. Types of testing • Levels of Testing • Component, integration, protocol, system • Types of Testing • Installation, compatibility, regression, acceptance • Performance, scalability, usability, security • User interface and interaction tests • Automated UI testing tools • Instrumented testing (covered later)

  7. Test-Driven Development (TDD) • Define a test case for a requirement • Write tests that assert all conditions of the test case • Write code against the test • Iterate on and refactor code until it passes the test • Repeat until all requirements have test cases, all tests pass, and all functionality has been implemented

  8. Tests in your project Android Studio creates three source sets for your project • main—code and resources • test—local unit tests • androidTest—instrumented tests

  9. Local Unit Tests

  10. Unit tests • Smallest testable parts of your program • Isolate each component and demonstrate the individual parts are correct • Java Method tests Results Inputs Java method Graceful Failure

  11. Local unit tests in JUnit • Compiled and run entirely on your local machine with the Java Virtual Machine (JVM) • Use to test the parts of your app (such as the internal logic) that do not need access to the Android framework or an Android device or emulator, or those for which you can create fake (mock) objects that pretend to behave like the framework equivalents • Unit tests are written with JUnit, a common unit testing framework for Java.

  12. Local unit tests in your project • Tests are in the same package as the associated application class. • Only org.junit imported - no Android classes • Project path for test classes: .../module-name/src/test/java/

  13. Imports for JUnit // Annotations import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; // Basic JUnit4 test runner import org.junit.runners.JUnit4; // assertThat method import static org.junit.Assert.assertThat;

  14. Testing class /** * JUnit4 unit tests for the calculator logic. * These are local unit tests; no device needed */ @RunWith(JUnit4.class) // Specify the test runner public class CalculatorTest { // Name it what you are testing }

  15. ExampleTest / ** * Test for simple addition. * Each test is identified by a @Test annotation. */ @Test public void addTwoNumbers() { double resultAdd = mCalculator.add(1d, 1d); assertThat(resultAdd, is(equalTo(2d))); }

  16. @Test Annotation • Tells JUnit this method is a test method (JUnit 4) • Information to the test runner • Not necessary anymore to prefix test methods with "test"

  17. setUp() method /** * Set up the environment for testing */ @Before public void setUp() { mCalculator = new Calculator(); } • Sets up environment for testing • Initialize variables and objects used in multiple tests

  18. tearDown() method /** * Release external resources */ @After public void tearDown() { .... } • Frees resources

  19. Running Tests in Android Studio

  20. Starting a test run • Right-click test class and select Run 'app_name' test • Right-click test package and select Run tests in 'package'

  21. Passing and failing Pass Result details Fail Fail Pass

  22. Testing Floating Point Results

  23. Testing Floating Point • Be careful with floating point tests • Recall from basic computer science: • Floating point arithmetic is not accurate in binary

  24. Test fails with floating point numbers

  25. Fix test with floating point numbers They are the same within .0005 in this test

  26. Learn more • Getting Started with Testing • Best Practices for Testing • Building Local Unit Tests • JUnit 4 Home Page • JUnit 4 API Reference • Android Testing Codelab • Android Tools Protip: Test Size Annotations • Android Testing Support - Testing Patterns (video)

  27. What's Next? • Concept Chapter: 3.2 C Testing Your App • Practical: 3.2 P Testing Apps with Unit Tests

  28. END