Skip to content

Commit 12b4e03

Browse files
authored
ANDROID-15807 Add @IgnoreLoggerazzi annotation to ignore a test (#7)
1 parent 9f32d0c commit 12b4e03

File tree

5 files changed

+28
-2
lines changed

5 files changed

+28
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,6 @@ class FakeAnalyticsTracker : AnalyticsTracker, LogsRecorder<String> {
136136
By default, Loggerazzi rule compares recorded logs by ensuring these are equal and in same order than the baseline logs.
137137

138138
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.
139+
140+
### Ignore a test
141+
If you want to ignore a test from Loggerazzi verification, you can use the `@IgnoreLoggerazzi` annotation in your test.

app/src/androidTest/java/com/telefonica/loggerazzi/ExampleInstrumentedTest.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ class ExampleInstrumentedTest {
3939
fun testEmpty() {
4040
// Empty, just to test empty logs comparation.
4141
}
42+
43+
@Test
44+
@IgnoreLoggerazzi
45+
fun testIgnoreLoggerazzi() {
46+
recorder.record("My log")
47+
}
48+
49+
@Test
50+
@IgnoreLoggerazzi
51+
fun testIgnoreLoggerazziWithoutGoldenFile() {
52+
recorder.record("My log")
53+
}
4254
}
4355

4456
class FakeTestRecorder: LogsRecorder<String> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
My log
2+
My second log
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.telefonica.loggerazzi
2+
3+
4+
@Retention(AnnotationRetention.RUNTIME)
5+
@Target(AnnotationTarget.FUNCTION)
6+
annotation class IgnoreLoggerazzi

loggerazzi/src/main/java/com/telefonica/loggerazzi/LoggerazziRule.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import java.io.File
99
class LoggerazziRule(
1010
recorder: LogsRecorder<String>,
1111
comparator: LogComparator<String> = DefaultLogComparator(),
12-
): GenericLoggerazziRule<String>(
12+
) : GenericLoggerazziRule<String>(
1313
recorder = recorder,
1414
stringMapper = object : StringMapper<String> {
1515
override fun fromLog(log: String): String = log
1616
override fun toLog(stringLog: String): String = stringLog
1717
},
1818
comparator = comparator,
1919
)
20+
2021
open class GenericLoggerazziRule<LogType>(
2122
val recorder: LogsRecorder<LogType>,
2223
private val stringMapper: StringMapper<LogType>,
@@ -47,6 +48,8 @@ open class GenericLoggerazziRule<LogType>(
4748
override fun succeeded(description: Description?) {
4849
super.succeeded(description)
4950

51+
val isTestIgnored = description?.getAnnotation(IgnoreLoggerazzi::class.java) != null
52+
5053
val testName = "${description?.className}_${description?.methodName}"
5154
val fileName = "${testName}.${System.nanoTime()}"
5255

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

59-
if (InstrumentationRegistry.getArguments().getString("record") != "true") {
62+
if (InstrumentationRegistry.getArguments().getString("record") != "true" && !isTestIgnored) {
6063
val goldenFile =
6164
InstrumentationRegistry.getInstrumentation().context.assets.open(
6265
"loggerazzi-golden-files/${testName}.txt"

0 commit comments

Comments
 (0)