Giter Site home page Giter Site logo

google-developer-training / advanced-android-testing Goto Github PK

View Code? Open in Web Editor NEW
995.0 995.0 671.0 3.73 MB

Android Testing Codelab

Home Page: https://codelabs.developers.google.com/codelabs/android-testing/

License: Apache License 2.0

Kotlin 100.00%

advanced-android-testing's People

Contributors

adazh avatar ceruleanotter avatar dmeng avatar egor-n avatar freewheelnat avatar gen0083 avatar hongbeomi avatar islamsalah avatar jfschmakeit avatar josealcerreca avatar nkoroste avatar prattpratt avatar richarth avatar ryanseys avatar sebaslogen avatar slinzner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

advanced-android-testing's Issues

changing status in setRefreshing doesn't failing the test!

In Unit test Test Artifact, class NotesPresenterTest , loadNotesFromRepositoryAndLoadIntoView testcase:

verify(mNotesView).setProgressIndicator(false);

when flipping false to true, it still passing the test ! even it get called from presenter with false!

to validate that do the following,
verify(mNotesView).setProgressIndicator(false);
verify(mNotesView).setProgressIndicator(true);
verify(mNotesView).setProgressIndicator(false);

it still pass!, not sure where is the bug!

Missing configuration/installation of Code Coverage functionality.

In my installation of Android Studio (2.2.3), there is no Code Coverage selection/configuration. It would be nice if the codelab could be extended to instruct the user on how to correct such a situation if it exists.

This link will provide the user with basic help on how to configure Code Coverage.
https://www.jetbrains.com/help/idea/2016.3/code-coverage.html

There are still a few small spelling and grammar mistakes, but we will overlook them.

This is a very good code lab to help the student get started with writing their own tests.

Kind regards,
MPongrac

Benfitials of MVP over Event drivven code

I'm sorry for creating an issue here maybe it was better to ask it on SO but I hope the creators of project or someone else answers my questions.
I've just become familiar with MVP pattern in android architecture, and packaging your classes over features instead of super classes.
Right now I use event driven architecture (Eventbus) in one of my apps. I was thinking what would be the benefit of using MVP over Event driven.
Using Eventbus I can seperate responsibilty of your methods very easily, So why would one migrate to MVP?
The other question is cant someone please overview the feasibility of using both MVP pattern and Event driven architecture?

The second question is the part you suggest using Dagger for mocking and stubbing, I see that's completely possible using a DI framework. but there's already mocking frameworks like mockito to mock your objects in test code instead of the source code, why should I use dagger in that sense?

Step 9 contains incorrect "Espresso-Intents" subheadings

In Step 9, the "Espresso-Contrib" section, there are two subheadings for the test cases sections labelled "Espresso-Intents Test #". As this is the "Espresso-Contrib" section, the subheadings should be "Espresso-Contrib Test #".

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with text: is "Notes cannot be empty"

Running errorShownOnEmptyMessage test I always get android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with text: is "Notes cannot be empty" exception.

These are my dependencies:

ext {
  // Sdk and tools
  minSdkVersion = 19
  targetSdkVersion = 26
  compileSdkVersion = 26
  buildToolsVersion = '26.0.1'

  // App dependencies
  supportLibraryVersion = '26.1.0'
  guavaVersion = '20.0'
  glideVersion = '3.6.1'
  junitVersion = '4.12'
  mockitoVersion = '1.10.19'
  powerMockito = '1.6.2'
  hamcrestVersion = '1.3'
  runnerVersion = '1.0.1'
  rulesVersion = '1.0.1'
  espressoVersion = '3.0.1'
}

I also replaced deprecated registering / unregistering methods for Idling Resources:

@Before public void registerIdlingResource() {
    IdlingRegistry.getInstance()
        .register(mAddNoteIntentsTestRule.getActivity().getCountingIdlingResource());
  }
@After public void unregisterIdlingResource() {
    IdlingRegistry.getInstance()
        .unregister(mAddNoteIntentsTestRule.getActivity().getCountingIdlingResource());
  }

Step 7 : "R.id.add_note_description)).perform(typeText(newNoteDescription)" fails to click at Android 22+

When running the test at:
Step 7 - Running Espresso tests

