javaselenium-webdrivertestngtestng-dataproviderinvocation

How to set invocation count during execution for test method that uses data provider in TestNg &java/ Kotlin?


I did refer this link and understood about IAnnotationTransformer.

I have a scenario where I have parameterized data with data provider. Need to run test with certain data for N number of times using invocation Count which.

How do I leverage the transform?

@DataProvider(name = "loginData", parallel = false)
fun loginDataProvider(): MutableIterator<String> {
    val loginTestData: ArrayList<String> = ["abc", "def", "xyz"]
    return loginTestData.iterator()
}
    
@Test(dataProvider = "loginData")
fun funloginTest(loginDetails: String){
    print("The value of $loginDetails")
}

How do I run this test 4 times for the parameter "def"?

Ideally I want to get the invocationCount before every run for the test case from an external source like a json and run the test N number of times for specific data

I tried looking at many invocationCount and IAnnotationTransformer and couldn't find the exact answer. Any idea or code snippet to solve the problem can help here


Solution

  • IAnnotationTransformer doesn't seems to work in this case. I suggest to modify the behaviour of DataProvider method instead.

    1. create a mapping of value need to override invocation count.
      e.g. { "funloginTest" : { "def" : 4 } }
    2. Inside loginDataProvider, we can use the test method name(Referring to document of DataProvider) to see which data need to have different invocation count.
    class RepeatedRunTest {
        // TODO read it from json
        private val testToCaseInvocationCount = mapOf("funloginTest" to mapOf("def" to 4))
    
        @DataProvider(name = "loginData", parallel = false)
        fun loginDataProvider(testMethod: Method): MutableIterator<String> {
            println(testMethod.name)
            val loginTestData: Array<String> = arrayOf("abc", "def", "xyz")
            return loginTestData.map { testData ->
                List(
                    // repeat if needed
                    testToCaseInvocationCount
                        .getOrDefault(testMethod.name, emptyMap())
                        .getOrDefault(testData, 1)
                ) { testData }
            }
                .flatten().toMutableList().listIterator()
        }
    
        @Test(dataProvider = "loginData")
        fun funloginTest(loginDetails: String) {
            print("The value of $loginDetails")
        }
    
    }