What are the source sets of your project?
main
- the app’s code and resources
test
androidTest
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
// 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?
1.
testOptions.unitTests {
includeAndroidResources = true
...
}
// the manifest is one of these resources2.
val observer = Observer^LiveData^Type^^ { }
// empty lambda - doesn't need to do anythingmyLiveData.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 lifecycle1.
// 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)