I see the test fail when testing using emulators at Android versions 22 and 23. It succeeds at version 19.
There is something about the presence of the soft keyboard that interferes with the click function - adding a click (commented out below) didn't help. The workaround is to close the soft keyboard after the first typeText operation. This is shown (commented out) below.

public void addNoteToNotesList() throws Exception {
    [...]
    // Add note title and description
    onView(withId(R.id.add_note_title)).perform(typeText(newNoteTitle)  /* ,
            closeSoftKeyboard() */  ); // Type new note title

    // onView(withId(R.id.add_note_description)).perform(click());

    onView(withId(R.id.add_note_description)).perform(typeText(newNoteDescription),
            closeSoftKeyboard()); // Type new note description and close the keyboard

The error thrown is:

Error performing 'single click' on view 'with id: com.example.android.testing.notes:id/add_note_description'

Android support v4 and v7

I never understand how you can publish a codelab that builds just fine however the Espresso test have all types of build errors that are related to support libraries v4 and v7. When will you fix this nonsense and why don't you keep up with this stuff after it's published.

After updating to support library v23.3.0+, the FAB cannot be anchored to the CoordinatorLayout

After updating the support library version from 23.1.1 to 23.3.0, the project breaks from a change to the CoordinatorLayout requirements. The FAB (floating action button) can no longer be anchored to the Coordinator Layout, but instead must be anchored to the contained FrameLayout. This needs to be changed in the activity_addnote.xml and activity_notes.xml files:

<android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
          ...
        <FrameLayout
            android:id="@+id/contentFrame"
            ...
        <android.support.design.widget.FloatingActionButton
            (***CHANGE HERE from coordinatorLayout to contentFrame)
            app:layout_anchor="@id/contentFrame"

Here is a stackoverflow answer that provides this guidance.

http://stackoverflow.com/questions/35843705/view-can-not-be-anchored-to-the-the-parent-coordinatorlayout

Typo in Chapter 7, 8, 9

The following text is duplicated right at the beginning of Chapter 7, 8 and 9:

If you would like to start fresh for this section, continue from snapshot β€˜step-7/8/9’:

[Question] What this code does?

I'm sorry for opening an issue but i dont know where else i can ask a question:

In the AddNotePresentar constructor there is this line:
addNoteView.setUserActionListener(this); (line 49)

Isn't this redundant because the constructor already set the mActionListener in the AddNoteFragment (line 79) or is there some usecase that i'm not following with that listener?

Cheers

7th step typo

https://codelabs.developers.google.com/codelabs/android-testing/index.html?index=../../index#6

The 7th step has typo

Launch NoteDetailActivity with an Intent extra parameter
Edit the file app/src/androidTestMock/java/.../notes/NoteDetailScreenTest.java and comment in the method as you work through this section:

should be

Launch NoteDetailActivity with an Intent extra parameter
Edit the file app/src/androidTestMock/java/.../notedetail/NoteDetailScreenTest.java and comment in the method as you work through this section:

Add CI

@slinzner Could we add TravisCI so users can see a working example of espresso?

dagger2

I have forked this project and created a new branch using dagger2.
See here
https://github.com/cpeppas/android-testing/tree/dagger

I thought this might be useful because dagger2 is becoming very popular nowadays.

Because of the fact that I cannot do a PL of a new branch that does not exist on the original project that's why I am raising this as an issue

Let me know if you think it is somehow valuable so I can do a PL in some way.

Run-on sentences make tutorial hard to read

Not an issue with the code but I don't know where else to put this. There are a lot of run-on sentences in the tutorial text that make it hard to follow the tutorial at times.
Examples:

Our repository here is only a mock object (and doesn't have any actual functionality), we need to fake the callback on the listener ourselves.

By default, all calls to the provided Android.jar throw exceptions, this allows you to write tests that do not depend on any Android platform behavior.

typo step 7

duplication of text. A sentence is repeated twice in a row.

If you would like to start fresh for this section, continue from snapshot β€˜step-7’:If you would like to start fresh for this section, continue from snapshot β€˜step-7’:

variantFilter causing an issue.

Cloning this and opening in Android Studio 1.5.1 I get the following error:

Error:(35, 0) No signature of method: java.lang.String.variantFilter() is applicable for argument types: (build_8svktq4s2wa741jcavj9ayd99$_run_closure1_closure8) values: [build_8svktq4s2wa741jcavj9ayd99$_run_closure1_closure8@191ee85a]

This refers to the app/build.gradle line

// Remove mockRelease as it's not needed.
android.variantFilter { variant ->
    if(variant.buildType.name.equals('release')
            && variant.getFlavors().get(0).name.equals('mock')) {
        variant.setIgnore(true);
    }
}

I tried changing android.variantFilter to applicationVariants.all which then moved the problem to getFlavors().

Am I using the wrong version of gradle?

com.android.dex.DexException when executing CodeCoverage task

On master branch executing

./gradlew createMockDebugCoverageReport

produces the following

UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Landroid/support/design/R$anim;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:502)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:334)
at com.android.dx.command.dexer.Main.run(Main.java:277)
at com.android.dx.command.dexer.Main.main(Main.java:245)
at com.android.dx.command.Main.main(Main.java:106)

