Last active
February 15, 2018 17:07
-
-
Save zawadz88/04e25ab261795240c8b8a208c5e7b0a6 to your computer and use it in GitHub Desktop.
Custom Filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.stepstone.base.test.runner.filter | |
| import android.os.Bundle | |
| import com.stepstone.base.BuildConfig | |
| import com.stepstone.base.test.runner.annotation.TestFilter | |
| import org.junit.runner.Description | |
| import timber.log.Timber | |
| /** | |
| * Filter used to determine if a test should be executed or not. | |
| * | |
| * This does the following: | |
| * | |
| * - runs all tests if 'class' bundle argument was provided to the runner (this means that either Android Test Orchestrator wants to run a single test or that we want to run a test from Android Studio directly) | |
| * - checks if [TestFilter] is present on a test method -> if so it filters the test based on that annotation | |
| * - checks if [TestFilter] is present on a test class -> if so it filters the test based on that annotation | |
| * - otherwise it runs the test | |
| * | |
| * | |
| * [TestFilter] filtering runs the following checks in order: | |
| * | |
| * - if flavor is not present in [TestFilter.flavors] it will not run the test | |
| * - if flavor is present in [TestFilter.notFlavors] it will not run the test | |
| * | |
| * To run a test on a single flavor e.g. flavor1 you need to add [TestFilter] to a test method like this: | |
| * | |
| * `@TestFilter(flavors = [FLAVOR_1])` | |
| * | |
| * To run a test on all flavors but one (flavor4) you need to add [TestFilter] to a test method like this: | |
| * | |
| * `@TestFilter(notFlavors = [FLAVOR_4])` | |
| * | |
| * @see org.junit.runner.manipulation.Filter | |
| */ | |
| class FlavorFilter(bundle: Bundle) : ParentFilter() { | |
| companion object { | |
| private const val CLASS_BUNDLE_KEY = "class" | |
| } | |
| private val shouldFilterTests: Boolean | |
| init { | |
| Timber.i("FlavorFilter bundle: %s", bundle) | |
| val listTestsForOrchestrator = java.lang.Boolean.parseBoolean(bundle.getString("listTestsForOrchestrator", "false")) | |
| val runsSingleClass = bundle.containsKey(CLASS_BUNDLE_KEY) | |
| shouldFilterTests = listTestsForOrchestrator && !runsSingleClass | |
| } | |
| override fun evaluateTest(description: Description): Boolean { | |
| if (!shouldFilterTests) { | |
| return true | |
| } | |
| val classTestFilter = description.testClass.getAnnotation(TestFilter::class.java) | |
| val testFilter = description.getAnnotation(TestFilter::class.java) | |
| if (testFilter != null) { | |
| return evaluateTestWithFilter(testFilter) | |
| } else if (classTestFilter != null) { | |
| return evaluateTestWithFilter(classTestFilter) | |
| } | |
| return true | |
| } | |
| private fun evaluateTestWithFilter(testFilter: TestFilter): Boolean { | |
| val filters = testFilter.filters | |
| val notFilters = testFilter.notFilters | |
| return (filters.contains(BuildConfig.FLAVOR) | |
| && !notFilters.contains(BuildConfig.FLAVOR)) | |
| } | |
| override fun describe(): String { | |
| return "Flavor filter" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment