Skip to content

Commit

Permalink
Merge pull request #36 from rubensousa/tests
Browse files Browse the repository at this point in the history
Add instrumented tests for report library
  • Loading branch information
rubensousa authored Sep 21, 2024
2 parents c3fdbd4 + e0a975c commit efc9931
Show file tree
Hide file tree
Showing 54 changed files with 1,610 additions and 749 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ jobs:
emulator-options: -no-window -no-snapshot-save -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
script: |
./gradlew uninstallAll
./gradlew --build-cache connectedAllureReport
./gradlew --build-cache connectedDebugAndroidTest
./gradlew --build-cache carioca-android:report-sample:generateAllureReport
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,81 @@
package com.rubensousa.carioca.android.report.coroutines

import com.rubensousa.carioca.android.report.AbstractInstrumentedReportRule
import com.rubensousa.carioca.android.report.CariocaInstrumentedReporter
import com.rubensousa.carioca.android.report.DefaultInstrumentedReporter
import com.rubensousa.carioca.android.report.coroutines.internal.InstrumentedCoroutineTestBuilder
import com.rubensousa.carioca.android.report.InstrumentedReportRule
import com.rubensousa.carioca.android.report.InstrumentedReporter
import com.rubensousa.carioca.android.report.coroutines.internal.InstrumentedCoroutineTest
import com.rubensousa.carioca.android.report.interceptor.CariocaInstrumentedInterceptor
import com.rubensousa.carioca.android.report.interceptor.DumpViewHierarchyInterceptor
import com.rubensousa.carioca.android.report.interceptor.LoggerInterceptor
import com.rubensousa.carioca.android.report.recording.RecordingOptions
import com.rubensousa.carioca.android.report.screenshot.ScreenshotDelegate
import com.rubensousa.carioca.android.report.screenshot.ScreenshotOptions
import com.rubensousa.carioca.android.report.stage.InstrumentedTestReport
import org.junit.runner.Description
import com.rubensousa.carioca.android.report.storage.ReportStorageProvider
import com.rubensousa.carioca.android.report.storage.TestStorageProvider
import com.rubensousa.carioca.report.runtime.TestMetadata
import com.rubensousa.carioca.report.runtime.TestReportConfig
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext

