How to use Junit Annotation in Android UI Framework- Kotlin & Espresso based

Problem Statement:
We need to add a capability in the framework, where they can Create and use JUnit tags to filter/group and run tests. e.g.

@Sanity
@Regression
@MyAnnotation

So, basically we will Run specific Android Espresso tests by creating cust…


This content originally appeared on DEV Community and was authored by Raghwendra Sonu

Problem Statement:
We need to add a capability in the framework, where they can Create and use JUnit tags to filter/group and run tests. e.g.

@Sanity
@Regression
@MyAnnotation

So, basically we will Run specific Android Espresso tests by creating custom annotations.

Solution:
Step 1) Create a custom annotation

import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy

//Create Annotation Class- Step3
@Target(
    AnnotationTarget.FUNCTION,
    AnnotationTarget.PROPERTY_GETTER,
    AnnotationTarget.PROPERTY_SETTER,
    AnnotationTarget.ANNOTATION_CLASS,
    AnnotationTarget.CLASS
)
@Retention(
    RetentionPolicy.RUNTIME
)
annotation class MyAnnotation\

@Target specifies the possible kinds of elements which can be annotated with the annotation (classes, functions, properties, expressions etc.)

We used it for annotate some functions, so we use AnnotationTarget.FUNCTION

@Retention specifies whether the annotation is stored in the compiled class files and whether it’s visible through reflection at runtime (by default, both are true).

AnnotationRetention.RUNTIME makes sure that the Rat annotation is visible to the test runner during the runtime.

Step 2) Annotate @MyAnnotation on the tests you want to run

@Test @MyAnnotation
fun AddTaskToDoListTestAndMarkDone() {
   ......
   ......
}

Step 3) Use gradlew to run only MyAnnotation tests

./gradlew connectedAndroidTest -P android.testInstrumentationRunnerArguments.annotation=com.example.todolist.app.MyAnnotation


This content originally appeared on DEV Community and was authored by Raghwendra Sonu


Print Share Comment Cite Upload Translate Updates
APA

Raghwendra Sonu | Sciencx (2021-05-22T05:16:02+00:00) How to use Junit Annotation in Android UI Framework- Kotlin & Espresso based. Retrieved from https://www.scien.cx/2021/05/22/how-to-use-junit-annotation-in-android-ui-framework-kotlin-espresso-based/

MLA
" » How to use Junit Annotation in Android UI Framework- Kotlin & Espresso based." Raghwendra Sonu | Sciencx - Saturday May 22, 2021, https://www.scien.cx/2021/05/22/how-to-use-junit-annotation-in-android-ui-framework-kotlin-espresso-based/
HARVARD
Raghwendra Sonu | Sciencx Saturday May 22, 2021 » How to use Junit Annotation in Android UI Framework- Kotlin & Espresso based., viewed ,<https://www.scien.cx/2021/05/22/how-to-use-junit-annotation-in-android-ui-framework-kotlin-espresso-based/>
VANCOUVER
Raghwendra Sonu | Sciencx - » How to use Junit Annotation in Android UI Framework- Kotlin & Espresso based. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/22/how-to-use-junit-annotation-in-android-ui-framework-kotlin-espresso-based/
CHICAGO
" » How to use Junit Annotation in Android UI Framework- Kotlin & Espresso based." Raghwendra Sonu | Sciencx - Accessed . https://www.scien.cx/2021/05/22/how-to-use-junit-annotation-in-android-ui-framework-kotlin-espresso-based/
IEEE
" » How to use Junit Annotation in Android UI Framework- Kotlin & Espresso based." Raghwendra Sonu | Sciencx [Online]. Available: https://www.scien.cx/2021/05/22/how-to-use-junit-annotation-in-android-ui-framework-kotlin-espresso-based/. [Accessed: ]
rf:citation
» How to use Junit Annotation in Android UI Framework- Kotlin & Espresso based | Raghwendra Sonu | Sciencx | https://www.scien.cx/2021/05/22/how-to-use-junit-annotation-in-android-ui-framework-kotlin-espresso-based/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.