Sharing code between local and instrumentation testsAlex ZhukovichBlockedUnblockFollowFollowingFeb 1I would like to talk about sharing code between local and instrumentation test cases.
We often use almost similar test data for them.
It can be a factory which produces test data for us or predefined set of data.
We usually split local and instrumentation test cases into different folders; it means that we should duplicate test data or factories for generating test data.
Alternatively, we can create a common test folder where we can store files, classes, functions which will be available for local and instrumentation tests.
Recently was announced that we can run Espresso tests with a Robolectric framework.
However, we would like to avoid code duplication if we want to have a possibility of running the same tests cases as local and instrumentation tests.
We have different folders for local and instrumentation tests, and we can create a shared folder for test cases which can be run as local or instrumentation tests.
Sharing code allows us to reduce code duplication for test data and test cases.
Video tutorial about “Sharing code between local and instrumentation tests”Sharing test data between local and instrumentation test casesLet’s take a look at different test cases which use the same data.
I want to show you test cases from MapNotes app.
The source code of the app you can find on GitHub.
MapNotes applicationWe can start with local unit test cases, which verify different that SignInPresenter works correctly.
As you can see, we use prepared earlier data for verification sign in process.
In this case, we use CORRECT_EMAIL, PASSWORD and AUTH_USER constants.
The same set of test data we can use for verification SignIn screen with UI instrumentation test case.
The main problem here is that we store the same data twice for local and instrumentation test cases.
As you can see, we have two almost similar files because they can have an additional set of data which is used only for some category of tests (local or instrumentation).
We can avoid file duplication with creating a shared folder for local and instrumentation test cases and store test data in this directory because after it we will have access to this data from both categories of tests.
First of all, we should go to the Project Structure view and change it to Project.
After it, we can have the same structure, as we have on a disk, which helps us to configure a shared folder and see it at the beginning in Android Studio.
Afterwards, we should create a new folder; I prefer to call it “sharedTest“.
However, everything depends on the purposes of the folder.
I’ll create a “sharedTest” folder where I want to store test data and test cases for similar scenarios for local and instrumentation tests.
The next step is to create a “java” folder if you use the default structure.
When it’s done, we can create a package where we want to store our test data, in my case, it’s a com.
Finally, we can move to the build.
We should open this file and add a sourceSets section to android one.
After moving files to the shared folder and removing them from test and androidTest folders, we avoid duplication and all test cases work correctly.
Another approach is using factories which will generate test data for us, like TestDataFactory.
Sharing test code and running the same scenarios with local and instrumentation testsLet’s start with checking dependencies which project should have for Espresso, Robolectric, Runners and extensions.
I grouped all dependencies for better reading, and everything works fine with these versions of dependencies.
Afterwards, we should move the LoginActivityTest test class to sharedTest folder.
After it, we will have a problem with dependencies because we are using the few modules for dependency injection.
It means that we should move TestAppModule file with all dependencies and FakeMapFragment as one of the dependencies which should be available for TestAppModule.
Finally, we can run our tests without any modification.
Let’s check these tests with local and instrumentation tests and see the time difference between them.
Finally, we can create configurations for running LoginActivityTest as local and as instrumentation tests or use gradle tasks.
Running a specific test class as instrumentation tests use gradle:.
/gradlew :MODULE:connectedVARIANTAndroidTest -Pandroid.
/gradlew :app:connectedDebugAndroidTest -Pandroid.
LoginActivityTestRunning a specific test class as local tests use gradle:.
/gradlew :MODULE:testVARIANTUnitTest –tests "PACKAGE.
/gradlew :app:testDebugUnitTest –tests "com.
LoginActivityTest"I had the next results:Instrumentation tests: 10s 792msLocal test: 4s 669msTo sum up, sharing code between different categories of tests helps you to reduce code duplication for test data and test in general when we use the same test cases for local and instrumentation when we want to run tests with Robolectric and Espresso.
This article was originally posted here.
If you have any questions please reach out on Twitter or leave a comment.
Alex Zhukovich (@Alex_Zhukovich) | TwitterThe latest Tweets from Alex Zhukovich (@Alex_Zhukovich).
Speaker, Android developer, Blogger.
comMore video tutorials:AlexZh DevAndroid development and testing tutorialswww.