/**
* A report rule for coroutine tests
* A report rule for coroutine tests.
* Please check [InstrumentedReportRule] for more info on how to use the basic report functionality
*/
open class InstrumentedCoroutineReportRule(
reporter: CariocaInstrumentedReporter = DefaultInstrumentedReporter(),
recordingOptions: RecordingOptions = RecordingOptions(),
screenshotOptions: ScreenshotOptions = ScreenshotOptions(),
interceptors: List<CariocaInstrumentedInterceptor> = listOf(
LoggerInterceptor(),
DumpViewHierarchyInterceptor()
),
open class InstrumentedCoroutineReportRule internal constructor(
reporter: InstrumentedReporter,
recordingOptions: RecordingOptions,
screenshotOptions: ScreenshotOptions,
private val interceptors: List<CariocaInstrumentedInterceptor>,
private val storageProvider: ReportStorageProvider,
) : AbstractInstrumentedReportRule(
reporter = reporter,
recordingOptions = recordingOptions,
screenshotOptions = screenshotOptions,
interceptors = interceptors
screenshotOptions = screenshotOptions
) {

private val testBuilder = InstrumentedCoroutineTestBuilder()
/**
* Public constructor that uses [TestStorageProvider] to save the reports
*/
constructor(
reporter: InstrumentedReporter = DefaultInstrumentedReporter(),
recordingOptions: RecordingOptions = RecordingOptions(),
screenshotOptions: ScreenshotOptions = ScreenshotOptions(),
interceptors: List<CariocaInstrumentedInterceptor> = listOf(
LoggerInterceptor(),
DumpViewHierarchyInterceptor()
),
) : this(
reporter = reporter,
recordingOptions = recordingOptions,
screenshotOptions = screenshotOptions,
interceptors = interceptors,
storageProvider = TestStorageProvider
)

override fun createTest(description: Description): InstrumentedTestReport {
return testBuilder.build(
description = description,
recordingOptions = RecordingOptions.from(description) ?: recordingOptions,
screenshotOptions = ScreenshotOptions.from(description) ?: screenshotOptions,
override fun createTest(
reportConfig: TestReportConfig?,
testMetadata: TestMetadata,
recordingOptions: RecordingOptions,
screenshotOptions: ScreenshotOptions,
): InstrumentedTestReport {
val outputPath = reporter.getOutputDir(testMetadata)
val testReport = InstrumentedCoroutineTest(
outputPath = outputPath,
metadata = testMetadata,
recordingOptions = recordingOptions,
screenshotDelegate = ScreenshotDelegate(
outputPath = outputPath,
defaultOptions = screenshotOptions,
storageProvider = storageProvider
),
interceptors = interceptors,
reporter = reporter,
interceptors = interceptors
storageProvider = storageProvider
)
reportConfig?.applyTo(testReport)
return testReport
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import com.rubensousa.carioca.android.report.screenshot.ScreenshotDelegate
import com.rubensousa.carioca.android.report.screenshot.ScreenshotOptions
import com.rubensousa.carioca.android.report.stage.InstrumentedReportDelegateFactory
import com.rubensousa.carioca.android.report.stage.InstrumentedStageReport
import com.rubensousa.carioca.android.report.stage.InstrumentedStageType
import com.rubensousa.carioca.android.report.storage.ReportStorageProvider
import com.rubensousa.carioca.report.runtime.ExecutionIdGenerator
import com.rubensousa.carioca.report.runtime.StageStack

Expand All @@ -38,6 +40,7 @@ internal class InstrumentedCoroutineDelegate(
private val stack: StageStack<InstrumentedStageReport>,
private val interceptors: List<CariocaInstrumentedInterceptor>,
private val outputPath: String,
private val storageProvider: ReportStorageProvider,
) : InstrumentedCoroutineStageScope {

override fun screenshot(description: String, options: ScreenshotOptions?) {
Expand All @@ -56,7 +59,7 @@ internal class InstrumentedCoroutineDelegate(

override suspend fun scenario(scenario: InstrumentedCoroutineScenario) {
executeStageAwait(createScenario(scenario)) { stage ->
stage.execute()
stage.executeScenario(scenario)
}
}

Expand Down Expand Up @@ -84,24 +87,27 @@ internal class InstrumentedCoroutineDelegate(
interceptors.intercept { onStagePassed(stage) }
}

private fun createStep(title: String, id: String?): InstrumentedCoroutineStep {
return InstrumentedCoroutineStep(
private fun createStep(title: String, id: String?): InstrumentedCoroutineStage {
return InstrumentedCoroutineStage(
id = id ?: ExecutionIdGenerator.get(),
title = title,
type = InstrumentedStageType.STEP,
outputPath = outputPath,
delegateFactory = delegateFactory,
id = id ?: ExecutionIdGenerator.get(),
title = title
storageProvider = storageProvider,
)
}

private fun createScenario(
scenario: InstrumentedCoroutineScenario,
): InstrumentedCoroutineScenarioImpl {
return InstrumentedCoroutineScenarioImpl(
): InstrumentedCoroutineStage {
return InstrumentedCoroutineStage(
outputPath = outputPath,
delegateFactory = delegateFactory,
id = scenario.id,
title = scenario.title,
scenario = scenario,
type = InstrumentedStageType.SCENARIO,
storageProvider = storageProvider
)
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,29 @@ import com.rubensousa.carioca.android.report.coroutines.InstrumentedCoroutineSce
import com.rubensousa.carioca.android.report.coroutines.InstrumentedCoroutineStageScope
import com.rubensousa.carioca.android.report.screenshot.ScreenshotOptions
import com.rubensousa.carioca.android.report.stage.InstrumentedReportDelegateFactory
import com.rubensousa.carioca.android.report.stage.InstrumentedStepReport
import com.rubensousa.carioca.android.report.stage.InstrumentedStageReport
import com.rubensousa.carioca.android.report.stage.InstrumentedStageType
import com.rubensousa.carioca.android.report.storage.ReportStorageProvider

internal class InstrumentedCoroutineStep(
internal class InstrumentedCoroutineStage(
type: InstrumentedStageType,
outputPath: String,
delegateFactory: InstrumentedReportDelegateFactory<InstrumentedCoroutineStageScope>,
id: String,
title: String,
) : InstrumentedStepReport(outputPath, id, title), InstrumentedCoroutineStageScope {
storageProvider: ReportStorageProvider,
private val id: String,
private val title: String,
) : InstrumentedStageReport(
type = type,
outputPath = outputPath,
storageProvider = storageProvider
), InstrumentedCoroutineStageScope {

private val delegate = delegateFactory.create(this)

override fun getId(): String = id

override fun getTitle(): String = title

override fun screenshot(description: String, options: ScreenshotOptions?) {
delegate.screenshot(description, options)
}
Expand All @@ -40,7 +52,11 @@ internal class InstrumentedCoroutineStep(
id: String?,
action: suspend InstrumentedCoroutineStageScope.() -> Unit,
) {
delegate.step(title, id, action)
delegate.step(
title = title,
id = id,
action = action
)
}

override suspend fun scenario(scenario: InstrumentedCoroutineScenario) {
Expand All @@ -56,4 +72,9 @@ internal class InstrumentedCoroutineStep(
pass()
}

internal suspend fun executeScenario(scenario: InstrumentedCoroutineScenario) {
scenario.run(this)
pass()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.rubensousa.carioca.android.report.coroutines.internal

import com.rubensousa.carioca.android.report.CariocaInstrumentedReporter
import com.rubensousa.carioca.android.report.InstrumentedReporter
import com.rubensousa.carioca.android.report.coroutines.InstrumentedCoroutineScenario
import com.rubensousa.carioca.android.report.coroutines.InstrumentedCoroutineStageScope
import com.rubensousa.carioca.android.report.coroutines.InstrumentedCoroutineTestScope
Expand All @@ -27,22 +27,25 @@ import com.rubensousa.carioca.android.report.screenshot.ScreenshotOptions
import com.rubensousa.carioca.android.report.stage.InstrumentedReportDelegateFactory
import com.rubensousa.carioca.android.report.stage.InstrumentedStageReport
import com.rubensousa.carioca.android.report.stage.InstrumentedTestReport
import com.rubensousa.carioca.android.report.storage.ReportStorageProvider
import com.rubensousa.carioca.report.runtime.TestMetadata

internal class InstrumentedCoroutineTest(
outputPath: String,
metadata: TestMetadata,
recordingOptions: RecordingOptions,
reporter: CariocaInstrumentedReporter,
reporter: InstrumentedReporter,
screenshotDelegate: ScreenshotDelegate,
interceptors: List<CariocaInstrumentedInterceptor>,
storageProvider: ReportStorageProvider,
) : InstrumentedTestReport(
outputPath = outputPath,
metadata = metadata,
recordingOptions = recordingOptions,
screenshotDelegate = screenshotDelegate,
reporter = reporter,
interceptors = interceptors
interceptors = interceptors,
storageProvider = storageProvider
), InstrumentedCoroutineTestScope {

private val coroutineDelegateFactory =
Expand All @@ -55,6 +58,7 @@ internal class InstrumentedCoroutineTest(
stack = stageStack,
interceptors = interceptors,
outputPath = outputPath,
storageProvider = storageProvider
)
}
}
Expand Down

This file was deleted.

Loading

0 comments on commit efc9931

Please sign in to comment.