Btw..Great codelab! Thanks for sharing.

IncompatibleClassChangeError

When running the first espresso test from step 7 i got this error:

java.lang.IncompatibleClassChangeError: com.example.android.testing.notes.notes.NotesActivity

the only way i was able to fix it was to include in the gradle file:
androidTestCompile ("com.android.support.test.espresso:espresso-contrib:$rootProject.ext.espressoVersion") {
exclude group: 'com.android.support', module: 'appcompat'
exclude group: 'com.android.support', module: 'support-v4'
exclude module: 'recyclerview-v7'
}

Am i doing something wrong?

Btw, great codelab :)

androidTestMock : No tests were found error, "Empty Test Suite"

I am hitting this error at the following codelab step:

Espresso test 3: Clicking a note

The Android studio setup is shown in the snips below on Windows 7. I have not found a solution for this problem. It appears that the use of the "AndroidTestMock" facility is not in wide use - and so there is little to be found out there for guidance. Hmm.

image

image

Process finished with exit code 1
Class not found: "com.example.android.testing.notes.notedetail.NoteDetailScreenTest"Empty test suite.

Espresso Test 2: Adding a note FAIL

I just run code with the doc in Section of Espresso Test 2: Adding a note in Nexus 5 (API 23). Here is the logcat:

android.support.test.espresso.PerformException: Error performing 'scroll RecyclerView to: holder with view: has descendant: with text: is "UI testing for Android"' on view 'with id: com.example.android.testing.notes.mock:id/notes_list'.
at android.support.test.espresso.PerformException$Builder.build(PerformException.java:83)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:70)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:53)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184)
at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:115)
at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:87)
at com.example.android.testing.notes.notes.NotesScreenTest.addNoteToNotesList(NotesScreenTest.java:123)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:257)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:240)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)
Caused by: java.lang.RuntimeException: Found 0 items matching holder with view: has descendant: with text: is "UI testing for Android", but position -1 was requested.
at android.support.test.espresso.contrib.RecyclerViewActions$ScrollToViewAction.perform(RecyclerViewActions.java:357)
at android.support.test.espresso.ViewInteraction$1.run(ViewInteraction.java:144)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Thanks for fix it.

step 7: Studio can't find APK while trying to run AndroidTest config

I hit run button for Tests in com.android... build config.
See gradle tasks: Executing tasks: [:app:assembleProdDebug, :app:assembleProdDebugAndroidTest]
Finally the run console says:

11/02 19:00:04: Launching Tests in 'com.example.android.testing.notes'
$ adb push /Users/bag/codelab/android-testing/app/build/outputs/apk/app-prod-debug.apk /data/local/tmp/com.example.android.testing.notes
$ adb shell pm install -r "/data/local/tmp/com.example.android.testing.notes"
WARNING: linker: /system/lib/libhoudini.so has text relocations. This is wasting memory and prevents security hardening. Please fix.
	pkg: /data/local/tmp/com.example.android.testing.notes
Success


The APK file /Users/bag/codelab/android-testing/app/build/outputs/apk/app-prod-debug-androidTest.apk does not exist on disk.
Error while Installing APK

It's looking for the app-prod-debug-androidTest.apk while there is no zipaligned version of this apk, there are only:

bag@iMac-Dmitry:~/codelab/android-testing/app/build/outputs/apk (step-7) $ ls
app-prod-debug-androidTest-unaligned.apk
app-prod-debug-unaligned.apk
app-prod-debug.apk

