mardi 29 novembre 2016

Generate TestNG tests during runtime

I have an obstacle to generate test's method during run-time. So problem is next: 1) I have ArrayList<String> with flexible size and flexible testNames: ["Test1","Test2","Test3","Test4"]. @BeforeSuite in the TestBase execution I am retrieving data from excel to this List. 2) My test class TestRunnerClass extends TestBase has looks like:

    class TestRunnerClass extends TestBase{                                                         

    @BeforeMethod
    private void setName(Method m)
    {
        setTestName(m.getName());
    }

    @Test(dataProvider = "getData", dataProviderClass =TestBase.class)
    private void runTest(String sheetName) throws Exceptions {

            ExcelUtils.run("sheetName"); //this is test execution for all tests(sheets) in excel file
     }
}

I am using Custom Reporter generation of surefire reports with TestNameListener and IReporter implementation and use Method name as a test Name and Class name as well:

class CustomReport implements IReporter, IExecutionListener {

/*code*/
for ( MethodResult methodResult : classResult.getMethodResults() ) {
                    List<ITestResult> results = methodResult.getResults();
                    int resultsCount = results.size();
                    assert resultsCount > 0;

                    ITestResult aResult = results.iterator().next();
                    String methodName = Utils.escapeHtml(aResult.getMethod().getMethodName());
                    long start = aResult.getStartMillis();
                    long end = aResult.getEndMillis();
                    long duration = aResult.getEndMillis() - start;

/*code */
}

So is here a way for me to modify TestRunnerClass in a way while runtime:

    @Test
    public void Test1SheetName() throws Exceptions {
           ExcelUtils.run("Test1SheetName"); 
    }
    @Test
    public void Test2SheetName() throws Exceptions {
           ExcelUtils.run("Test2SheetName"); 
    }

    @Test
    public void Test3SheetName() throws Exceptions {
           ExcelUtils.run("Test3SheetName"); 
    }
    @Test
    public void Test4SheetName() throws Exceptions {
           ExcelUtils.run("Test4SheetName"); 
    }

or probably possible override MethodName during run-time?

  • I am aware that all test-classes in the testng.xml are becomes loaded to jvm at the point of @BeforeSuite, so reflection will not help, right?
  • May be there is possible to generate new one class with wanted methods and somehow put it to new testng.xml and feed to jvm?
  • Is here any other solutions, thoughts, how can I dynamically create methodNames during runtime?




Aucun commentaire:

Enregistrer un commentaire