Skip to content

Commit

Permalink
Add TestStorageResultsWriter to write test results into sdcard when u…
Browse files Browse the repository at this point in the history
…sing androidx test orchestrator
  • Loading branch information
kamilwawrzyczek committed Mar 17, 2022
1 parent 8283fd5 commit 14632e2
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.qameta.allure.android.listeners

import android.os.Build
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.runner.*
import org.junit.runner.notification.*

class ExternalStoragePermissionsListener : RunListener() {

override fun testRunStarted(description: Description?) {
InstrumentationRegistry.getInstrumentation().uiAutomation.apply {
val testServicesPackage = "androidx.test.services"
when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.R -> {
executeShellCommand("appops set $testServicesPackage MANAGE_EXTERNAL_STORAGE allow")
}
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP -> {
executeShellCommand("pm grant $testServicesPackage android.permission.READ_EXTERNAL_STORAGE")
executeShellCommand("pm grant $testServicesPackage android.permission.WRITE_EXTERNAL_STORAGE")
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.runner.AndroidJUnitRunner
import io.qameta.allure.android.AllureAndroidLifecycle
import io.qameta.allure.android.internal.isDeviceTest
import io.qameta.allure.android.listeners.ExternalStoragePermissionsListener
import io.qameta.allure.android.writer.TestStorageResultsWriter
import io.qameta.allure.kotlin.Allure
import io.qameta.allure.kotlin.junit4.AllureJunit4
import io.qameta.allure.kotlin.util.PropertiesUtils
import org.junit.runner.*
import org.junit.runner.manipulation.*
import org.junit.runner.notification.*
Expand Down Expand Up @@ -50,7 +53,8 @@ open class AllureAndroidJUnit4(clazz: Class<*>) : Runner(), Filterable, Sortable
return AllureJunit4(androidLifecycle)
}

protected open fun createAllureAndroidLifecycle() : AllureAndroidLifecycle = AllureAndroidLifecycle()
protected open fun createAllureAndroidLifecycle() : AllureAndroidLifecycle =
createDefaultAllureAndroidLifecycle()

/**
* Creates listener for tests running in an emulated Robolectric environment.
Expand All @@ -76,13 +80,15 @@ open class AllureAndroidJUnitRunner : AndroidJUnitRunner() {
Allure.lifecycle = createAllureAndroidLifecycle()
val listenerArg = listOfNotNull(
arguments.getCharSequence("listener"),
AllureJunit4::class.java.name
AllureJunit4::class.java.name,
ExternalStoragePermissionsListener::class.java.name.takeIf { useTestStorage }
).joinToString(separator = ",")
arguments.putCharSequence("listener", listenerArg)
super.onCreate(arguments)
}

protected open fun createAllureAndroidLifecycle() : AllureAndroidLifecycle = AllureAndroidLifecycle()
protected open fun createAllureAndroidLifecycle() : AllureAndroidLifecycle =
createDefaultAllureAndroidLifecycle()
}

/**
Expand All @@ -96,3 +102,16 @@ open class MultiDexAllureAndroidJUnitRunner : AllureAndroidJUnitRunner() {
}
}

private fun createDefaultAllureAndroidLifecycle() : AllureAndroidLifecycle {
if (useTestStorage) {
return AllureAndroidLifecycle(TestStorageResultsWriter())
}

return AllureAndroidLifecycle()
}

private val useTestStorage: Boolean
get() = PropertiesUtils.loadAllureProperties()
.getProperty("allure.results.useTestStorage", "false")
.toBoolean()

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.qameta.allure.android.writer

import androidx.test.services.storage.TestStorage
import io.qameta.allure.kotlin.AllureResultsWriter
import io.qameta.allure.kotlin.OutputStreamResultsWriter
import io.qameta.allure.kotlin.model.TestResult
import io.qameta.allure.kotlin.model.TestResultContainer
import io.qameta.allure.kotlin.util.PropertiesUtils
import java.io.InputStream

class TestStorageResultsWriter : AllureResultsWriter {
private val defaultAllurePath by lazy { PropertiesUtils.resultsDirectoryPath }
private val testStorage by lazy { TestStorage() }

private val outputStreamResultsWriter = OutputStreamResultsWriter { name ->
testStorage.openOutputFile("$defaultAllurePath/$name")
}

override fun write(testResult: TestResult) {
outputStreamResultsWriter.write(testResult)
}

override fun write(testResultContainer: TestResultContainer) {
outputStreamResultsWriter.write(testResultContainer)
}

override fun write(source: String, attachment: InputStream) {
outputStreamResultsWriter.write(source, attachment)
}
}
3 changes: 2 additions & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ object Versions {
const val multiDex = "2.0.1"

object Test {
const val runner = "1.3.0"
const val orchestrator = "1.4.1"
const val runner = "1.4.0"
const val junit = "1.1.2"
const val espresso = "3.3.0"
const val robolectric = "4.3.1"
Expand Down
1 change: 1 addition & 0 deletions samples/junit4-android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ dependencies {
}

testImplementation("org.robolectric:robolectric:${Versions.Android.Test.robolectric}")
androidTestUtil("androidx.test:orchestrator:${Versions.Android.Test.orchestrator}")
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
allure.results.useTestStorage=true
allure.results.directory=allure-results
allure.link.issue.pattern=https://jira.domain-name.com/browse/{}

0 comments on commit 14632e2

Please sign in to comment.