Testing Flashcards

1
Q

What are the source sets of your project?

A

main
- the app’s code and resources

test

  • for the app’s local unit tests
  • run locally on machine’s JVM (high speed, low fidelity)

androidTest

  • for the Android instrumented tests
  • run on a real / emulated device (low speed, high fidelity)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

List the basic set up steps required for JUnit tests

A
@RunWith(JUnit4.class)
@SmallTest
public class MyTest {
private ClassUnderTest myClass;
    @Before
    public void setUp( ) {
        myClass = new MyClassUnderTest();
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are some basic assertions?

A

assertEquals( )
assertNull( )

assertThat(val1, is(equalTo(“required val”))

equalTo( ) is a matcher

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which matcher can be used to mitigate for the unpredictable nature of float values?

A

Replace equalTo( ) matcher with closeTo(expectedValue, errorMargin)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the code to implement hamcrest?

A

testImplementation “org.hamcrest:hamcrest-all:$hamcrestVersion”

implementation - available to all source sets

testImplementation & androidTestImplementation - only available in corresponding source sets

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to get a mock context using AndroidX with JUnit4?

A

1 implement these:

testImplementation “androidx.test.ext:junit-ktx:$androidXTestExtKotlinRunnerVersion”

testImplementation “androidx.test:core-ktx:$androidXTestCoreVersion”

testImplementation "org.robolectric:robolectric:$robolectricVersion"
// Robolectric provides the simulated Android environment
2. Add @RunWith(AndroidJUnit4::class) above your test class.
// replaces default test runner; enables AndroidX Test to work with both unit and instrumented tests
  1. ApplicationProvider.getApplicationContext( )

// this is simulated in unit tests, but it gets the real context for instrumented tests - same library (AndroidX Test) works for both

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to fix the following warnigs?

  1. No such manifest file: ./AndroidManifest.xml
  2. “WARN: Android SDK 29 requires Java 9…”
A
1. 
testOptions.unitTests {
        includeAndroidResources = true
... 
    }
// the manifest is one of these resources
  1. Configure AS to use Java 9 (too complicated for the Codelab)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. What does InstantExecutorRule do?
  2. What is its dependency?
  3. Syntax in the Test file?
A
  1. A JUnit Rule that
    a) runs some before and after code
    b) runs all Architecture Component-related jobs synchronously on the same thread.
  2. testImplementation “androidx.arch.core:core-testing:$archTestingVersion”
  3. class MyViewModelTest {
    @get:Rule
    var instantExecutorRule = InstantTaskExecutorRule( )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. What makes LiveData difficult to implement in unit test files?
  2. How to solve this?
A
  1. A LiveData needs an Observer
    An Observer needs a LifecycleOwner (usually a Fragment or an Activity)
    No Fragment / Activity is available to a unit test.
2. 
val observer = Observer^LiveData^Type^^ { }
// empty lambda - doesn't need to do anything
myLiveData.observeForever(observer)
// must remember to 
finally{
.removeObserver(observer)
}
// get the value
val value = myLiveData.value

Alternatively, use some boilerplate from the Codelab in a util function that adds an observer, gets the value and removes the observer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which 3 options is it a good idea to disable when running Espresso tests?

A

Window animation scale
Transition animation scale
Animator duration scale

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What lines need to be present in build.gradle(Module) to run Espresso?

What are they for?

A

testImplementation ‘junit:junit:4.12’

androidTestImplementation ‘com.android.support.test:runner:1.0.1’

androidTestImplementation ‘com.android.support.test.espresso:espresso-core:3.0.1’

// to execute tests, Espresso and UI Automator use JUnit as their testing framework

// at the end of defaultConfig{ }
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
// enables hooks controlling an Android component independently of the component's normal lifecycle
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. Give syntax for using the 3 different types of Hamcrest expressions for Espresso
  2. Which rule needs to be implemented at the top of TestClass in order to use them?
A

1.
// ViewMatcher (finds the view)
onView(withId(R.id.my_view))
// ViewAction (performs an action on the View)
.perform(click( ))
// ViewAssertion (checks the state of a View)
.check(matches(isDisplayed( )));

2.
@Rule
val mActivityRule = ActivityTestRule^^(MainActivity.class);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When using Espresso, how do you assert the state of a Child View?

A

Iterate through the child views using an array (add a String array to strings.xml to facilitate finding each id)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly