Testing Flashcards
What are the source sets of your project?
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)
List the basic set up steps required for JUnit tests
@RunWith(JUnit4.class) @SmallTest public class MyTest {
private ClassUnderTest myClass;
@Before public void setUp( ) { myClass = new MyClassUnderTest(); }
What are some basic assertions?
assertEquals( )
assertNull( )
assertThat(val1, is(equalTo(“required val”))
equalTo( ) is a matcher
Which matcher can be used to mitigate for the unpredictable nature of float values?
Replace equalTo( ) matcher with closeTo(expectedValue, errorMargin)
What is the code to implement hamcrest?
testImplementation “org.hamcrest:hamcrest-all:$hamcrestVersion”
implementation - available to all source sets
testImplementation & androidTestImplementation - only available in corresponding source sets
How to get a mock context using AndroidX with JUnit4?
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
- 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 to fix the following warnigs?
- No such manifest file: ./AndroidManifest.xml
- “WARN: Android SDK 29 requires Java 9…”
1. testOptions.unitTests { includeAndroidResources = true ... } // the manifest is one of these resources
- Configure AS to use Java 9 (too complicated for the Codelab)
- What does InstantExecutorRule do?
- What is its dependency?
- Syntax in the Test file?
- A JUnit Rule that
a) runs some before and after code
b) runs all Architecture Component-related jobs synchronously on the same thread. - testImplementation “androidx.arch.core:core-testing:$archTestingVersion”
- class MyViewModelTest {
@get:Rule
var instantExecutorRule = InstantTaskExecutorRule( )
- What makes LiveData difficult to implement in unit test files?
- How to solve this?
- 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.
Which 3 options is it a good idea to disable when running Espresso tests?
Window animation scale
Transition animation scale
Animator duration scale
What lines need to be present in build.gradle(Module) to run Espresso?
What are they for?
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
- Give syntax for using the 3 different types of Hamcrest expressions for Espresso
- Which rule needs to be implemented at the top of TestClass in order to use them?
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);
When using Espresso, how do you assert the state of a Child View?
Iterate through the child views using an array (add a String array to strings.xml to facilitate finding each id)