If I manually rename the file to app-prod-debug-androidTest.apk, the job completes:

11/02 18:57:31: Launching Tests in 'com.example.android.testing.notes'
$ adb push /Users/bag/codelab/android-testing/app/build/outputs/apk/app-prod-debug.apk /data/local/tmp/com.example.android.testing.notes
$ adb shell pm install -r "/data/local/tmp/com.example.android.testing.notes"
WARNING: linker: /system/lib/libhoudini.so has text relocations. This is wasting memory and prevents security hardening. Please fix.
	pkg: /data/local/tmp/com.example.android.testing.notes
Success


No apk changes detected since last installation, skipping installation of /Users/bag/codelab/android-testing/app/build/outputs/apk/app-prod-debug-androidTest.apk
$ adb shell am force-stop com.example.android.testing.notes.test
Running tests

$ adb shell am instrument -w -r   -e package com.example.android.testing.notes -e debug false com.example.android.testing.notes.test/android.support.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests

What to do to run the tests?

Codelab zip files show as encrypted on Windows 7

Hi All,

Thanks very much for setting up this codelab for testing. It is great! One note - this was new to me. The files in the zip file at the codelab download web page - when unpacked on a Windows 7 system - show as encrypted. The zip file was downloaded from here:

https://codelabs.developers.google.com/codelabs/android-testing/#1

Some filenames show in a green color in Windows Explorer, indicating that they are encrypted. Evidently this is some interesting interaction where files encrypted on a Mac OS system are tagged with a bit that is confused by Windows as needing encryption. My solution - I will switch to pulling the files for the code lab from github. You can right click to Properties -> Advanced to clear the encryption status, but that is a hassle. :-)

Here is more info on the interesting interaction issue:

https://blogs.msdn.microsoft.com/asklar/2012/05/03/why-do-zip-files-from-mac-os-show-up-as-greenencrypted/

New Gradle sync icon in Android Studio 2.2+

In step 6 there is a section 'Optionally sync your project'. With this section there is a line of text showing the button to sync Gradle.

This button icon has changed in Android Studio 2.2 so it might be worth showing both icons.

"Step 7: Espresso Test 2: Adding a note" refers to wrong view

In the following description, after the FAB is clicked, the text says that Espresso will wait for the new screen, by waiting on the View with id R.id.fab_add_notes. This should instead reference a view on the new screen, i.e. R.id.add_note_title. I've marked the incorrect id in the excerpt below:

Similar to the previous Espresso test we looked at, we will first find the floating action button (R.id.fab_add_notes) and click it:

// Click on the add note button
onView(withId(R.id.fab_add_notes)).perform(click());

Next (once the "add note screen" is open) we will enter our note text and save the note. (We don't need to wait for the new screen to open - Espresso will do this automatically for us. It waits until a View with the id >>>R.id.fab_add_notes<<< can be found.)

// Add note title and description
onView(withId(R.id.add_note_title)).perform(typeText(newNoteTitle), closeSoftKeyboard()); // Type new note title
onView(withId(R.id.add_note_description)).perform(typeText(newNoteDescription),
       closeSoftKeyboard()); // Type new note description and close the keyboard

typo in step 6

there is a line that says:

First we capture the callback in the captor, then we call it with an empty list of notes:

which is referring to this code:

mLoadNotesCallbackCaptor.getValue().onNotesLoaded(NOTES);

However NOTES is not an empty list. So either use the EMPTY_NOTES field in the code or change the description so that it refers to a list of notes rather than empty list of notes.

code coverage user interface is pretty different now...

In the step "Bonus: Code Coverage" the latest Android Studio version (2.1.1) is quite different from what is discussed in:

https://codelabs.developers.google.com/codelabs/android-testing/#10

After some fumbling around I found the code coverage controls and am now happily experimenting with them. There does seem to be some documentation here that matches what I see in Android Studio:

https://www.jetbrains.com/help/idea/2016.1/viewing-code-coverage-results.html

Basically a right click on the tests in the Project view provides a "Run soAndSo with Coverage" while mysteriously this is sometimes greyed out in the Run menubar drop-down. The "Coverage" side tab or View then Tool Windows then Coverage selection gives you the Coverage view of the underlying code. And the various packages are then annoted in the Project window with the percentage of test coverage. Rather nice!!

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.