Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,6 @@ class FakeAnalyticsTracker : AnalyticsTracker, LogsRecorder<String> {
By default, Loggerazzi rule compares recorded logs by ensuring these are equal and in same order than the baseline logs.

In case a different comparation mechanism is needed (such as ignoring the order of the events, or ignoring certain logs), you can implement an specific [LogComparator](loggerazzi/src/main/java/com/telefonica/loggerazzi/LogComparator.kt), which can be provided to the LoggerazziRule on its creation.

### Ignore a test
If you want to ignore a test from Loggerazzi verification, you can use the `@IgnoreLoggerazzi` annotation in your test.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ class ExampleInstrumentedTest {
fun testEmpty() {
// Empty, just to test empty logs comparation.
}

@Test
@IgnoreLoggerazzi
fun testIgnoreLoggerazzi() {
recorder.record("My log")
}

@Test
@IgnoreLoggerazzi
fun testIgnoreLoggerazziWithoutGoldenFile() {
recorder.record("My log")
}
}

class FakeTestRecorder: LogsRecorder<String> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
My log
My second log
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.telefonica.loggerazzi


@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FUNCTION)
annotation class IgnoreLoggerazzi
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import java.io.File
class LoggerazziRule(
recorder: LogsRecorder<String>,
comparator: LogComparator<String> = DefaultLogComparator(),
): GenericLoggerazziRule<String>(
) : GenericLoggerazziRule<String>(
recorder = recorder,
stringMapper = object : StringMapper<String> {
override fun fromLog(log: String): String = log
override fun toLog(stringLog: String): String = stringLog
},
comparator = comparator,
)

open class GenericLoggerazziRule<LogType>(
val recorder: LogsRecorder<LogType>,
private val stringMapper: StringMapper<LogType>,
Expand Down Expand Up @@ -47,6 +48,8 @@ open class GenericLoggerazziRule<LogType>(
override fun succeeded(description: Description?) {
super.succeeded(description)

val isTestIgnored = description?.getAnnotation(IgnoreLoggerazzi::class.java) != null

val testName = "${description?.className}_${description?.methodName}"
val fileName = "${testName}.${System.nanoTime()}"

Expand All @@ -56,7 +59,7 @@ open class GenericLoggerazziRule<LogType>(
testFile.createNewFile()
testFile.writeText(log)

if (InstrumentationRegistry.getArguments().getString("record") != "true") {
if (InstrumentationRegistry.getArguments().getString("record") != "true" && !isTestIgnored) {
val goldenFile =
InstrumentationRegistry.getInstrumentation().context.assets.open(
"loggerazzi-golden-files/${testName}.txt"
Expand Down